nginx环境下,socket.io连接https
项目中有个聊天功能,之前一直请求的 http://ip:端口 的格式,但是升级https之后,就报错了(https请求http服务器路径报错),,,于是找了很多资料配置服务器
1、我使用的是宝塔面板,因此conf 文件
在 pannel文件夹
下面
/www/server/panel/vhost/nginx
找到对应域名下的 conf文件, 在server模块下加入一个location配置:
location ^~/socket.io {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://ip:端口;
}
注意:找的资料里面,有的写得如下:(不适用当前情况,socket.io路径不对,服务器会报错 404找不到socket.io模块)
location /socket.io/ {
proxy_pass http://ip:端口;
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;
proxy_set_header Host $host;
}
web端配置的链接socket路径就变成了: https://xx.xx.com , 连接成功
注意事项:server中添加location的时候,一定要引入对应https的加密文件,否则https不起效果(宝塔面板在配置https的时候会自动引入,这里只是说明一种https不起效果的情况)
,
ssl_certificate /www/server/panel/vhost/cert/xx.xxx.com/fullchain.pem;
ssl_certificate_key /www/server/panel/vhost/cert/xx.xxx.com/privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
ssl_ciphers ECxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxHE;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
error_page 497 https://$host$request_uri;