网站添加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