Home > Mobile >  NGINX net::ERR_CONNECTION_REFUSED with PM2 ReactJS Frontend and Express Backend in an AWS
NGINX net::ERR_CONNECTION_REFUSED with PM2 ReactJS Frontend and Express Backend in an AWS

Time:10-16

I have running in an Amazon Web Server a ReactJS Frontend connecting to an Express APIREST. With PM2 to launch both processes and NGINX.

The problem comes when I try to log-in to the app in chrome, the frontend is okay but it refuses to connect to the APIREST. Error Image

These are the files I have:

APIREST .env

DB_CCT=my_db
DB_CCT_USER=user
DB_CCT_PASS=password
DB_CCT_HOST=127.0.0.1
NODE_ENV=development
PORT=8017
JWT_SECRET=jotauvedoblete

FRONTEND ReactJS .env

I also tried to add the public IP instead of localhost or 127.0.0.1 but doesn't work either, it shows the connection timed out.

REACT_APP_DB=http://localhost:8017/api/v1/

PM2 ecosystem.config.js

module.exports = {
  apps: [{
    name: 'CCT_REACT',
    cwd: 'cct-web-client/',
    script: 'npm',
 
    // Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
    args: 'run start:production',
    instances: 1,
    autorestart: true,
    watch: false,
    max_memory_restart: '1G',
    env: {
      NODE_ENV: 'production',
      PORT: 5001
    },
    env_production: {
      NODE_ENV: 'production',
      PORT: 5001
    }
  },
  {
    name: 'CCT_API_REST',
    cwd: 'cct_api_rest/',
    script: 'npm',
 
    // Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
    args: 'run start',
    instances: 1,
    autorestart: true,
    watch: false,
    max_memory_restart: '1G',
    env: {
      NODE_ENV: 'production',
      PORT: 8017
    },
    env_production: {
      NODE_ENV: 'production',
      PORT: 8017
    }
  }],
 
  deploy: {
    production: {
    }
  }
}

NGINX service

server {

        listen 80 default_server;
        listen [::]:80 default_server;

        root /home/ubuntu/CCT/cct-web-client/build;

        index index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                try_files $uri/ /index.html?q=$uri&$args;
        }

}

CodePudding user response:

If you are getting a connection refused, which means that the requests are getting blocked.

I see that you have specified PORT 8017 for your API App, check 2 things -

  1. Check in your AWS Security Groups of EC2 Inbound rules for PORT 8017 is open for all.
  2. Also try to Nginx restart once - `sudo systemctl restart Nginx.
  3. For your API, just start your API without pm2 & add a console.log in your route & middleware & make a POST request from POSTMAN to check if your app is working the right way.

CodePudding user response:

Allow connect to port 80 on your aws and remove all other and use following nginx config. Also set api backend url in your frontend to your domain or ip on port 80.

server {

    listen 80 default_server;
    listen [::]:80 default_server;

    root /home/ubuntu/CCT/cct-web-client/build;

    index index.html index.htm index.nginx-debian.html;

    server_name _;

    # Frontend
    location / {
        try_files $uri /index.html;
    }

    # node api reverse proxy
    location /api/ {
        proxy_pass http://localhost:8017/;
    }

}

Also make sure to check first with any api client if you can consume your api without frontend. It should not be timedout.

  • Related