Introduction
In this lab, you will learn how to configure Nginx as a reverse proxy.
Think of a Reverse Proxy like a receptionist at a large company. When you call the main number, you don't speak directly to the CEO or the support team. You speak to the receptionist, who asks "How can I help you?" and then transfers your call to the right person.
In this scenario:
- Nginx is the receptionist (listening on port 80).
- Python App is the "Support Team" (listening on port 8000).
- You are the client calling in.
You will set up Nginx so that when someone asks for /app, Nginx will forward the request to a Python server and return the response to the client.
Reverse proxies are central to modern web architecture. They sit in front of application servers to provide a stable public entry point, and they can also handle TLS termination, load balancing, caching, and request routing.
This means Nginx does not always serve files itself. Sometimes its main job is to receive client traffic and pass that traffic to another service behind it.
Start a Backend Application
First, we need something to "proxy" requests to. We will create a very simple web application using Python.
We will create a specific folder for our app to serve files from.
Create a folder named app inside your project directory and create a simple HTML file inside it:
mkdir -p ~/project/app
echo "Hello from the Python Backend." > ~/project/app/index.html
Now, let's start the Python server. This command tells Python to act as a web server, serving files from the current directory on port 8000.
This Python process is the backend application server in our example. It will handle the actual content generation, while Nginx will sit in front as the public-facing entry point.
We add the & symbol at the end to run it in the background. This allows you to keep using your terminal while the server runs silently.
cd ~/project
python3 -m http.server 8000 &
Let's verify it started correctly. We look for a process listening on port 8000:
netstat -tuln | grep 8000
You should see an output showing 0.0.0.0:8000 (LISTEN). This means our "Backend App" is ready to take calls on extension 8000.
Configure Nginx Reverse Proxy
In this step, you will modify the Nginx configuration file to tell it about our Python app.
We need to edit the default configuration file using sudo and the nano text editor.
sudo nano /etc/nginx/sites-enabled/default
Scroll down inside the server { ... } block. You will see an existing location / { ... } block. We need to add a new location /app block below it.
Add the following code into the file (be careful with the braces { } and the semicolon ;):
location /app {
proxy_pass http://127.0.0.1:8000;
}
What does this do?
location /app: Tells Nginx to watch for requests starting with/app.proxy_pass ...: Acts like the call transfer button. It tells Nginx: "When you get a request for/app, forward it tolocalhost(127.0.0.1) on port 8000."
That forwarding is the essence of a reverse proxy. The client talks to Nginx, but Nginx becomes a client of the backend server on the user's behalf.
Your file structure should look similar to this:
server {
listen 80 default_server;
...
location / {
try_files $uri $uri/ =404;
}
location /app {
proxy_pass http://127.0.0.1:8000;
}
}
Press Ctrl+O then Enter to save the file, and Ctrl+X to exit nano.
Validate and Reload
We have updated the configuration file, but Nginx is still using the old rules. We need to reload the service.
First, always test your configuration for typos before reloading. If you reload a broken config, the web server might stop working entirely.
Run the test command:
sudo nginx -t
You should see: syntax is ok
test is successful
If the test passes, reload Nginx to apply your changes:
sudo service nginx reload
Nginx has now updated its instructions and is ready to forward requests.
At this point, Nginx is acting as a router between URLs and backend services, not just as a file server reading from disk.
Test the Proxy
In this final step, we will verify that our "Receptionist" is doing their job properly.
We will send a web request to Nginx (Port 80) asking for /app/. Nginx should instantly forward this to the Python server (Port 8000).
Run the following command:
curl http://localhost/app/
You should see the message from our backend server:
Hello from the Python Backend.
What just happened?
- You asked Nginx (
localhost:80) for/app/. - Nginx checked its rules, matched
/app, and followed theproxy_passinstruction. - Nginx called the Python Server on port 8000.
- Python Server returned the "Hello..." page to Nginx.
- Nginx returned the "Hello..." page to You.
This request chain is worth remembering because it appears everywhere in modern infrastructure: browser -> reverse proxy -> application server -> reverse proxy -> browser.
You have successfully built a Reverse Proxy!
Summary
In this lab, you have successfully configured Nginx as a reverse proxy. You learned how to:
- Host a simple backend application using Python.
- Configure Nginx with a
locationblock for routing. - Forward traffic using the
proxy_passdirective. - Verify the proxy chain using
curl.
This architecture is the backbone of modern web applications, allowing you to use Nginx for security and load balancing while other applications handle the logic in the background.



