Ad

Deploying Node.js/React App On AWS Server, Ports Not Forwarding Properly

I'm trying to deploy a NodeJS/React app on an ec2 instance on AWS.

My app runs fine on port 3000, but is not being forwarded to port 80.

Neither modifying proxy_pass or modifying iptables seems to work in this scenario.

I've tried the following:

  1. Modifying Nginx's server configuration to forward port 3000 to port 80. My Nginx configuration:

    server {
        listen 80;
        location / {
            proxy_pass http://[My Private ec2 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;
            server_name example.com www.example.com;
        }
    }
    
  2. Modifying iptables to forward port 3000 to port 80.

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

My directory is structured as follows:

- appname/ - /api (node.js server-side code) - /client (React client-side code)

I have tried running npm start from within appname/client/ as well as npm build. sudo netstat -lntp | grep 80 shows no processes listening on port 80, so the port is available.

The app renders on [public IP]:3000. When I try to access [public IP], the browser displays 'This site can't be reached'.

This seems like a fairly straightforward thing to do, yet nginx and iptables configurations both are ignored. Am I missing something?

Ad

Answer

Ports are also needed to forward from Amazon EC2 instance's console panel. In order to enable ports from EC2 instance console panel, perform the below mentioned steps:

  1. Login to Amazon EC2 Dashboard
  2. Select your EC2 instance machine
  3. After selecting your EC2 machine, find the section Security groups in bottom panel
  4. Click on the assigned security group name, it should something like launch-wizard-{number}
  5. Then, open inbound tab from the bottom panel
  6. Click on the edit button and add your ports (80, 3000) which needs to be open in the instance machine

You can check the below URL to get more info about Amazon EC2 Port Forwarding

https://aws.amazon.com/premiumsupport/knowledge-center/connect-http-https-ec2/

Ad
source: stackoverflow.com
Ad