139 lines
4.3 KiB
Nginx Configuration File
139 lines
4.3 KiB
Nginx Configuration File
# 生产环境 Nginx 配置
|
||
# 用途:反向代理 + HTTPS 支持
|
||
|
||
events {
|
||
worker_connections 1024;
|
||
use epoll;
|
||
}
|
||
|
||
http {
|
||
include /etc/nginx/mime.types;
|
||
default_type application/octet-stream;
|
||
|
||
# 日志格式
|
||
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
|
||
'$status $body_bytes_sent "$http_referer" '
|
||
'"$http_user_agent" "$http_x_forwarded_for"';
|
||
|
||
access_log /var/log/nginx/access.log main;
|
||
error_log /var/log/nginx/error.log warn;
|
||
|
||
# 性能优化
|
||
sendfile on;
|
||
tcp_nopush on;
|
||
tcp_nodelay on;
|
||
keepalive_timeout 65;
|
||
types_hash_max_size 2048;
|
||
client_max_body_size 20M;
|
||
|
||
# Gzip 压缩
|
||
gzip on;
|
||
gzip_vary on;
|
||
gzip_proxied any;
|
||
gzip_comp_level 6;
|
||
gzip_types text/plain text/css text/xml text/javascript application/json application/javascript application/xml+rss;
|
||
|
||
# Upstream 定义
|
||
upstream backend {
|
||
server backend:8000;
|
||
keepalive 32;
|
||
}
|
||
|
||
upstream admin {
|
||
server admin:3000;
|
||
keepalive 32;
|
||
}
|
||
|
||
# HTTP → HTTPS 重定向
|
||
server {
|
||
listen 80;
|
||
server_name _;
|
||
|
||
# Let's Encrypt 验证路径(用于证书申请和续期)
|
||
location /.well-known/acme-challenge/ {
|
||
root /var/www/certbot;
|
||
try_files $uri =404;
|
||
}
|
||
|
||
# 健康检查(允许 HTTP 访问)
|
||
location /health {
|
||
proxy_pass http://backend/api/health;
|
||
access_log off;
|
||
}
|
||
|
||
# 其他请求重定向到 HTTPS
|
||
location / {
|
||
return 301 https://$host$request_uri;
|
||
}
|
||
}
|
||
|
||
# HTTPS 服务器
|
||
server {
|
||
listen 443 ssl http2;
|
||
server_name _;
|
||
|
||
# SSL 证书配置(Let's Encrypt)
|
||
# 注意:首次部署时,这些路径可能不存在,需要先配置证书
|
||
# 证书配置步骤见 docs/deploy.md
|
||
ssl_certificate /etc/letsencrypt/live/_/fullchain.pem;
|
||
ssl_certificate_key /etc/letsencrypt/live/_/privkey.pem;
|
||
|
||
# SSL 安全配置
|
||
ssl_protocols TLSv1.2 TLSv1.3;
|
||
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384';
|
||
ssl_prefer_server_ciphers on;
|
||
ssl_session_cache shared:SSL:10m;
|
||
ssl_session_timeout 10m;
|
||
|
||
# HSTS(可选,生产环境推荐)
|
||
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
|
||
|
||
# /api -> backend
|
||
location /api/ {
|
||
proxy_pass http://backend;
|
||
proxy_http_version 1.1;
|
||
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_set_header Connection "";
|
||
proxy_read_timeout 30s;
|
||
proxy_connect_timeout 10s;
|
||
proxy_send_timeout 30s;
|
||
}
|
||
|
||
# 健康检查
|
||
location /health {
|
||
proxy_pass http://backend/api/health;
|
||
access_log off;
|
||
}
|
||
|
||
# 其余 -> admin(如果 admin 未上线,返回静态占位页)
|
||
location / {
|
||
# 如果 admin 服务不可用,返回占位页
|
||
# 可以通过检查 admin 服务状态来决定
|
||
proxy_pass http://admin;
|
||
proxy_http_version 1.1;
|
||
proxy_set_header Upgrade $http_upgrade;
|
||
proxy_set_header Connection "upgrade";
|
||
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_read_timeout 30s;
|
||
proxy_connect_timeout 10s;
|
||
proxy_send_timeout 30s;
|
||
|
||
# 如果 admin 服务不可用,返回 503 或静态占位页
|
||
# 可以通过 error_page 配置实现
|
||
error_page 502 503 504 = @admin_fallback;
|
||
}
|
||
|
||
# Admin 服务不可用时的占位页
|
||
location @admin_fallback {
|
||
default_type text/html;
|
||
return 503 '<!DOCTYPE html><html><head><title>Admin 服务维护中</title></head><body><h1>Admin 服务维护中</h1><p>管理后台暂时不可用,请稍后再试。</p></body></html>';
|
||
}
|
||
}
|
||
}
|