【发布时间】: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
【问题讨论】: