skip to Main Content

Trying to setup a staging environment on Amazon LINUX EC2 instance and migrate from Heroku.
My repository has two folders:

  • Web
  • API

Our frontend and backend are running on the same port in deployment

In dev, these are run on separate ports and all requests from WEB and proxied to API
(for ex. WEB runs on PORT 3000 and API runs on PORT 3001. Have a proxy set up in the package.json file in WEB/)

Currently the application deployment works like this:

  • Build Web/ for distribution
  • Copy build/ to API folder
  • Deploy to Heroku with web npm start

In prod, we only deploy API folder with the WEB build/

Current nginx.conf looks like this
Commented out all other attempts

Also using PM2 to run the thread like so
$ sudo pm2 bin/www
Current thread running like so:
pm2 log
This is running on PORT 3000 on the EC2 instance

Going to the public IPv4 DNS for instance brings me to the login, which it’s getting from the /build folder but none of the login methods (or any API calls) are working.
502 response example

I have tried a lot of different configurations. Set up the proxy_pass to port 3000 since thats where the Node process is running.
The only response codes I get are 405 Not Allowed and 502 Bad Gateway

Please let me know if there is any other information I can provide to find the solution.

2

Answers


  1. Chosen as BEST ANSWER

    Turns out there was an issue with express-sessions being stored in Postgres. This led me to retest the connection strings and I found out that I kept receiving the following error:

    connect ECONNREFUSED 127.0.0.1:5432

    I did have a .env file holding the env variables and they were not being read by pm2. So I added this line to app.js:

    const path = require("path");
    require('dotenv').config({ path: path.join(__dirname, '.env') });
    

    then restarted the app with pm2 with the following command:

    $ pm2 restart /bin/www --update-env


  2. It looks like you don’t have an upstream block in your configuration. Looks like you’re trying to use proxy-pass to send to a named server and port instead of a defined upstream. There’s is an example on this page that shows how you define the upstream and then send traffic to it. https://nginx.org/en/docs/http/ngx_http_upstream_module.html

        server backend1.example.com       weight=5;
        server backend2.example.com:8080;
        server unix:/tmp/backend3;
        server backup1.example.com:8080   backup;
        server backup2.example.com:8080   backup;
    }
    server {
        location / {
            proxy_pass http://backend;
        }
    }````
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search