Gunicorn chạy Django tốt, nhưng không nên để Gunicorn tiếp nhận request trực tiếp từ internet. Nginx đứng trước làm reverse proxy — xử lý SSL, static files, connection pooling, và rate limiting hiệu quả hơn nhiều.
Kiến Trúc Triển Khai
Internet → Nginx (port 80/443) → Gunicorn (port 8000) → Django
Nginx nhận tất cả request, serve static files trực tiếp, và forward dynamic request đến Gunicorn.
Cài Đặt Cơ Bản
# /etc/nginx/sites-available/blog
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
# Static files — nginx serve trực tiếp, không qua Django
location /static/ {
alias /app/staticfiles/;
expires 1y;
add_header Cache-Control "public, immutable";
}
location /media/ {
alias /app/media/;
}
# Forward đến Gunicorn
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;
}
}
# Kích hoạt
ln -s /etc/nginx/sites-available/blog /etc/nginx/sites-enabled/
nginx -t && systemctl reload nginx
Cấu Hình HTTPS Với Let's Encrypt
# Cài certbot
apt install certbot python3-certbot-nginx
certbot --nginx -d yourdomain.com -d www.yourdomain.com
# Certbot tự sửa nginx config, thêm:
server {
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
# Redirect HTTP → HTTPS
if ($scheme = http) {
return 301 https://$host$request_uri;
}
}
Gzip Compression
# /etc/nginx/nginx.conf
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_types
text/plain
text/css
application/json
application/javascript
text/xml
image/svg+xml;
Rate Limiting
# Định nghĩa zone (trong http block)
limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s;
limit_req_zone $binary_remote_addr zone=login:10m rate=5r/m;
# Áp dụng trong location
location /api/ {
limit_req zone=api burst=20 nodelay;
proxy_pass http://127.0.0.1:8000;
}
location /admin/login/ {
limit_req zone=login;
proxy_pass http://127.0.0.1:8000;
}
Security Headers
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
Upstream Load Balancing
# Chạy nhiều Gunicorn workers
upstream django_app {
server 127.0.0.1:8000;
server 127.0.0.1:8001;
server 127.0.0.1:8002;
keepalive 32;
}
location / {
proxy_pass http://django_app;
}
Nginx có thể serve hàng chục nghìn concurrent connections với rất ít RAM nhờ kiến trúc event-driven non-blocking. Đó là lý do nó thay thế Apache trong phần lớn production stack hiện đại.
Kết Luận
Cấu hình Nginx đúng là một trong những việc có ROI cao nhất trong việc vận hành web app. Static files, SSL, gzip và rate limiting — bốn thứ này đủ để site của bạn nhanh hơn và an toàn hơn đáng kể.
Chưa có bình luận. Hãy là người đầu tiên!