0

I was able to setup DNS forwarding from my domain registered with NameCheap to my Node.js site hosted on an AWS EC2 instance. However, I have to add the :3000 port number onto the URL to view my site. Obviously I prefer to not have to append the port number so I started to research this and it looks like I need to setup a reverse proxy with NGINX on my AWS server. I have a couple of questions that I would like to have peoples' feedback on.

  1. Is using NGINX as a reverse proxy on my AWS EC2 server the best solution?
  2. Should I do the same in my local environment so that all the configuration is ready when I pull from Git or, should I have NGINX only on my AWS environment and run it differently for production? I use Windows for local development and the NGINX setup seems to be more for Linux.
Casivio
  • 333
  • 7
  • 15
  • Why cant you configure your NodeJS app to listen on default ports (443/80) instead of port 3000? – vjgn Jun 04 '21 at 23:45
  • Another options is to use ALB and Target Group to do this. Refer to the solution suggested in this question: https://stackoverflow.com/questions/42429728/how-to-forward-port-in-aws-application-load-balancer-alb-port-forwarding – vjgn Jun 04 '21 at 23:52
  • @vjgn, it is my understanding that would make the site vulnerable to attack since those ports have root access. I do not understand that issue well but I was trying to avoid it based on all the comments I saw – Casivio Jun 07 '21 at 19:27

1 Answers1

0

Most common applications are able to run as web server on their own, but the Nginx webserver is able to provide following main benefits from these features:

  • Load Balancing
  • Increased Security
  • Better Performance
  • Easy Logging and Auditing
  • Encrypted Connection

If you already have installed nginx and nodejs in your server, you can configure your reverse proxy like this:

Open the "default" config file:

sudo nano /etc/nginx/sites-available/default

Write appropriate configs there:

server {
        # optional
        client_max_body_size 30M;
        listen 80 default_server;
        listen [::]:80 default_server;

        # some static directory path
        root /var/www/html;

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

        # You can leave this _
        # server_name _;
        # but if you have already connected your domain with your server:
        server_name example.com www.example.com;

        location / {
                proxy_pass http://localhost: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;
        }
}

Make sure you've successfully tested your configs after last changes:

sudo nginx -t

Restart Nginx

sudo service nginx restart
# or
sudo systemctl restart nginx

Run your app and check the website. Hope it will work, if any issues, let me know with your comment.

About the local environment: no need to do anything. You can work on your project as you did before. Nothing to do for local. Just connect that to the remote git origin and restart your node-server after any changes. It's a good practice to connect your server project with your remote origin via CI/CD pipelines.

boolfalse
  • 1,892
  • 2
  • 13
  • 14