【问题标题】:nginx + node.js = failed while connecting to upstream with iframelynginx + node.js = 使用 iframely 连接到上游时失败
【发布时间】:2016-01-29 13:16:46
【问题描述】:

我尝试使用 iframely。我在我的服务器 ubuntu + nginx 上安装了自托管版本: https://iframely.com/docs/host

当我像这样启动节点时: # node server iframely 运行良好 否则,我会收到 502 bad gateway 错误。

错误

在日志错误中:

2016/01/25 06:06:58 [error] 13265#0: *4476 connect() failed (111: Connection refused) while connecting to upstream, client: xx:xx:xx:xx:xx:xx, server: iframely.custom.com, request: "GET /iframely?url=http://coub.com/view/2pc24rpb HTTP/1.1", upstream: "http://127.0.0.1:8061/iframely?url=http://coub.com/view/2pc24rpb", host: "iframely.custom.com"

当我尝试时: # curl -i 127.0.0.1:8061/iframely?url=http://coub.com/view/2pc24rpb

它确认错误: curl: (7) Failed to connect to 127.0.0.1 port 8061: Connection refused

我从 node 开始,我知道 node.js 可能没有在端口 8061 上侦听。

当我尝试时: netstat -pantu

我没有看到有问题的端口,但其他类似的端口被另一个 node.js 应用程序使用,它运行良好:

tcp6 0 0 127.0.0.1:4567 127.0.0.1:60724 ESTABLISHED 12329/node

配置

我的主机配置:

upstream iframely.custom.com {
        ip_hash;
        server localhost:8061;
        keepalive 8;
}
server {

        listen 80;
        listen [::]:80;
        server_name iframely.custom.com;
        root /usr/share/nginx/html/iframely.custom.com;

        # Logs
        access_log /var/log/iframely.access_log;
        error_log /var/log/iframely.error_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://iframely.custom.com/;
                proxy_redirect off;

                # Socket.IO Support
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
        }

        # Exclude from the logs to avoid bloating when it's not available
        include drop.conf;

}

我已尝试更改 127.0.0.1 的配置 localhost,但没有任何改变。

如何让 node.js 应用程序保持活力:我必须永远重启它吗? 会不会是 ipv4 或 ipv6 的问题?

我在 serverfault 上发布了这个问题,因为我认为这是 nginx 配置的问题。但是有人说我错了。

提前感谢您的帮助。 jb

【问题讨论】:

    标签: node.js nginx


    【解决方案1】:

    首先,你应该让节点应用程序监听8061端口,它应该显示在“netstat -tpln”中,例如:

    tcp        0      0 127.0.0.1:8061          0.0.0.0:*               LISTEN      21151/node
    

    其次,您应该使用 curl 对其进行测试。如果得到响应,则节点服务器可以正常工作。

    最后,将焦点转移到 nginx。

    【讨论】:

    • 感谢您的测试。最后我解决了这个问题,你可以看到。
    【解决方案2】:

    只有一个后端,使用upstream 模块没有任何好处。您可以删除您的 upstream 部分并像这样更新您的 proxy_pass 行:

        proxy_pass http://127.0.0.1:8061/;
    

    也有可能后端正在侦听 IP,但没有响应名称“localhost”。这不太可能,但可能。但它必须监听某个IP地址,所以使用IP地址更安全。

    Vladislav 的上述建议也很好。

    【讨论】:

    • 谢谢你的回答,其实我用的是多节点应用。关于我的问题,upstream 似乎终于解决了。
    【解决方案3】:

    我用永远解决这个问题:https://github.com/foreverjs/forever

    【讨论】:

      猜你喜欢
      • 2014-02-26
      • 2012-11-21
      • 2020-03-13
      • 2021-06-22
      • 1970-01-01
      • 2013-11-03
      • 2019-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多