简体   繁体   中英

socket io with node js is not connecting through nginx proxy_pass

The issue is when i use nginx the socket is not establishing.Any one help me in steps to follow to integrate Socket oi with nginx. i tried this

location /field  {
          # the following is required for WebSockets
        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;

        # supposedly prevents 502 bad gateway error;
        # ultimately not necessary in my case
        proxy_buffers 8 32k;
        proxy_buffer_size 64k;

        # the following is required
        proxy_pass 
          proxy_redirect off;

        # the following is required as well for WebSockets
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        tcp_nodelay on; # not necessary


            }

First of all check your nginx version. According to that page websockets supported after v1.3.13;

http://nginx.com/blog/nginx-nodejs-websockets-socketio/

Then compare your nginx conf with the below configuration as stated in the nginx blog;

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

upstream websocket {
    server 192.168.100.10:8010;
}

server {
    listen 8020;
    location / {
        proxy_pass http://websocket;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
    }
}

http://nginx.com/blog/websocket-nginx/

Also check your firewall configuration for the port you chose for socket.io server. (I've read that some ISPs are blocking websocket connection for ports other than 80 & 443, please also check if server receive packets using tcpdump etc.)

If everyhing is okay up until now, check your nginx error logs (/var/log/nginx/error.log) to see if there are any socket.io related error messages. You can paste it here for further analysis.

Then if there is no socket error in nginx logs start your node app with DEBUG mode on as below;

DEBUG=* node yourfile.js

And check if any socket connection message is printed to console. You can also paste it for further analysis.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM