DRF 部署上线 #
一、部署概述 #
1.1 部署架构 #
text
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ Nginx │────▶│ Gunicorn │────▶│ Django │
│ (反向代理) │ │ (应用服务器) │ │ (应用) │
└─────────────┘ └─────────────┘ └─────────────┘
│ │
▼ ▼
┌─────────────┐ ┌─────────────┐
│ 静态文件 │ │ 数据库 │
└─────────────┘ └─────────────┘
1.2 部署清单 #
- Gunicorn应用服务器
- Nginx反向代理
- PostgreSQL数据库
- Redis缓存
- Supervisor进程管理
- 日志和监控
二、生产环境配置 #
2.1 settings/production.py #
python
import os
from .base import *
DEBUG = False
ALLOWED_HOSTS = os.getenv('ALLOWED_HOSTS', '').split(',')
SECURE_SSL_REDIRECT = True
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
SECURE_BROWSER_XSS_FILTER = True
SECURE_CONTENT_TYPE_NOSNIFF = True
X_FRAME_OPTIONS = 'DENY'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.getenv('DB_NAME'),
'USER': os.getenv('DB_USER'),
'PASSWORD': os.getenv('DB_PASSWORD'),
'HOST': os.getenv('DB_HOST'),
'PORT': os.getenv('DB_PORT', '5432'),
}
}
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.redis.RedisCache',
'LOCATION': os.getenv('REDIS_URL'),
}
}
STATIC_ROOT = BASE_DIR / 'staticfiles'
MEDIA_ROOT = BASE_DIR / 'media'
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': '{levelname} {asctime} {module} {message}',
'style': '{',
},
},
'handlers': {
'file': {
'level': 'ERROR',
'class': 'logging.FileHandler',
'filename': BASE_DIR / 'logs' / 'django.log',
'formatter': 'verbose',
},
},
'loggers': {
'django': {
'handlers': ['file'],
'level': 'ERROR',
'propagate': True,
},
},
}
三、Gunicorn配置 #
3.1 安装 #
bash
pip install gunicorn
3.2 gunicorn.conf.py #
python
import multiprocessing
bind = '127.0.0.1:8000'
workers = multiprocessing.cpu_count() * 2 + 1
worker_class = 'sync'
timeout = 30
keepalive = 2
errorlog = '-'
accesslog = '-'
loglevel = 'info'
3.3 启动命令 #
bash
gunicorn myproject.wsgi:application -c gunicorn.conf.py
四、Nginx配置 #
4.1 nginx.conf #
nginx
upstream django {
server 127.0.0.1:8000;
}
server {
listen 80;
server_name api.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
server_name api.example.com;
ssl_certificate /etc/letsencrypt/live/api.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/api.example.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
location /static/ {
alias /var/www/myproject/staticfiles/;
expires 30d;
}
location /media/ {
alias /var/www/myproject/media/;
expires 30d;
}
location / {
proxy_pass http://django;
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;
}
}
五、Docker部署 #
5.1 Dockerfile #
dockerfile
FROM python:3.11-slim
WORKDIR /app
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
RUN python manage.py collectstatic --noinput
EXPOSE 8000
CMD ["gunicorn", "myproject.wsgi:application", "-b", "0.0.0.0:8000"]
5.2 docker-compose.yml #
yaml
version: '3.8'
services:
web:
build: .
command: gunicorn myproject.wsgi:application -b 0.0.0.0:8000
volumes:
- .:/app
- static_volume:/app/staticfiles
- media_volume:/app/media
expose:
- "8000"
depends_on:
- db
- redis
environment:
- DEBUG=False
- DATABASE_URL=postgres://user:password@db:5432/mydb
- REDIS_URL=redis://redis:6379/0
db:
image: postgres:15
volumes:
- postgres_data:/var/lib/postgresql/data
environment:
- POSTGRES_DB=mydb
- POSTGRES_USER=user
- POSTGRES_PASSWORD=password
redis:
image: redis:7-alpine
volumes:
- redis_data:/data
nginx:
image: nginx:alpine
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
- static_volume:/app/staticfiles
- media_volume:/app/media
depends_on:
- web
volumes:
postgres_data:
redis_data:
static_volume:
media_volume:
六、Supervisor配置 #
6.1 安装 #
bash
sudo apt install supervisor
6.2 配置文件 #
ini
[program:myproject]
command=/var/www/myproject/venv/bin/gunicorn myproject.wsgi:application -c gunicorn.conf.py
directory=/var/www/myproject
user=www-data
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/var/log/myproject/gunicorn.log
七、监控和日志 #
7.1 Sentry集成 #
python
import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration
sentry_sdk.init(
dsn=os.getenv('SENTRY_DSN'),
integrations=[DjangoIntegration()],
traces_sample_rate=1.0,
)
7.2 健康检查 #
python
from rest_framework.decorators import api_view
from rest_framework.response import Response
@api_view(['GET'])
def health_check(request):
return Response({'status': 'healthy'})
八、总结 #
本章学习了DRF部署:
- 生产环境配置
- Gunicorn应用服务器
- Nginx反向代理
- Docker容器化
- 监控和日志
部署是API上线的最后一步!
最后更新:2026-03-28