0
0
Djangoframework~5 mins

Nginx as reverse proxy in Django

Choose your learning style9 modes available
Introduction

Nginx as a reverse proxy helps direct web traffic to your Django app safely and efficiently. It acts like a friendly gatekeeper that manages requests from users to your server.

You want to serve your Django app to many users without slowing down.
You want to hide your Django app server details from the internet for security.
You want to handle static files like images and CSS separately for faster loading.
You want to use HTTPS (secure connection) easily with your Django app.
You want to balance traffic between multiple Django app servers.
Syntax
Django
server {
    listen 80;
    server_name yourdomain.com;

    location / {
        proxy_pass http://127.0.0.1:8000;
        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;
    }
}

proxy_pass tells Nginx where your Django app is running (usually localhost with port 8000).

Headers like X-Real-IP help your Django app know the real user IP address.

Examples
This serves static files directly from Nginx, so Django doesn't have to handle them.
Django
location /static/ {
    alias /path/to/your/staticfiles/;
}
This example shows how to configure Nginx to use HTTPS with SSL certificates.
Django
server {
    listen 443 ssl;
    server_name yourdomain.com;

    ssl_certificate /etc/ssl/certs/yourcert.pem;
    ssl_certificate_key /etc/ssl/private/yourkey.pem;

    location / {
        proxy_pass http://127.0.0.1:8000;
        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;
    }
}
Sample Program

This Nginx config sends all normal web requests to the Django app running on localhost port 8000. Static files are served directly from the specified folder for faster loading.

Django
server {
    listen 80;
    server_name example.com;

    location /static/ {
        alias /home/user/myproject/static/;
    }

    location / {
        proxy_pass http://127.0.0.1:8000;
        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;
    }
}
OutputSuccess
Important Notes

Remember to collect static files in Django using python manage.py collectstatic before serving them with Nginx.

Make sure your Django app is running and accessible on the port Nginx proxies to.

Use firewall rules to allow traffic only through Nginx, not directly to Django app port.

Summary

Nginx as a reverse proxy forwards user requests to your Django app safely and efficiently.

It can serve static files directly, improving speed and reducing load on Django.

It helps add HTTPS and security features easily in front of your Django app.