Serve app on port 80 using Nginx

Tags:
Date:

Friday 22/07/2022

This example was tested on ubuntu machine but should work the same on any linux distribution.

First of all you need to install nginx:

sudo apt-get install nginx

Then we need to remove the default nginx configuration file:

sudo rm /etc/nginx/sites-enabled/default

Create new nginx configuration file (I will name it myApp):

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

This is myApp file content:

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_pass         "http://127.0.0.1:3000";
    }
}

Make sure to change server_name example.com and the proxy_pass 3000 port to your settings.

Basically this configuration will make nginx act as a reverse proxy and will forward all the requests from port 80 to port 3000.

Then we will create a symlink between sites-available and sites-enabled.

sudo ln -s /etc/nginx/sites-available/myApp /etc/nginx/sites-enabled/myApp

And finally we will restart our service and everything should work:

sudo service nginx restart

© 2023 Vadim Alakhverdov