Nginx 安装 SSL 配置 HTTPS
一、检测 Nginx SSL 模块是否已安装
1、进入docker下Nginx容器
92a0c8382918 为 CONTAINER_ID
docker exec -it 92a0c8382918 /bin/bash
2、查看nginx编译参数:nginx -V
nginx -V
3、查找是否带有 --with-http_gzip_static_module 参数
可将 configure arguments 参数信息拷贝出来,进行搜索
如果没有–with-http_gzip_static_module这个参数,需要为 nginx 安装 SSL 模块,
这里安装Nginx后就有这个参数,所以无需安装。
4、进入etc/nginx 目录,查看 nginx.conf 文件是否存在
Nginx默认会安装在etc目录下面
cd etc
ls
进入nginx目录
cd nginx
ls
我们看到了nginx.conf配置文件。
5、使用 Ctrl + D 退出容器
二、SSL 证书部署
1、下载申请好的 ssl 证书文件压缩包到本地并解压
这里是用的 pem 与 key 文件,文件名可以更改。
2、 Nginx 挂载宿主机的目录
(1)、创建挂载目录
mkdir /etc/nginx
mkdir /etc/nginx/conf.d
(2)、复制容器里的conf到宿主机
docker cp 92a0c8382918:/etc/nginx/nginx.conf /etc/nginx/nginx.conf
docker cp 92a0c8382918:/etc/nginx/conf.d /etc/nginx/conf.d
(3)、 新建 cert 文件夹存放证书文件
mkdir /etc/nginx/cert
3、上传 ssl 证书文件到 /etc/nginx/cert 目录
具体如何上传请查看:
FileZilla 将本地文件上传到linux目录
三、Nginx.conf 配置
1、打开 nginx.conf 配置文件
cd /etc/nginx
vim nginx.conf
nginx.conf 内容如下:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
请注意最后一行 ,由此可知 ,包含了最终执行的配置文件在 conf.d 目录的 default.conf , 因此需要编辑该文件。
include /etc/nginx/conf.d/*.conf;
2、编辑配置文件 default.conf
先切换到 /etc/nginx/conf.d 目录
cd /etc/nginx/conf.d
打开default.conf 配置文件
vim default.conf
(1)、配置 https 证书及域名:
server {
listen 443 ssl;
server_name timematter.qnr-chat.com;
ssl_certificate /etc/nginx/cert/6007466_timematter.qnr-chat.com.pem;
ssl_certificate_key /etc/nginx/cert/6007466_timematter.qnr-chat.com.key;
}
注意:
Nginx1.15 版本及以上开启 ssl , 要这样写 :
listen 443 ssl;
Nginx1.15 版本以前开启 ssl, 要这样写:
listen 443;
ssl on;
(2)、配置反向的代理IP及端口
获取需要转发的应用服务器IP ,如果该应用也部署在docker 中,则可以通过以下方法查询:
docker ps
docker inspect 92a0c8382918
此处为: 172.17.0.4
配置 proxy_pass 如下:
server {
listen 443 ssl;
server_name timematter.qnr-chat.com;
ssl_certificate /etc/nginx/cert/6007466_timematter.qnr-chat.com.pem;
ssl_certificate_key /etc/nginx/cert/6007466_timematter.qnr-chat.com.key;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
proxy_pass http://172.17.0.4:8090;
}
}
(3)、配置http 重定向 https
# 反向代理
server {
listen 80;
server_name timematter.qnr-chat.com;
rewrite ^(.*)$ https://${server_name}$1 permanent;#将80端口的访问转发到443端口
}
(4)、保存配置文件 default.conf
完整配置如下:
server {
listen 443 ssl;
server_name timematter.qnr-chat.com;
ssl_certificate /etc/nginx/cert/6007466_timematter.qnr-chat.com.pem;
ssl_certificate_key /etc/nginx/cert/6007466_timematter.qnr-chat.com.key;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
proxy_pass http://172.17.0.4:8090;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
server {
listen 80;
server_name timematter.qnr-chat.com;
rewrite ^(.*)$ https://${server_name}$1 permanent;#将80端口的访问转发到443端口
}
四、重启 nginx
(1)、停止容器
docker stop 92a0c8382918
(2)、删除容器
docker rm 92a0c8382918
(3)、重新启动一个有挂载目录的容器
docker run -d -it -p 80:80 -p 443:443 -v /etc/nginx/nginx.conf:/etc/nginx/nginx.conf -v /etc/nginx/cert:/etc/nginx/cert -v/etc/nginx/logs:/var/log/nginx -v /etc/nginx/conf.d:/etc/nginx/conf.d nginx
五、测试
1、开放宿主机 443 端口
2、浏览器运行配置的HTTPS 域名
https://timematter.qnr-chat.com/x.html
3、浏览器运行配置的HTTP 域名
http://timematter.qnr-chat.com/x.html
可见已自动跳转到 https 地址