このビルドパックを使用して、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 )
.
/static
nginxが機能するかどうかをテストするためのサンプルhtmlファイルもあります。
<html>This is from nginx.</html>
.
この構成appname.herokuapp.com
では、「This is from Node.」と表示されるはずです。しかし、代わりに私は502
.
appname.herokuapp.com/static
「これはnginxからのものです」と表示されるので、nginxと静的コンテンツに問題はありません。
upstream
nginx設定で値のすべての組み合わせを試しましserver
たが、どれも機能しませんでした。ノードへのnginxプロキシリクエストを作成するには、他に何ができますか?
Procfile
役立つ場合に備えて、これが私の Herokuです。web: bin/start_nginx