Skip to content

Step 2: Configure Nginx for Node.js, Python, and .NET Applications

Node.js Application

Create a configuration file for your Node.js application:

bash
sudo nano /etc/nginx/sites-available/nodeapp

sudo vim /etc/nginx/sites-available/ok8s-api

Add the following configuration:

nginx
server {
    listen 80;
    server_name your_domain_or_IP;

    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;
    }
}

Enable the configuration:

bash
sudo ln -s /etc/nginx/sites-available/nodeapp /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

Python Application (using Gunicorn)

Create a configuration file for your Python application:

bash
sudo nano /etc/nginx/sites-available/pythonapp

Add the following configuration:

nginx
server {
    listen 80;
    server_name your_domain_or_IP;

    location / {
        proxy_pass http://localhost:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Enable the configuration:

bash
sudo ln -s /etc/nginx/sites-available/pythonapp /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

.NET Application

Create a configuration file for your .NET application:

bash
sudo nano /etc/nginx/sites-available/dotnetapp

Add the following configuration:

nginx
server {
    listen 80;
    server_name your_domain_or_IP;

    location / {
        proxy_pass http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection keep-alive;
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Enable the configuration:

bash
sudo ln -s /etc/nginx/sites-available/dotnetapp /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

Step 3: Using PM2 to Manage Node.js Applications

If you are running a Node.js application, you can use PM2 to manage your application processes. To install PM2, run the following command:

bash
sudo npm install -g pm2

Check the PM2 version:

bash
pm2 --version

To start your Node.js application with PM2, navigate to your application directory and run the following command:

bash
pm2 start app.js

To view the status of your application, run:

bash
pm2 status

To restart your application, run:

bash
pm2 restart app

To stop your application, run:

bash
pm2 stop app

To remove your application from PM2, run:

bash
pm2 delete app

For more information on PM2, refer to the PM2 documentation.