Nginx 配置反向代理 WebSocket

Nginx 反向代理 WebSocket 服务时需要设置 Upgrade 和 Connection 的 header。

nginx 配置
出现报错时的配置

location /ws {
    proxy_set_header  Host $host;
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header  X-Forwarded-Proto   $scheme;
    proxy_pass        http://127.0.0.1:9000/ws;
}    

反向代理成功的配置

location /ws {
    proxy_set_header  Host $host;
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header  X-Forwarded-Proto   $scheme;
    proxy_pass        http://127.0.0.1:9000/ws;
    
    # 关键配置 start
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    # 关键配置 end
}            

你可能感兴趣的:(nginx,nginx,websocket,运维)