使用 nginx 作为 websocket 连接的代理

Kan*_*rat 2 nginx websocket

如何使用 nginx 作为 websocket 的代理?如果我需要从 clientSite.com( javascript) 连接到 socketSite.com:port 并且我不会显示用户的链接“socketSite.com:port”

我可以使用 nginx 代理重定向来自/到 websocket 服务器的请求吗?

小智 6

绝对可以!使用以下配置:

location /myHandler{
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header HOST $host;
    proxy_set_header X_Forwarded_For $remote_addr;
    proxy_pass http://localhost:8880;
    proxy_redirect default;
    client_max_body_size 1000m;
} 
Run Code Online (Sandbox Code Playgroud)

我使用弹簧 websocket。/myHandler是我创建 websocket 连接的 URL,http://localhost:8880;是我的 Tomcat 服务器地址。Nginx 服务器和 Tomcat 运行在同一台机器上。