4

このビルドパックを使用して、Heroku でノード + nginx セットアップで静的ファイルを提供しています。静的アセットは適切に提供されますが、ノードを介してコンテンツを提供しようとすると、502 Bad Gateway. ノード自体は正常に動作し、nginx も同様です。問題は、nginx のupstream設定を正しく構成していないために、2 つが連携する必要がある場合です。これが私のnginx confです:

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 )

.

/staticnginxが機能するかどうかをテストするためのサンプルhtmlファイルもあります。

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

.

この構成appname.herokuapp.comでは、「This is from Node.」と表示されるはずです。しかし、代わりに私は502.

appname.herokuapp.com/static「これはnginxからのものです」と表示されるので、nginxと静的コンテンツに問題はありません。

upstreamnginx設定で値のすべての組み合わせを試しましserverたが、どれも機能しませんでした。ノードへのnginxプロキシリクエストを作成するには、他に何ができますか?

Procfile役立つ場合に備えて、これが私の Herokuです。web: bin/start_nginx

4

2 に答える 2

0

私は Heroku にあまり詳しくなく、Nginx にもかなり慣れていませんが、試してみます: Nginx-config は、Nginx と node.js アプリが同じポートを使用していると言っているようです (<% = ENV['PORT'] %>)。

必要なのは、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;
}
}

この構成は、リビングルームでホストしている Web サーバーで機能しています。幸運を!

于 2013-07-15T07:18:35.170 に答える