1

I've been having some trouble configuring an nginx server on a EC2 Linux instance. I'm running an application on port 3000 and want to map that to port 80 using nginx.

Here is my configuration file:

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;

This is the default file that comes with nginx with very slight changes by me, most notably the inclusion of a custom file called default, whose contents are as follows:

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

With the items in square brackets replaced with the correct values. Whenever I try to navigate to the website I get 502 Bad Gateway nginx/1.12.1.

My server is a node.js server running on port 3000.

I've tried troubleshooting and reading other stackoverflow questions about bad gateways but I can't figure out the solution. Thank you

Em Eldar
  • 686
  • 1
  • 8
  • 25

2 Answers2

2

Follow a different approach. Allow your application to run on port 3000 (and listen on 3000 as well). In this case, you would then have to open it as
http://url:3000

Now we just need to forward all requests coming to port 80 to 3000 which can be easily done using iptables:

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

Now you should be simply able to open it with the url, without the port number

Anshul Verma
  • 1,065
  • 1
  • 9
  • 26
  • Thanks, @anushul it works for me, my node + express Rest API running on 4000, I have setup Nginx, added security group entry for 8080, updated the default file, although it's raising the same error, I tried with the above command, Its worked. Thanks – Bhagvat Lande Feb 06 '21 at 15:46
0

SELinux was the root cause of the problem for me. Persistently allowing network http traffic solved my problem:

$ sudo setsebool -P httpd_can_network_connect 1

More about this solution: (13: Permission denied) while connecting to upstream:[nginx]

seth
  • 63
  • 1
  • 2
  • 8