Step 2: Configure Nginx for Node.js, Python, and .NET Applications
Node.js Application
Create a configuration file for your Node.js application:
sudo nano /etc/nginx/sites-available/nodeapp
sudo vim /etc/nginx/sites-available/ok8s-apiAdd the following configuration:
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:
sudo ln -s /etc/nginx/sites-available/nodeapp /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginxPython Application (using Gunicorn)
Create a configuration file for your Python application:
sudo nano /etc/nginx/sites-available/pythonappAdd the following configuration:
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:
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:
sudo nano /etc/nginx/sites-available/dotnetappAdd the following configuration:
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:
sudo ln -s /etc/nginx/sites-available/dotnetapp /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginxStep 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:
sudo npm install -g pm2Check the PM2 version:
pm2 --versionTo start your Node.js application with PM2, navigate to your application directory and run the following command:
pm2 start app.jsTo view the status of your application, run:
pm2 statusTo restart your application, run:
pm2 restart appTo stop your application, run:
pm2 stop appTo remove your application from PM2, run:
pm2 delete appFor more information on PM2, refer to the PM2 documentation.