简体   繁体   中英

disconnet event doesn't fire in nginx + socket.io + xhr-polling

I try to proxy node.js, socket.io and xhr-polling traffic through nginx.

Then Socket.io doesn't fire "disconnect" event.

However, bellow condition works fine.

  • Direct connection to Node.js port without nginx proxy.
  • Switch transport to websocket from xhr-polling.

My version is bellow.

  • nginx : 1.4.4
  • node.js : 0.10.24
  • socket.io : 0.9.15

My nginx config file setting is below.

server {
    listen       80;
    server_name  localhost;


   location / {
        proxy_pass http://127.0.0.1:3000/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_redirect off;

        proxy_read_timeout 300;
        proxy_connect_timeout 300;

        chunked_transfer_encoding off;
        proxy_buffering off;

        proxy_set_header    Host    $http_host;
        proxy_set_header    X-Real-IP          $remote_addr;
        proxy_set_header    X-Forwarded-Host   $host;
        proxy_set_header    X-Forwarded-Server $host;
        proxy_set_header    X-Forwarded-For    $proxy_add_x_forwarded_for;
        proxy_set_header    X-NginX-Proxy true;
   }

How can I fix this? Am I missing something? Thanks in Advance :)


In addition, I found new conditions that occurs my problem.

The result of my problem was depend on below.

  1. the client browser and mobile device.
  2. proxy throgh nginx (port 80) or direct access to node.js (port 3000).

The findings are following.

・ Chrome @ Mac OS X
・ Aquos Phone(Android Brower, Android 4.0.4)
    port 80 ⇒ NG
    port 3000 ⇒ OK

・ Gallaxy S(Android Brower, Android 4.2.2)
・ iPhone 5(Safari, iOS 7) 
    port 80 ⇒ NG
    port 3000 ⇒ NG
  • "OK" is "can fire disconnect event in realtime", "NG" is "can't fire disconnect event".
  • "port 80" uses nginx, "port 3000" uses node.js.
  • Trasport is xhr-polling only.
  • Gallaxy S and iPhone 5 are compatible with websocket.

Do you mean disconnect event when user closes the browser or tab? If so, try this code on the client side when you connecting to server:

io.connect('/', {
  'sync disconnect on unload':  true
});

(The first argument '/' may be different in your configuration)

Here you can find details.

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