Logo

How to Set Up Nginx as a Reverse Proxy Server for PM2 on Ubuntu 24.04

Mar 17, 2024

To set up Nginx as a reverse proxy server for PM2 on Ubuntu 24.04, follow the steps below.

Step 1 : Install Nginx using the following command:

sudo apt update
sudo apt install nginx

Step 2 : Create a new configuration file for your app. For example:

sudo nano /etc/nginx/sites-available/example.com

Add the following configuration, replacing example.com and 3000 with your domain and PM2 app port respectively:

server {
    listen 80;
    server_name 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;
    }
}

Step 3 : Create a symbolic link to enable the new configuration:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

Step 4 : Check the syntax of the Nginx configuration:

sudo nginx -t

Step 5 : Restart Nginx to apply the changes:

sudo systemctl restart nginx

Step 6 : Open your web browser and visit your domain (e.g., http://example.com). You should see your PM2 app running through Nginx.

Congratulations! You have successfully set up Nginx as a reverse proxy server for PM2 on Ubuntu 24.04.

Recommended