EC2实例上的Nginx 502错误网关错误

时间:2018-03-28 23:34:27

标签: node.js linux nginx amazon-ec2

我在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问题,但我无法找出解决方案。谢谢

1 个答案:

答案 0 :(得分:1)

采用不同的方法。允许您的应用程序在端口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  

现在您应该只需使用网址打开它,而无需端口号

相关问题