记录一下有关于docker nginx ssl证书的坑

最近公司需要部署阿里云服务器有关于ssl证书的坑,目前记录一下
1.2020/06/11 02:24:17 [warn] 1#1: the “ssl” directive is deprecated, use the “listen … ssl” directive instead in /etc/nginx/nginx.conf:26
nginx: [warn] the “ssl” directive is deprecated, use the “listen … ssl” directive instead in /etc/nginx/nginx.conf:26

2020/06/11 02:24:17 [warn] 1#1: the "ssl" directive is deprecated, use the "listen ... ssl" directive instead in /etc/nginx/nginx.conf:26
nginx: [warn] the "ssl" directive is deprecated, use the "listen ... ssl" directive instead in /etc/nginx/nginx.conf:26

意思是,在配置文件已经配置过
listen 443 ssl;
则不需要再进行ssl on;的配置,自行删除ssl on;就好
多用于nginx 1.15以上的版本,自行查看就好

2.端口需要特殊暴露
docker run -di -p 80:80 -p 443:443 -v /root/ssl:/root/ssl --net mpmcs --ip x.x.x.x --name=nginx nginx
不仅仅要暴露80端口,也要暴露443端口,同时在阿里云的服务器的安全组配置下也要放行80和443端口,-v参数里做的是ssl证书映射,可以不管

3.错误nginx 400 bad request“The plain HTTP request was sent to HTTPS port”
在这里插入图片描述
这个是因为nginx报这种错误是因为每一次用户请求试图通过HTTP访问你的网站,这个请求被重定向到HTTPS。于是nginx预计使用SSL交互,但原来的请求(通过端口80接收)是普通的HTTP请求,于是会产生错误。
解决办法
“listen 443;”修改为“listen 443 ssl”;新增“listen 80”,这样Nginx就可以同时处理HTTP请求和HTTPS请求了,具体参考如下:

server {

listen 80;
listen 443 ssl;
server_name domanname.com www.domanname.com;  #将localhost修改为您证书绑定的域名,例如:www.example.com。
#rewrite ^(.*)$ https://$host$1 permanent;

ssl_certificate /root/ssl/domanname.pem;   #将domain name.pem替换成您证书的文件名。
ssl_certificate_key  /root/ssl/domanname.key;   #将domain name.key替换成您证书的密钥文件名。
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;  #使用此加密套件。
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;   #使用该协议进行配置。
ssl_prefer_server_ciphers on;   

因为我们是api有关于proxy的配置
proxy_redirect http:// https://;
也需要添加上方代码到整个转发函数内

这样便可以实现http到https的随意跳转

下面贴一下有关于整个nginx的配置,配置了最大连接数优化,nginx负载均衡分发,ssl证书以及域名映射


user  nginx;
worker_processes  4;#几核cup写几

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  65535;
}


http {

server {

listen 80;
listen 443 ssl;
server_name domanname.com www.domanname.com;  #将localhost修改为您证书绑定的域名,例如:www.example.com。
#rewrite ^(.*)$ https://$host$1 permanent;

ssl_certificate /root/ssl/4049630_www.domanname.com.pem;   #将domain name.pem替换成您证书的文件名。
ssl_certificate_key  /root/ssl/4049630_www.domanname.com.key;   #将domain name.key替换成您证书的密钥文件名。
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;  #使用此加密套件。
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;   #使用该协议进行配置。
ssl_prefer_server_ciphers on;   

location / {

proxy_pass http://blance;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect http:// https://;
client_max_body_size    2000m;

}

}

upstream blance{

server x.x.x.x:8080 ;

server x.x.x.x:8080;


}


    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;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值