【问题标题】:502 Bad Gateway in node + nginx proxy setup on Heroku502 Bad Gateway in node + Heroku 上的 nginx 代理设置
【发布时间】:2013-07-04 23:26:01
【问题描述】:

我正在使用this buildpack 在 Heroku 上使用节点 + nginx 设置提供静态文件。虽然静态资产提供正确,但尝试通过节点提供内容会导致502 Bad Gateway。 Node 本身工作正常,nginx 也是如此。问题是当两者需要一起工作时,我猜是因为我没有正确配置 nginx upstream 设置。 这是我的 nginx 配置文件:

worker_processes                1;
error_log                       /app/nginx/logs/error.log;
daemon                          off;

events {
    worker_connections          1024;
}


http {
    default_type                application/octet-stream;
    sendfile                    on;
    keepalive_timeout           65;
    gzip                        on;

    upstream node_conf {
          server                127.0.0.1:<%= ENV['PORT'] %>;
          keepalive             64;
        }

    server {
        listen                  <%= ENV['PORT'] %>;
        server_name             localhost;

        location / {
            root                html;
            index               index.html index.htm;
            proxy_redirect      off;
            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_set_header    Connection          "";
            proxy_http_version  1.1;
            proxy_pass          http://node_conf;
        }

        location ~* ^.+\.(jpg|gif|png|ico|css|js|html|htm)$ {
            root                /app;
            access_log          off;
            expires             max;
        }

        location /static {
            root                /app;
            index               index.html index.htm;
        }
    }
}

.

我的_static.cfg

SERVER_TYPE="nginx"
BUILD_WEB_ASSETS="true"

.

我的节点服务器:

var app = require( 'express ')()
app.get( '/', function(req, res) { res.send( 'This is from Node.' ) })
app.listen( process.env.PORT )

.

我在/static 也有一个示例 html 文件来测试 nginx 是否工作:

<html>This is from nginx.</html>

.

使用此配置,appname.herokuapp.com 应显示“这是来自节点”。但是我得到了502

appname.herokuapp.com/static 应该显示“This is from nginx”,所以 nginx 和静态内容没有问题。

我已经在 nginx server 设置中尝试了 upstream 的所有值组合,但没有一个有效。 我还能尝试向节点发出 nginx 代理请求吗?

这是我的 Heroku Procfile,以防万一:web: bin/start_nginx

【问题讨论】:

    标签: node.js heroku nginx


    【解决方案1】:

    我对 Heroku 不是很熟悉,对 Nginx 也很陌生,但我会试一试:在我看来,Nginx-config 似乎在说 Nginx 和 node.js 应用程序使用相同的端口()。

    你想要的是 Nginx 监听传入的连接(通常是端口 80),并将它们转发到 node.js 应用程序。这是一个 Nginx 配置示例:

    # the IP(s) on which your node server is running. I chose port 4000.
    upstream xxx.xxx.xxx.xxx { #Your IP adress as seen from the internet
    server 127.0.0.1:4000; #Your local node.js process
    }
    # the nginx server instance
    server {
    listen 0.0.0.0:80; #Have Nginx listen for all incoming connections on port 80
    server_name my-site;
    access_log /var/log/nginx/my-site.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://xxx.xxx.xxx.xxx/; #Your IP adress as seen from the internet
    proxy_redirect off;
    }
    }
    

    此配置适用于我在客厅中托管的网络服务器。祝你好运!

    【讨论】:

    • 我尝试过为 nginx 使用端口 80,但 Heroku 拒绝许可。这是我从你的配置中得到的:nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)
    • 好吧,就像我说的,很遗憾我没有任何使用 Heroku 的经验,但似乎你需要为 Nginx 使用 。如果您将 nginx 设置为侦听 127.0.0.1:,并按照我发布的配置将 node.js 进程保持在端口 4000 上,它是否有效?
    • 我厌倦了与 Heroku 的 BS 系统打交道(我联系了他们自己不知道的支持)并迁移到 Digital Ocean 上的一个 droplet。但如果我稍后真的回到 Heroku,我会试一试。
    【解决方案2】:

    Here's a README 提供了让 nginx 和 Node.js 从我不久前创建的项目中协同工作的信息。还包括一个示例 nginx.conf。

    作为一个概述,基本上你只是用 Node 创建套接字,然后设置 nginx 来管道这些上游。当我想运行多个 Node 进程并让 nginx 站在它前面时,我通常会使用它。

    它还包括开箱即用的 socket.io,因此您也可以使用它们来查看如何配置您的 Node 实例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-29
      • 2017-12-30
      • 2014-10-17
      • 2014-04-30
      • 2016-01-04
      • 1970-01-01
      • 2018-04-15
      • 2018-05-23
      相关资源
      最近更新 更多