我正在尝试使用 nginx 作为 websocket nodejs 服务器的代理,但总是出现 502 错误网关。
我的域配置:
upstream server {
server 127.0.0.1:8090;
}
server {
listen 8085;
server_name server.cz server;
ssl on;
#ssl_certificate /etc/ssl/server/server.cz.pem;
ssl_certificate /etc/ssl/server/server.cz.crt;
ssl_certificate_key /etc/ssl/server/server.cz.key;
access_log /var/log/nginx/server.log;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://server;
proxy_redirect off;
#WEBSOCKET
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
我正在使用 curl 测试这个:
curl -i -N -vv -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Host: server.cz" -H "Origin: https://server.cz" -k https://127.0.0.1:8085
这将导致 502 bad gateway full 结果:http://pastebin.com/bL1CYzjt
如果我直接在 nodejs 服务器上尝试此操作,它可以正常工作。
Nginx 日志:
[error] 4083#0: *1 upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: server.cz, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8090/", host: "server.cz"
服务器日志
"GET / HTTP/1.1" 502 172 "-" "curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3"