nginx搭建小程序所使用的的 https和wss共存並且http301跳轉https

http https ws wss 的區別,因爲小程序只能使用https 所以它的websoket 只能使用wss 協議

http -> new WebSocket(‘ws://xxx’) 一般使用 80端口
https -> new WebSocket(‘wss://xxx’)一般使用443 端口
http://img.520haigo.com
ws://img.520haigo.com/wss
https://img.520haigo.com
wss:///img.520haigo.com/wss

nginx 搭建出小程序所使用的https和wss共存 nginx配置如下

這些配置放到一個nginx配置下面就可以 例如: conf.d/img.520haigo.com.conf文件中的配置 如果使用阿里雲服務器需要安全組中開啓9501端口

upstream websocket{           //監聽 你的後端soket的端口  我用的swoole9501 
  server 127.0.0.1:9501 fail_timeout=0;
}
server{                                 //這裏是http 轉發到  https協議,適用於網頁端 例子就是www.baidu.com 301 跳轉操作
        listen 80;
        server_name  img.520haigo.com;
        location / {
            if ($request_method ~ ^(POST|DELETE|OPTIONS)$) {
               proxy_pass https://img.520haigo.com;
               break ;
            }
            rewrite ^/(.*)$   https://img.520haigo.com/$1 permanent;
        }
}	
server{
 		listen   443  ssl;
        server_name  img.520haigo.com;
        index index.html index.htm index.php;
        root /alidata/www/doodoo/blog/public;
        #ssl on;
        ssl_certificate /alidata/server/nginx-1.7.9/2471666_img.520abc.com.pem;                //你的證書地址
        ssl_certificate_key /alidata/server/nginx-1.7.9/2471666_img.520abc.com.key;			//你的證書地址
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        location /wss{                                                //這裏是websoket的轉發操作,如果訪問 wss://....../wss 就會進入到這裏轉發到後端的ws協議
                proxy_pass http://websocket;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "Upgrade";
                proxy_read_timeout 600;
        }
        location / {
                try_files $uri $uri/ /index.php?$query_string;  //laravel 框架必須配置的文件重寫操作
        }
        location ~ .*\.(php|php5)?$
        {
                #fastcgi_pass  unix:/tmp/php-cgi.sock;
                fastcgi_pass  127.0.0.1:9000;    //監聽的php端口
                fastcgi_index index.php;
                include fastcgi.conf;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
                expires 30d;
        }
        location ~ .*\.(js|css)?$
        {
                expires 1h;
        }
}

上面的配置適用於: http => 301 跳轉https && https=>wss
https 證書的話直接去阿里雲申請免費的DV證書就可以

注意: 筆者在上面的配置這裏踩過坑 ,https跳轉wss協議 最主要的配置有三個 如下

proxy_http_version 1.1; //告訴nginx 使用http1.1 長連接協議 http 默認是1.0的
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection “Upgrade”;//告訴nginx 使用http的升級版,並會在http頭信息中添加websoket 的信息

如果 proxy_http_version 1.1 沒有設置,會出現wx.connectSocket 出現連接成功
在這裏插入圖片描述
但是wx.onSocketOpen 不會出現成功,發送消息也會失敗,提示soket連接失敗
注意:筆者配置的時候遇到了 配置 proxy_http_version nginx 啓動報錯在這裏插入圖片描述
遇到這個問題,我選擇了重裝nginx 之前是 1.7.2 後來重裝的nginx-1.8.2

注意:nginx 在60秒內沒有消息接收的話,會自動斷開連接 proxy_read_timeout 600; 設置nginx監聽時常 改爲600 秒但也不是長久之計,socket還需要添加 心跳檢測才行呀!!!

前端訪問 wss://img.520haigo.com/wss

在這裏插入圖片描述
在這裏插入圖片描述
注意啦:nginx 裏面配置的是 /wss 所以域名裏面 wss後邊切不可加/ , wss/ 這樣的話會返回301

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章