首先,在 nginx.conf 的 http{ } 段添加:

 map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

然后,在 server{ } 段添加:

location /wss/  {
    proxy_pass http://127.0.0.1:9501/;
    proxy_http_version 1.1;    
    proxy_connect_timeout 4s;
    proxy_read_timeout 60s;
    proxy_send_timeout 60s;
    proxy_set_header Upgrade $http_upgrade;    
    proxy_set_header Connection "Upgrade";    
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

访问 http://yourdomain.com/wss/ 时,请求会被转发给 http://127.0.0.1:9501/

配置项说明:
proxy_read_timeout:nginx接收upstream server数据超时, 默认60s, 如果连续的60s内没有收到1个字节, 连接关闭。
proxy_send_timeout:nginx发送数据至upstream server超时, 默认60s, 如果连续的60s内没有发送1个字节, 连接关闭。