4

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

.

我还有一个示例 html 文件/static来测试 nginx 是否有效:

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

.

使用此配置,appname.herokuapp.com应显示“This is from Node”。但相反,我得到了502.

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

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

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

4

2 回答 2

0

这是一个自述文件,其中包含让 nginx 和 Node.js 从我不久前创建的项目中协同工作的信息。还包括一个示例 nginx.conf。

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

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

于 2013-07-15T17:43:35.570 回答
0

I am not really familiar with Heroku, and pretty new to Nginx, but I'll give it a shot: To me it looks like the Nginx-config is saying that Nginx and the node.js app are using the same port (<%= ENV['PORT'] %>).

What you want is Nginx to listen to incoming connections (usually port 80), and have it forward them to the node.js app. Here is an example Nginx config:

# 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;
}
}

This config is working for me on a webserver I am hosting in my living-room. Good luck!

于 2013-07-15T07:18:35.170 回答