本地 python app.py 能跑,不代表能上线。生产环境通常是:Docker 跑应用 + Nginx 对外反代。本文给你最小可用图景。
为什么需要 Nginx
- 统一 80/443 入口,后端不直接暴露
- 静态资源缓存、HTTPS 终结
- 多服务按域名/路径分流
反向代理片段
server {
listen 80;
server_name demo.example.com;
location / {
proxy_pass http://webapp: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;
}
}
Docker Compose 思路
services:
webapp:
build: .
networks: [app-net]
nginx:
image: nginx:1.25-alpine
ports: ["80:80"]
volumes:
- ./nginx.conf:/etc/nginx/conf.d/default.conf:ro
depends_on: [webapp]
networks: [app-net]
上线检查清单
- 健康检查接口:
/health - 日志落盘或收集,方便排错
- 密钥走环境变量
- 数据库不映射公网端口
你现在的 antpython.net 与博客,就是「同网桥 + Nginx 按域名反代」的典型结构。