網站添加https后websocket不能使用


注意點:
如果網站使用HTTPS,WebSocket必須要使用wss協議;
使用wss協議的連接請求必須只能寫域名,而非IP+端口;
建議在URL域名后面為websocket定義一個路徑,本例中是/socket/;

var socket = new WebSocket("wss://www.aabb.cn/socket/");


location /socket/ {
    proxy_pass http://127.0.0.1:3000;           
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
}


注意:

  1、location /socket/ {...}這里要格外注意!
    html中的url是 wss://www.aabb.com/socket/,所以Nginx配置中一定要是 /socket/
    如果前端是 wss://www.aabb.com/socket,Nginx對應是 /socket

  2、proxy_pass對應的最好是公網IP加端口號, 'localhost','127.0.0.1'
  3、proxy_http_version 1.1 版本號必須是1.1,這條配置必需

其他:

#服務器socket連接端口9999,為了避免沖突,這里用9990反向代理到9999,同時實現了wss轉ws,服務器端不需要做修改
server {
    listen 9990;
    server_name xx.xx.xx.xx;
    
    ssl on;
    ssl_certificate "/usr/cert/barrage.crt";
ssl_certificate_key "/usr/cert/barrage.key";
    ssl_protocols SSLv3 SSLv2 TLSv1 TLSv1.1 TLSv1.2;
ssl_session_cache shared:SSL:1m;
    ssl_session_timeout  10m;
    ssl_prefer_server_ciphers on;
    

    location /{
        #反向代理到9999端口,同時協議轉換為http,這樣服務器端代碼就不需要做修改
        proxy_pass http://120.77.222.242:9999;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        #由於服務器端源碼(建議大家做好大小寫匹配)只匹配了"Upgrade"字符串,所以如果這里填"upgrade"服務器端會將這條http請求當成普通的請求,導致websocket握手失敗
        proxy_set_header Connection "Upgrade";
        proxy_set_header Remote_addr $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_read_timeout 600s;
    }
}

假設下面四種情況分別用 http://192.168.1.1/proxy/test.html 進行訪問。

第一種:
location /proxy/ {
proxy_pass http://127.0.0.1/;
}
代理到URL:http://127.0.0.1/test.html

第二種(相對於第一種,最后少一個 / )
location /proxy/ {
proxy_pass http://127.0.0.1;
}
代理到URL:http://127.0.0.1/proxy/test.html

第三種:
location /proxy/ {
proxy_pass http://127.0.0.1/aaa/;
}
代理到URL:http://127.0.0.1/aaa/test.html

第四種(相對於第三種,最后少一個 / )
location /proxy/ {
proxy_pass http://127.0.0.1/aaa;
}
代理到URL:http://127.0.0.1/aaatest.html

來源:
https://www.jianshu.com/p/b010c9302cd0

配置證書
https://www.cnblogs.com/yyzybb/p/3840554.html
https://blog.csdn.net/cen50958/article/details/93320699

java 內存管理

https://yq.aliyun.com/articles/648040
https://blog.csdn.net/a13662080711/article/details/107061789
https://blog.csdn.net/fengyeqing5/article/details/84923273

https://www.jianshu.com/p/f2f5e6039c06
https://blog.csdn.net/weixin_30323961/article/details/95639909
https://blog.csdn.net/cxh217707/article/details/84216291

內存泄露
https://www.jianshu.com/p/43b2ecdfe005
https://www.cnblogs.com/eeexu123/p/10913389.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM