通过nginx反向代理实现https网站连接ws服务

在Nginx配置文件中增加以下内容:

location /websocket {
    proxy_pass http://127.0.0.1:1234;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
    proxy_set_header Access-Control-Allow-Origin "*";
}

可以实现对"/websocket"这个路径的反向代理

1234是你的ws服务端口号,/websocket可以改为任何你需要的路径。

 

前端连接:

ws = new WebSocket("wss://testdomain.com/websocket");

使用你的域名+你配置的代理路径如“/websocket”,就可以连接到服务了。

posted @ 2025-06-09 11:06  何效名  阅读(133)  评论(0)    收藏  举报