A while back, I was running two websites on the same server. One was a blog, the other a small web app. I wanted both to be easy to access, like mydomain.com for the blog and app.mydomain.com for the app. At first, I didn’t know how to make that work. Then I found out about something called a reverse proxy, and Nginx made it easy.
If you’re in a similar spot, this guide is for you.
What is a Reverse Proxy?
Let’s keep it simple.
A proxy is like a middleman. It takes a request and sends it somewhere else.
A reverse proxy is a middleman that sits in front of one or more servers. Instead of forwarding requests from your computer to the internet (like a normal proxy), it handles requests coming from the internet and passes them to your servers.
So when someone visits your site, the reverse proxy gets the request and sends it to the right place behind the scenes.
Why Use a Reverse Proxy?
There are some good reasons to use Nginx as a reverse proxy. Here are a few:
- You can run many apps or websites on the same server.
- You can use one domain or subdomain to reach different services.
- You can hide your backend server for better security.
- You can handle HTTPS (SSL) in one place.
For example, let’s say:
- Your blog runs at
http://localhost:3000
- Your app runs at
http://localhost:5000
You can use Nginx to point:
https://yourdomain.com
to the bloghttps://yourdomain.com/app
to the app
No need to expose different ports to the internet.
What You’ll Need
Before we start, make sure you have:
- A Linux server (Ubuntu or Debian work great)
- Root or sudo access
- Nginx installed
- A domain name (optional, but useful)
If you don’t have Nginx yet, don’t worry. I’ll show you how to install it next.
Step 1: Install Nginx
To install Nginx, open your terminal and run:
For Ubuntu/Debian:
sudo apt update
sudo apt install nginx
For CentOS/RHEL:
sudo yum install epel-release
sudo yum install nginx
Start Nginx:
sudo systemctl start nginx
Enable it on boot:
sudo systemctl enable nginx
Check if it’s running:
sudo systemctl status nginx
If you visit your server’s IP address in a browser, you should see a “Welcome to Nginx” page.
Step 2: Set Up Your Web App
Let’s say your web app runs on port 3000. Maybe it’s something simple like a Node.js app or even a test site. You should already be able to visit it using:
http://localhost:3000
But that’s not friendly for users. They don’t want to type ports. That’s where the reverse proxy helps.
Step 3: Create a New Nginx Configuration
Now you’ll create a config file that tells Nginx to act as a reverse proxy.
Let’s make one for a site called example.com
.
sudo nano /etc/nginx/sites-available/example.com
Add this:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
Here’s what this does:
listen 80
: listens on normal HTTPserver_name
: your domainlocation /
: defines the root pathproxy_pass
: sends traffic to your app running on port 3000- The headers help pass visitor info correctly
Save and close the file (CTRL+O
, Enter
, CTRL+X
).
Now link it to sites-enabled:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
Then test and reload Nginx:
sudo nginx -t
sudo systemctl reload nginx
If your domain is pointing to the server, it should now show the app running on port 3000 when you visit http://example.com
.
Step 4: Add HTTPS with Certbot (Optional but Recommended)
If you want your site to use HTTPS (and you should), it’s easy with Certbot.
Install it:
sudo apt install certbot python3-certbot-nginx
Then run:
sudo certbot --nginx -d example.com
Follow the prompts. Certbot will:
- Get a free SSL certificate
- Set up HTTPS for you
- Reload Nginx
After that, your site will work with https://example.com
.
When to Use Reverse Proxy
You might wonder — do I need this?
Well, ask yourself:
- Do you run more than one site or app on the same server?
- Do you want a cleaner URL for your users?
- Do you need to protect backend services from being exposed?
If you said yes to any of those, reverse proxy is probably what you need.
Benefits of Using Nginx Reverse Proxy
Here’s why I like using Nginx as a reverse proxy:
It makes things cleaner:
- No more weird ports in the URL
- All apps go through the same public face
It helps you scale:
- Add more apps later
- Set different rules for each
It’s easier to secure:
- SSL in one place
- You can block IPs, rate limit, and more
Troubleshooting Tips
If something isn’t working, try this:
- Run
sudo nginx -t
to check for config errors - Look in
/var/log/nginx/error.log
for clues - Make sure your app is actually running
- Use
curl http://localhost:3000
to test your app locally
Two Handy Lists
Nginx Reverse Proxy Advantages:
- One public domain for many apps
- Easy SSL setup
- Can hide backend servers
- Loads pages faster with caching
Things to Watch For:
- Typos in config files
- Ports not open or app not running
- SSL certs expired (use auto-renew with Certbot)
- Wrong server_name in config
Final Thoughts
Setting up a reverse proxy might seem tricky at first. But once you try it, it starts to make sense.
I’ve used it for web apps, admin panels, dashboards, and more. It keeps things neat and tidy.
If you ever want to run a second site without paying for a new server, reverse proxy is the way to go.
Have you tried Nginx reverse proxy before? What did you use it for?