访问前端项目界面时,若不配置ssl只能通过http方式访问。配置好ssl后可以通过https方式访问。
下面是nginx.conf文件中里的部分配置:
server {
listen 8090 ssl;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
ssl_password_file /var/log/nginx/ssl/pwd;
client_max_body_size 10m;
ssl_certificate /var/log/nginx/ssl/server.crt;
ssl_certificate_key /var/log/nginx/ssl/server.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
location /v2 {
proxy_pass http://xx.xx.xx.xx:8081;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
real_ip_header X-Forwarded-For;
}
location /404 {
index index.html index.htm;
root /usr/share/nginx/html;
error_page 404 /index.html;
}
location / {
index index.html index.htm;
root /usr/share/nginx/html;
error_page 404 /index.html;
}
# redirect server error pages to the static page /50x.html
#
#error_page 500 502 503 504 /50x.html;
location = /50x.html {
root 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;
#}
}
关键的地方:
listen 8090 ssl;
ssl_password_file /var/log/nginx/ssl/pwd; #加密私钥的口令存放文件
client_max_body_size 10m;
ssl_certificate /var/log/nginx/ssl/server.crt; #公钥证书
ssl_certificate_key /var/log/nginx/ssl/server.key; #私钥文件
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
nginx.conf里面配置的/var/log/nginx/ssl/pwd,/var/log/nginx/ssl/server.crt,/var/log/nginx/ssl/server.key都指的是容器生成后的,容器内容的路径。不是宿主机的路径。
包括放置静态资源文件的路径 /usr/share/nginx/html也是指的容器内容的路径。在我们生成容器时,已给/usr/share/nginx/html挂载好了外部宿主机的路径(/data/nginx8090/html)。相关文件的修改只需在/data/nginx8090/html下修改 会自动同步到容器内。
location / {
index index.html index.htm;
root /usr/share/nginx/html;
error_page 404 /index.html;
}
注意:如果前期创建Nginx容器时没有要求使用https方式,访问前端。即我们没有在nginx.conf中进行相关ssl配置。后期需要改成https访问,这个时候,容器已经创建了,再管理外部挂载,不好整了。
可以先在外部挂载文件nginx.conf中添 加/var/log/nginx/ssl/pwd,/var/log/nginx/ssl/server.crt,/var/log/nginx/ssl/server.key配置。然后进入容器内部,把pwd、server.crt、server.key文件放到/var/log/nginx/ssl目录下。
或者把容器暂停,然后删除容器。之后根据添加了新挂载点目录的脚本创建容器。
也可以修改已有容器里的挂载点配置,然后重启容器。但是这样有点太麻烦了。