gpt4 book ai didi

node.js - Node 中的 502 Bad Gateway + Heroku 上的 nginx 代理设置

转载 作者:搜寻专家 更新时间:2023-11-01 00:08:44 26 4
gpt4 key购买 nike

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

.

我的 Node 服务器:

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 应该显示“This is from Node”。但我得到的是 502

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

我已经在 nginx server 设置中尝试了 upstream 值的所有组合,但都没有奏效。 我还可以尝试向 Node 发出 nginx 代理请求吗?

这是我的 Heroku Procfile 以防有帮助:web: bin/start_nginx

最佳答案

我对 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;
}
}

此配置适用于我在客厅托管的网络服务器。祝你好运!

关于node.js - Node 中的 502 Bad Gateway + Heroku 上的 nginx 代理设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17512932/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com