【问题标题】:socket io with node js is not connecting through nginx proxy_pass带有节点 js 的套接字 io 未通过 nginx proxy_pass 连接
【发布时间】:2015-06-04 10:01:27
【问题描述】:

问题是当我使用 nginx 时,套接字没有建立。任何人都可以帮助我按照步骤将 Socket oi 与 nginx 集成。 我试过这个

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


            }

【问题讨论】:

    标签: node.js nginx socket.io


    【解决方案1】:

    首先检查你的 nginx 版本。根据 v1.3.13 之后支持的页面 websockets;

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

    然后将您的 nginx conf 与 nginx 博客中所述的以下配置进行比较;

    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/

    同时检查您为 socket.io 服务器选择的端口的防火墙配置。 (我读到一些 ISP 正在阻止 80 和 443 以外的端口的 websocket 连接,还请检查服务器是否使用 tcpdump 等接收数据包。)

    如果到目前为止一切正常,请检查您的 nginx 错误日志 (/var/log/nginx/error.log) 以查看是否有任何与 socket.io 相关的错误消息。您可以将其粘贴到此处进行进一步分析。

    然后,如果 nginx 日志中没有套接字错误,则使用 DEBUG 模式启动您的节点应用程序,如下所示;

    DEBUG=* node yourfile.js
    

    并检查是否有任何套接字连接消息打印到控制台。您也可以粘贴它以进行进一步分析。

    【讨论】:

      猜你喜欢
      • 2022-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-30
      • 2020-09-25
      • 2015-09-12
      • 2011-06-25
      • 2017-02-04
      相关资源
      最近更新 更多