gpt4 book ai didi

node.js - EC2 实例上的 Nginx 502 Bad Gateway 错误

转载 作者:太空宇宙 更新时间:2023-11-04 10:10:12 26 4
gpt4 key购买 nike

我在 EC2 Linux 实例上配置 nginx 服务器时遇到了一些问题。我在端口 3000 上运行一个应用程序,并想使用 nginx 将其映射到端口 80。

这是我的配置文件:

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
worker_connections 1024;
}

http {
log_format main '$remote-addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;
top_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_names_hash_bucket_size 128;
include /etc/nginx/mime.types;
default_type application/octet-stream;

include /etc/nginx/conf.d/*.conf;

index index.html index.htm

server {
listen 80 default_server;
[::]:80 default_server;
server_name localhost;

include /etc/nginx/default.d/*.conf;

location / {
}

error_page 404 /404.html;
location =/40x.html {
}

error_page 500 502 503 504 /50x.html;
location =/50x.html {
}
}

include /etc/nginx/sites-enabled/default;

这是 nginx 自带的默认文件,我做了很小的改动,最值得注意的是包含了一个名为 default 的自定义文件,其内容如下:

server {
listen 80;
server_name [my_domain_name];
location / {
proxy_pass http://[my_private_ip]:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}

将方括号中的项目替换为正确的值。每当我尝试导航到该网站时,我都会收到 502 Bad Gateway nginx/1.12.1。

我的服务器是一个运行在 3000 端口的 node.js 服务器。

我已尝试排除故障并阅读其他有关错误网关的 stackoverflow 问题,但我无法找到解决方案。谢谢

最佳答案

采用不同的方法。允许您的应用程序在端口 3000 上运行(并同时监听 3000)。在这种情况下,您必须将其打开为
http://url:3000

现在我们只需要将所有到达端口 80 的请求转发到 3000,这可以使用 iptables 轻松完成:

sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000  

现在您应该可以简单地使用 url 打开它,而无需端口号

关于node.js - EC2 实例上的 Nginx 502 Bad Gateway 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49545826/

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