nginx as a reverse proxy
You put nginx in front of a python3 backend that only listens on 127.0.0.1:9000. Classic web stack: backend produces content, nginx handles port 80, caching, logs and SSL. Four tasks, around 40 minutes. Prequel to the deploy-and-tls module (that's this architecture + Let's Encrypt).
Put a backend behind nginx
nginx as a reverse proxy
A reverse proxy is a server that accepts requests from the internet and forwards them to a backend behind the scenes. The browser only talks to nginx (port 80/443); the actual program (here a Python backend) listens only locally on 127.0.0.1:9000 and is not reachable from outside at all.
Why do this?
- Security: the backend is not directly exposed.
- TLS/HTTPS: nginx handles encryption centrally.
- Caching, logs, load balancing: nginx can do this for many backends in one place.
This is the classic web stack — and the precursor to the module "Deploy a live website with HTTPS".
Key terms
- Backend: the program that produces the content (here listening on
127.0.0.1:9000). proxy_pass: the nginx directive that forwards requests to the backend.- sites-available / sites-enabled: nginx stores site configs in
sites-available/and activates them via a symlink insites-enabled/.
Your goal
You configure nginx so that port 80 forwards requests to the local backend on port 9000.
Exercises
1. nginx is running
Concept: service status. A reverse proxy is useless if nginx is not running. Make sure the nginx service is active.
sudo systemctl enable --now nginxCheck:
systemctl is-active nginxreportsactive.2. Site config written
Concept: site configuration. A site config describes how nginx handles a particular website. With
proxy_passyou forward all requests to the local backend.Create
/etc/nginx/sites-available/lab-proxy.conf(sudo nano ...) with:server { listen 80; location / { proxy_pass http://127.0.0.1:9000; proxy_set_header Host $host; } }Check:
/etc/nginx/sites-available/lab-proxy.confcontainsproxy_pass http://127.0.0.1:9000.3. Enable the site
Concept: enabling a site (symlink). A config in
sites-available/is only present, not active. It becomes active through a symlink intosites-enabled/. Remove the default site so it doesn't interfere, and reload nginx.sudo ln -sf /etc/nginx/sites-available/lab-proxy.conf /etc/nginx/sites-enabled/lab-proxy.conf sudo rm -f /etc/nginx/sites-enabled/default sudo nginx -t && sudo systemctl reload nginxCheck:
/etc/nginx/sites-enabled/lab-proxy.confexists and containsproxy_pass http://127.0.0.1:9000.4. Proxy delivers backend content
Concept: end-to-end test. Checking the config means: test live that nginx really serves the backend content. Request localhost on port 80 — you must see the backend's response.
curl -s http://localhost/ > /tmp/nginx-output.txt cat /tmp/nginx-output.txtCheck:
/tmp/nginx-output.txtcontainsHello from Lab-Backend(the proxy passes the backend content through).
Now practice it yourself
Reading is good – doing is better. Start this course on a real Linux VM, right in your browser. A free account is all it takes.
Start for freeLab content under CC BY 4.0 – free to use with attribution (© TechLogia).
