Back

How to Use Nginx as a Reverse Proxy on a VPS (Step-by-Step)

Articles

06/03/2026

Nginx Reverse Proxy • VPS • 2026 Tutorial

How to Use Nginx as a Reverse Proxy on a VPS (Step-by-Step)

Want to run Node.js apps, Python APIs, or multiple web services on one VPS? Nginx reverse proxy is the standard solution.
This tutorial shows you how to install Nginx, configure server blocks, proxy traffic to your app, enable HTTPS, and avoid common mistakes.

by Hamza

A reverse proxy sits in front of your application and forwards incoming requests to it. On a VPS, this is extremely useful because
many apps (Node.js, Python, Go, Java) run on ports like 3000, 5000, or 8080, while your public website
should be accessible on standard web ports 80 (HTTP) and 443 (HTTPS).

Nginx is one of the most popular reverse proxies in the world because it’s fast, stable, and simple to configure.
With the right setup, you can host multiple apps on one VPS (each on its own domain or subdomain), add SSL certificates,
and improve security without changing your application code.

What You’ll Learn

  • What Nginx reverse proxy is (and when to use it)
  • How to install Nginx on Ubuntu VPS
  • How to proxy traffic to a Node.js / Python app on a private port
  • How to serve multiple apps with domains and subdomains
  • How to enable HTTPS with Let’s Encrypt
  • Common errors (502, wrong port, firewall issues) and fixes

1) When Should You Use Nginx as a Reverse Proxy?

If your app is running on a non-standard port (example: localhost:3000), users cannot easily access it with a clean domain
like example.com. A reverse proxy solves that by receiving traffic on ports 80/443 and forwarding it internally.

✅ Use reverse proxy when

  • You run a Node/Python API on port 3000/5000
  • You want HTTPS without changing your app
  • You want multiple services on one VPS
  • You need caching, compression, or rate limiting

❌ You may not need it if

  • You only host a static site (HTML/CSS)
  • You use fully-managed hosting that already includes proxy/SSL
  • Your app already serves HTTPS and handles domains properly

2) Install Nginx on Your VPS (Ubuntu)

First, update your VPS and install Nginx. This tutorial assumes Ubuntu 22.04/24.04.

sudo apt update && sudo apt upgrade -y
sudo apt install -y nginx
sudo systemctl enable nginx
sudo systemctl start nginx

Confirm Nginx is running:

sudo systemctl status nginx

3) Configure Your App (Example: Node.js on Port 3000)

Your application should listen on a private port and on localhost (recommended), like:
127.0.0.1:3000. This keeps the app private and lets Nginx control public traffic.

Best practice: bind your app to 127.0.0.1 instead of 0.0.0.0, unless you have a reason to expose it publicly.
Nginx should be the only public entry point.

4) Create an Nginx Reverse Proxy Config (Server Block)

Nginx uses server blocks to define how it handles a domain. You’ll create a config file in
/etc/nginx/sites-available/ and enable it in sites-enabled.

A) Create the config file

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

B) Paste this example (replace domain + port)

server {
listen 80;
server_name example.com www.example.com;location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection “upgrade”;
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;
}
}

C) Enable the config

sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

5) Serve Multiple Apps (Domains and Subdomains)

One of the biggest benefits of Nginx reverse proxy is that you can run multiple services on the same VPS.
Each service can use its own domain or subdomain and point to a different internal port.

Example mapping

  • api.example.com → 127.0.0.1:4000
  • app.example.com → 127.0.0.1:3000
  • admin.example.com → 127.0.0.1:5000

DNS reminder

Don’t forget to create A records for each domain/subdomain pointing to your VPS IP.
If DNS is wrong, Nginx will never receive the traffic.

6) Enable HTTPS with Let’s Encrypt (Recommended)

Once HTTP reverse proxy works, the next step is adding HTTPS. Let’s Encrypt provides free SSL certificates via Certbot.

sudo apt install -y certbot python3-certbot-nginx
sudo certbot –nginx -d example.com -d www.example.com

Certbot can automatically update your Nginx config to use SSL and redirect HTTP → HTTPS.

7) Common Errors and Fixes (502, 404, Connection Refused)

502 Bad Gateway

Your app is not running, wrong port, or app crashed. Confirm it’s listening on 127.0.0.1:PORT and restart your app process manager.

Site not loading / DNS issues

Verify the domain points to your VPS IP (A record). DNS changes can take time to propagate.

Firewall blocking traffic

Ensure ports 80 and 443 are open: sudo ufw allow 80 and sudo ufw allow 443.

Wrong server_name

If your server_name doesn’t match the domain in DNS, Nginx may serve the default site instead of your app.

Conclusion: Reverse Proxy is the VPS “Pro Setup”

Using Nginx as a reverse proxy is one of the most important skills for hosting modern apps on a VPS. It lets you keep your application private,
route traffic to the correct service, run multiple apps on one server, and add HTTPS without rewriting your backend.

If you want a reliable VPS to run Nginx reverse proxy setups, APIs, and containers, you can explore
Retzor VPS hosting
for scalable and developer-friendly plans.

© 2026 • Nginx Reverse Proxy VPS Guide
Retzor Reviews