前言

公司之前都是http协议的项目,近期需要将主系统全部升级为https协议,所以才有了这篇文章,记录一下整个升级的过程和踩的坑

最基本的 nginx的配置
首先是监听http的80端口 转发到https的443端口

如果是有的特殊路径不想转发则用location的精确匹配 rewrite只是放在 / 下的模糊匹配里面 只有精确匹配匹配不到的时候 再去走rewrite转发请求

server
    {
        listen       80;
        server_name  haah.cn www.haha.cn ;
        underscores_in_headers on;

        location / {
            rewrite ^(.*)$ https://$host$1 permanent;
        }
        location /haah_pc/ {
            root /hehe/hehe/web;
            index index.html;
            try_files $uri  /haah_pc/index.html;
        }
    }

https的nginx配置

这里注意一个坑 underscores_in_headers on;这个配置是为了允许header头的key值中可以有下划线 否则会报错

server {

        server_name     haha.cn www.haha.cn;
        underscores_in_headers on;
        listen  443;
        ssl on;
        ssl_certificate   /mnt/ssl/heeh.cn.pem;
        ssl_certificate_key   /mnt/ssl/hehe.cn.key;
        ssl_session_timeout  5m;
        ssl_protocols SSLv3 SSLv2 TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers  ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
        ssl_prefer_server_ciphers   on;

        access_log  logs/host.access.log  main;
        error_log  logs/error.log  error;

        location /hehe/ {
            root   /hehe/hehe/web;
            index  index.html;
            try_files $uri  /hehe/index.html;
        }
}

注意websocket的坑

由于http升级为https 导致websocket也升级为了wss 这里有一个坑 需要为websocket配置下代理转向http

location /socket.io/  {
            proxy_pass http://heeh.haha.cn:888;        #通过配置端口指向部署websocker的项目
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
            proxy_set_header X-real-ip $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
     }
前端项目的坑

由于之前的好多外链还有图片都是http的导致无法访问 所以此时前端项目都需要增加一个meta头设置 让浏览器将http协议的外链自动升级为https

<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">

更多推荐