我使用this buildpack在Heroku上使用节点+ nginx设置来提供静态文件。在静态资产正常服务的同时,尝试通过节点投放内容的结果为502 Bad Gateway
。节点本身工作正常,nginx也是如此。问题是当两者需要一起工作时,我想这是因为我没有配置好nginx upstream
设置。 这是我的nginx conf:502 Heroku中的错误网关+ 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
应显示“这是来自节点”。但是我得到了502
。
appname.herokuapp.com/static
显示“这是来自nginx”,因为它应该,所以没有nginx和静态内容的问题。
我已经尝试了nginx server
设置中upstream
的每个值的组合,但都没有工作。 我还可以尝试向节点发出nginx代理请求吗?
这里是我的Heroku Procfile
的情况下,它可以帮助:web: bin/start_nginx
我试过用nginx的端口80,但Heroku拒绝权限。这是我从你的配置中得到的:'nginx:[emerg] bind()to 0.0.0.0:80 failed(13:Permission denied)' – vjk2005
好吧,就像我说的,不幸的是我没有Heroku的使用经验,似乎你需要为Nginx使用<%= ENV ['PORT']%>。如果将nginx设置为侦听127.0.0.1:<%= ENV ['PORT']%>,并在端口4000上保留node.js进程,如同在我发布的配置中一样,它会工作吗? – grenadejumper
我厌倦了与Heroku的BS系统打交道(我联系了他们自己没有线索的支持者),并将他们迁移到数字海洋上的水滴。但是如果我以后回到Heroku,我会给这个镜头一个镜头。 – vjk2005