Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Nginx as Reverse Proxy for Django
📖 Scenario: You have a Django web application running on your local machine on port 8000. You want to set up Nginx as a reverse proxy to forward requests from port 80 to your Django app. This setup helps improve performance and security by letting Nginx handle client requests first.
🎯 Goal: Build a simple Nginx configuration file that acts as a reverse proxy forwarding HTTP requests to your Django app running on localhost:8000.
📋 What You'll Learn
Create a basic Nginx server block configuration
Set the server to listen on port 80
Use proxy_pass to forward requests to http://localhost:8000
Include necessary proxy headers for Django compatibility
💡 Why This Matters
🌍 Real World
Using Nginx as a reverse proxy is common in deploying Django apps to improve security, performance, and scalability.
💼 Career
Many web developer and DevOps roles require configuring Nginx to serve as a reverse proxy for backend applications like Django.
Progress0 / 4 steps
1
Create the Nginx server block skeleton
Create a file called nginx.conf and write a basic server block that listens on port 80 with an empty location / block.
Django
Hint
Start by defining the server block and set it to listen on port 80. Add an empty location / block inside.
2
Add the proxy_pass directive
Inside the location / block, add the line proxy_pass http://localhost:8000; to forward requests to the Django app running on port 8000.
Django
Hint
Use proxy_pass inside the location / block to forward all requests to http://localhost:8000.
3
Add proxy headers for Django compatibility
Inside the location / block, add these lines to set headers: 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;
Django
Hint
These headers help Django know the original request details like host and client IP.
4
Complete the Nginx reverse proxy configuration
Add the server_name localhost; line inside the server block to specify the server name.
Django
Hint
Setting server_name helps Nginx know which requests to handle.
Practice
(1/5)
1. What is the main role of Nginx when used as a reverse proxy for a Django application?
easy
A. Replace the Django application with a static website
B. Directly execute Django Python code
C. Forward client requests to the Django application server
D. Store the Django database backups
Solution
Step 1: Understand Nginx reverse proxy function
Nginx acts as a middleman that receives client requests and forwards them to the Django app server.
Step 2: Identify what Nginx does not do
Nginx does not replace Django, execute Python code, or store backups; it only forwards requests and can serve static files.
Final Answer:
Forward client requests to the Django application server -> Option C
Quick Check:
Nginx forwards requests [OK]
Hint: Nginx forwards requests to Django, it doesn't run Django code [OK]
Common Mistakes:
Thinking Nginx runs Django code
Confusing Nginx with a database
Assuming Nginx replaces Django app
2. Which Nginx configuration snippet correctly sets it as a reverse proxy forwarding requests to a Django app running on port 8000?
easy
A. location / { proxy_pass http://127.0.0.1:8000; }
B. location / { root /var/www/html; }
C. location / { fastcgi_pass 127.0.0.1:8000; }
D. location / { proxy_redirect off; }
Solution
Step 1: Identify correct proxy directive
To forward requests, Nginx uses proxy_pass with the Django app address and port.
Step 2: Check other options for correctness
root serves static files, fastcgi_pass is for FastCGI apps, and proxy_redirect controls redirects but doesn't forward requests.
Final Answer:
location / { proxy_pass http://127.0.0.1:8000; } -> Option A
Quick Check:
Use proxy_pass for reverse proxy [OK]
Hint: proxy_pass forwards requests to backend server [OK]
Common Mistakes:
Using root instead of proxy_pass for proxying
Confusing fastcgi_pass with proxy_pass
Omitting the backend address in proxy_pass
3. Given this Nginx config snippet, what happens when a client requests /static/css/style.css?
A. Nginx serves the static file directly from /home/user/myproject/static/css/style.css
B. Nginx forwards the request to Django app on port 8000
C. Nginx returns a 404 error because alias is incorrect
D. Nginx redirects the request to /static/css/style.css on Django
Solution
Step 1: Understand alias directive for static files
The location /static/ block uses alias to serve files directly from the filesystem path.
Step 2: Analyze request routing
Requests to /static/ are served by Nginx from the alias path; other requests go to Django via proxy_pass.
Final Answer:
Nginx serves the static file directly from /home/user/myproject/static/css/style.css -> Option A
Quick Check:
Static files served by alias [OK]
Hint: alias serves static files directly, proxy_pass forwards others [OK]
Common Mistakes:
Thinking all requests go to Django
Confusing alias with root directive
Assuming Nginx redirects instead of serving static files
4. You configured Nginx as a reverse proxy for Django but get a 502 Bad Gateway error. Which fix is most likely correct?
medium
A. Change Nginx config to use root instead of proxy_pass
B. Start the Django app server on the port Nginx proxies to
C. Remove the location block for static files
D. Disable firewall on the client machine
Solution
Step 1: Understand 502 Bad Gateway cause
502 means Nginx cannot connect to the backend server, often because it is not running or listening on the expected port.
Step 2: Identify the correct fix
Starting the Django app server on the port Nginx expects resolves the connection issue.
Final Answer:
Start the Django app server on the port Nginx proxies to -> Option B
Quick Check:
502 error means backend not reachable [OK]
Hint: 502 means backend server not running or unreachable [OK]
Common Mistakes:
Changing root instead of fixing backend server
Removing static files block unrelated to 502
Disabling client firewall won't fix server connection
5. You want Nginx to serve static files directly and forward all other requests to Django on port 8000. Which combined Nginx config snippet achieves this correctly?
hard
A. location /static/ { proxy_pass http://127.0.0.1:8000; } location / { alias /var/www/myproject/static/; }
B. location / { alias /var/www/myproject/static/; } location /static/ { proxy_pass http://127.0.0.1:8000; }