Logo

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

Dec 02, 2023

To Set Up Nginx as a Reverse Proxy Server for PM2 on Ubuntu 23.04, follow the steps below.

Step 1 : Install Nginx

Begin by installing Nginx on your Ubuntu 23.04 system using the following command:

sudo apt update
sudo apt install nginx

Step 2 : Create a New Configuration for the App

Create a new configuration file for your app (replace "example.com" with your actual domain):

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

Inside the file, configure the reverse proxy settings. Here's a basic example:

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://127.0.0.1:3000;  # Adjust the port based on your PM2 configuration
        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;
    }
}

Save and exit the text editor.

Step 3 : Enable the Configuration

Create a symbolic link to enable the Nginx configuration:

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

Step 4 : Test the Configuration

Ensure there are no syntax errors in your Nginx configuration:

sudo nginx -t

Step 5 : Restart Nginx

Restart Nginx to apply the changes:

sudo systemctl restart nginx

Step 6 : Check the Results

Visit your domain (e.g., http://example.com) in a web browser to verify that Nginx is successfully acting as a reverse proxy for your PM2 app.

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

Recommended