I'm trying to use nginx as proxy for websocket nodejs server, but I'm always getting 502 bad gateway.

my domain config:

upstream server {

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;

  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection "upgrade";

I'm testing this with curl:

curl -i -N -vv -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Host: server.cz" -H "Origin: https://server.cz" -k

this will result with 502 bad gateway full result: http://pastebin.com/bL1CYzjt

If I try this directly to nodejs server it works fine.

Nginx log:

[error] 4083#0: *1 upstream prematurely closed connection while reading response header from upstream, client:, server: server.cz, request: "GET / HTTP/1.1", upstream: "", 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/ libidn/1.23 librtmp/2.3"
  • What is the nodejs server supposed to return? It's nginx not recognizing the return values so it tosses the 502 error. – Nathan C Apr 7 '14 at 12:28
  • I found out, my bad. In nginx configuration was another domain binded to same port without ssl so https wasn't working. – zajca Apr 7 '14 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.