| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- events {
- worker_connections 1024;
- }
- 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 10M;
- # Gzip 压缩
- gzip on;
- gzip_vary on;
- gzip_min_length 1024;
- gzip_proxied any;
- gzip_comp_level 6;
- gzip_types
- text/plain
- text/css
- text/xml
- text/javascript
- application/javascript
- application/json
- application/xml+rss
- application/atom+xml
- image/svg+xml;
- # 上游服务器
- upstream tolan_backend {
- server backend:8765;
- keepalive 32;
- }
- # 速率限制
- limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s;
- limit_req_zone $binary_remote_addr zone=websocket:10m rate=5r/s;
- # 主服务器配置
- server {
- listen 80;
- server_name localhost;
-
- # 安全头
- add_header X-Frame-Options "SAMEORIGIN" always;
- add_header X-XSS-Protection "1; mode=block" always;
- add_header X-Content-Type-Options "nosniff" always;
- add_header Referrer-Policy "no-referrer-when-downgrade" always;
- add_header Content-Security-Policy "default-src 'self' 'unsafe-inline' 'unsafe-eval' data: blob:; connect-src 'self' ws: wss:;" always;
- # 前端静态文件
- location / {
- root /usr/share/nginx/html;
- index index.html index.htm;
- try_files $uri $uri/ /index.html;
-
- # 缓存策略
- location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
- expires 1y;
- add_header Cache-Control "public, immutable";
- access_log off;
- }
-
- # HTML 文件不缓存
- location ~* \.html$ {
- expires -1;
- add_header Cache-Control "no-cache, no-store, must-revalidate";
- }
- }
- # WebSocket 代理
- location /ws {
- limit_req zone=websocket burst=10 nodelay;
-
- proxy_pass http://tolan_backend;
- 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;
-
- # WebSocket 超时设置
- proxy_connect_timeout 60s;
- proxy_send_timeout 60s;
- proxy_read_timeout 60s;
-
- # 缓冲设置
- proxy_buffering off;
- proxy_cache off;
- }
- # API 代理 (如果需要)
- location /api/ {
- limit_req zone=api burst=20 nodelay;
-
- proxy_pass http://tolan_backend/;
- 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;
-
- # API 超时设置
- proxy_connect_timeout 30s;
- proxy_send_timeout 30s;
- proxy_read_timeout 30s;
- }
- # 健康检查
- location /health {
- proxy_pass http://tolan_backend/health;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- access_log off;
- }
- # 禁止访问敏感文件
- location ~ /\. {
- deny all;
- access_log off;
- log_not_found off;
- }
-
- location ~ /\.well-known {
- allow all;
- }
- # 监控端点
- location /nginx_status {
- stub_status on;
- allow 127.0.0.1;
- allow 172.20.0.0/16;
- deny all;
- access_log off;
- }
- }
- # HTTPS 配置 (如果有 SSL 证书)
- # server {
- # listen 443 ssl http2;
- # server_name your-domain.com;
- #
- # ssl_certificate /etc/ssl/certs/tolan.crt;
- # ssl_certificate_key /etc/ssl/certs/tolan.key;
- #
- # # SSL 设置
- # ssl_protocols TLSv1.2 TLSv1.3;
- # ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384;
- # ssl_prefer_server_ciphers off;
- # ssl_session_cache shared:SSL:10m;
- # ssl_session_timeout 10m;
- #
- # # HSTS
- # add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
- #
- # # 复用上面的 location 配置
- # include /etc/nginx/conf.d/locations.conf;
- # }
-
- # HTTP 重定向到 HTTPS (如果启用 SSL)
- # server {
- # listen 80;
- # server_name your-domain.com;
- # return 301 https://$server_name$request_uri;
- # }
- }
|