nginx.conf 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. events {
  2. worker_connections 1024;
  3. }
  4. http {
  5. include /etc/nginx/mime.types;
  6. default_type application/octet-stream;
  7. # 日志格式
  8. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  9. '$status $body_bytes_sent "$http_referer" '
  10. '"$http_user_agent" "$http_x_forwarded_for"';
  11. access_log /var/log/nginx/access.log main;
  12. error_log /var/log/nginx/error.log warn;
  13. # 基础设置
  14. sendfile on;
  15. tcp_nopush on;
  16. tcp_nodelay on;
  17. keepalive_timeout 65;
  18. types_hash_max_size 2048;
  19. client_max_body_size 10M;
  20. # Gzip 压缩
  21. gzip on;
  22. gzip_vary on;
  23. gzip_min_length 1024;
  24. gzip_proxied any;
  25. gzip_comp_level 6;
  26. gzip_types
  27. text/plain
  28. text/css
  29. text/xml
  30. text/javascript
  31. application/javascript
  32. application/json
  33. application/xml+rss
  34. application/atom+xml
  35. image/svg+xml;
  36. # 上游服务器
  37. upstream tolan_backend {
  38. server backend:8765;
  39. keepalive 32;
  40. }
  41. # 速率限制
  42. limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s;
  43. limit_req_zone $binary_remote_addr zone=websocket:10m rate=5r/s;
  44. # 主服务器配置
  45. server {
  46. listen 80;
  47. server_name localhost;
  48. # 安全头
  49. add_header X-Frame-Options "SAMEORIGIN" always;
  50. add_header X-XSS-Protection "1; mode=block" always;
  51. add_header X-Content-Type-Options "nosniff" always;
  52. add_header Referrer-Policy "no-referrer-when-downgrade" always;
  53. add_header Content-Security-Policy "default-src 'self' 'unsafe-inline' 'unsafe-eval' data: blob:; connect-src 'self' ws: wss:;" always;
  54. # 前端静态文件
  55. location / {
  56. root /usr/share/nginx/html;
  57. index index.html index.htm;
  58. try_files $uri $uri/ /index.html;
  59. # 缓存策略
  60. location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
  61. expires 1y;
  62. add_header Cache-Control "public, immutable";
  63. access_log off;
  64. }
  65. # HTML 文件不缓存
  66. location ~* \.html$ {
  67. expires -1;
  68. add_header Cache-Control "no-cache, no-store, must-revalidate";
  69. }
  70. }
  71. # WebSocket 代理
  72. location /ws {
  73. limit_req zone=websocket burst=10 nodelay;
  74. proxy_pass http://tolan_backend;
  75. proxy_http_version 1.1;
  76. proxy_set_header Upgrade $http_upgrade;
  77. proxy_set_header Connection "upgrade";
  78. proxy_set_header Host $host;
  79. proxy_set_header X-Real-IP $remote_addr;
  80. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  81. proxy_set_header X-Forwarded-Proto $scheme;
  82. # WebSocket 超时设置
  83. proxy_connect_timeout 60s;
  84. proxy_send_timeout 60s;
  85. proxy_read_timeout 60s;
  86. # 缓冲设置
  87. proxy_buffering off;
  88. proxy_cache off;
  89. }
  90. # API 代理 (如果需要)
  91. location /api/ {
  92. limit_req zone=api burst=20 nodelay;
  93. proxy_pass http://tolan_backend/;
  94. proxy_set_header Host $host;
  95. proxy_set_header X-Real-IP $remote_addr;
  96. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  97. proxy_set_header X-Forwarded-Proto $scheme;
  98. # API 超时设置
  99. proxy_connect_timeout 30s;
  100. proxy_send_timeout 30s;
  101. proxy_read_timeout 30s;
  102. }
  103. # 健康检查
  104. location /health {
  105. proxy_pass http://tolan_backend/health;
  106. proxy_set_header Host $host;
  107. proxy_set_header X-Real-IP $remote_addr;
  108. access_log off;
  109. }
  110. # 禁止访问敏感文件
  111. location ~ /\. {
  112. deny all;
  113. access_log off;
  114. log_not_found off;
  115. }
  116. location ~ /\.well-known {
  117. allow all;
  118. }
  119. # 监控端点
  120. location /nginx_status {
  121. stub_status on;
  122. allow 127.0.0.1;
  123. allow 172.20.0.0/16;
  124. deny all;
  125. access_log off;
  126. }
  127. }
  128. # HTTPS 配置 (如果有 SSL 证书)
  129. # server {
  130. # listen 443 ssl http2;
  131. # server_name your-domain.com;
  132. #
  133. # ssl_certificate /etc/ssl/certs/tolan.crt;
  134. # ssl_certificate_key /etc/ssl/certs/tolan.key;
  135. #
  136. # # SSL 设置
  137. # ssl_protocols TLSv1.2 TLSv1.3;
  138. # ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384;
  139. # ssl_prefer_server_ciphers off;
  140. # ssl_session_cache shared:SSL:10m;
  141. # ssl_session_timeout 10m;
  142. #
  143. # # HSTS
  144. # add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
  145. #
  146. # # 复用上面的 location 配置
  147. # include /etc/nginx/conf.d/locations.conf;
  148. # }
  149. # HTTP 重定向到 HTTPS (如果启用 SSL)
  150. # server {
  151. # listen 80;
  152. # server_name your-domain.com;
  153. # return 301 https://$server_name$request_uri;
  154. # }
  155. }