nginx.conf 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. worker_processes auto; # 自动匹配 CPU 核数
  2. events {
  3. worker_connections 8192; # 每进程最大连接数
  4. use epoll; # 使用高效事件模型
  5. multi_accept on; # 批量接受新连接
  6. }
  7. http {
  8. include mime.types;
  9. default_type application/octet-stream;
  10. # 隐藏 Nginx 版本信息
  11. server_tokens off;
  12. # 文件 I/O 性能优化
  13. sendfile on;
  14. tcp_nopush on;
  15. tcp_nodelay on;
  16. # 文件描述符缓存(减少系统调用)
  17. open_file_cache max=10000 inactive=30s;
  18. open_file_cache_valid 60s;
  19. open_file_cache_min_uses 2;
  20. open_file_cache_errors on;
  21. # Keepalive 连接优化
  22. keepalive_timeout 65;
  23. keepalive_requests 100;
  24. # 客户端请求限制
  25. client_max_body_size 10m;
  26. client_body_buffer_size 128k;
  27. client_header_buffer_size 1k;
  28. large_client_header_buffers 4 8k;
  29. # 日志缓冲(提升高并发性能)
  30. access_log off; # 生产环境可关闭访问日志,或使用缓冲
  31. # access_log /var/log/nginx/access.log combined buffer=32k flush=5s;
  32. # 限流配置
  33. limit_req_zone $binary_remote_addr zone=api_limit:10m rate=10r/s;
  34. limit_req_zone $binary_remote_addr zone=ws_limit:10m rate=5r/s;
  35. limit_conn_zone $binary_remote_addr zone=conn_limit:10m;
  36. # 启用gzip压缩(优化压缩级别)
  37. gzip on;
  38. gzip_vary on;
  39. gzip_min_length 1024;
  40. gzip_proxied any;
  41. gzip_comp_level 5; # 降低压缩级别,平衡 CPU 和压缩比
  42. gzip_types
  43. text/plain
  44. text/css
  45. text/js
  46. text/xml
  47. text/javascript
  48. application/javascript
  49. application/xml+rss
  50. application/json
  51. application/wasm
  52. image/svg+xml;
  53. # HTTP 端口 80 配置,自动重定向到 HTTPS
  54. server {
  55. listen 80;
  56. server_name www.lyrria.com;
  57. #verification.html 可以直接通过HTTP访问
  58. location = /verification.html {
  59. root /root/Lyrria/frontend/build;
  60. try_files $uri =404;
  61. add_header Cache-Control "no-cache, no-store, must-revalidate";
  62. add_header Pragma "no-cache";
  63. add_header Expires "0";
  64. }
  65. # 其他所有路径重定向到HTTPS
  66. location / {
  67. return 301 https://$host$request_uri;
  68. }
  69. }
  70. server {
  71. listen 443 ssl http2; # 启用 HTTP/2
  72. server_name www.lyrria.com;
  73. ssl_certificate "/etc/nginx/cert/lyrria.com.pem"; # SSL证书位置
  74. ssl_certificate_key "/etc/nginx/cert/lyrria.com.key"; # SSL证书位置
  75. # SSL/TLS 优化
  76. ssl_session_cache shared:SSL:50m; # 增加会话缓存大小
  77. ssl_session_timeout 1h;
  78. ssl_session_tickets off; # 禁用 session tickets,使用共享缓存
  79. ssl_protocols TLSv1.2 TLSv1.3;
  80. ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384';
  81. ssl_prefer_server_ciphers off; # TLS 1.3 不需要此选项
  82. # ssl_dhparam /etc/nginx/cert/dhparam.pem; # 如果存在 DH 参数文件,取消注释
  83. # 处理favicon.ico请求
  84. location = /favicon.ico {
  85. root /root/Lyrria/frontend/build;
  86. access_log off;
  87. log_not_found off;
  88. expires 1y;
  89. add_header Cache-Control "public, immutable";
  90. }
  91. # 处理robots.txt请求
  92. location = /robots.txt {
  93. access_log off;
  94. log_not_found off;
  95. return 200 "User-agent: *\nDisallow:\n";
  96. }
  97. # WebSocket连接代理(使用WSS端口8766)
  98. location /ws {
  99. limit_req zone=ws_limit burst=10 nodelay;
  100. limit_conn conn_limit 10;
  101. proxy_pass http://127.0.0.1:8766;
  102. proxy_http_version 1.1;
  103. # WebSocket 必需头部
  104. proxy_set_header Upgrade $http_upgrade;
  105. proxy_set_header Connection "upgrade";
  106. # 标准代理头部
  107. proxy_set_header Host $host;
  108. proxy_set_header X-Real-IP $remote_addr;
  109. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  110. proxy_set_header X-Forwarded-Proto $scheme;
  111. proxy_set_header X-Forwarded-Host $host;
  112. # WebSocket 优化:禁用缓冲
  113. proxy_buffering off;
  114. proxy_cache off;
  115. # 超时设置
  116. proxy_read_timeout 3600s;
  117. proxy_send_timeout 3600s;
  118. proxy_connect_timeout 10s;
  119. }
  120. # API接口代理(如果有REST API需求)
  121. location ^~ /api/ {
  122. limit_req zone=api_limit burst=20 nodelay;
  123. proxy_pass http://127.0.0.1:8766$request_uri;
  124. # 代理头部
  125. proxy_set_header Host $host;
  126. proxy_set_header X-Real-IP $remote_addr;
  127. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  128. proxy_set_header X-Forwarded-Proto $scheme;
  129. proxy_set_header X-Forwarded-Host $host;
  130. # 代理缓冲优化
  131. proxy_buffering on;
  132. proxy_buffer_size 4k;
  133. proxy_buffers 8 4k;
  134. proxy_busy_buffers_size 8k;
  135. proxy_temp_file_write_size 8k;
  136. # 超时设置
  137. proxy_read_timeout 60s;
  138. proxy_send_timeout 60s;
  139. proxy_connect_timeout 10s;
  140. }
  141. # 客户端指标上报接口代理
  142. location = /metrics/client {
  143. limit_req zone=api_limit burst=10 nodelay;
  144. proxy_pass http://127.0.0.1:8766/metrics/client;
  145. # 代理头部
  146. proxy_set_header Host $host;
  147. proxy_set_header X-Real-IP $remote_addr;
  148. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  149. proxy_set_header X-Forwarded-Proto $scheme;
  150. proxy_set_header X-Forwarded-Host $host;
  151. # 代理缓冲
  152. proxy_buffering on;
  153. proxy_buffer_size 4k;
  154. proxy_buffers 4 4k;
  155. # 超时设置
  156. proxy_read_timeout 60s;
  157. proxy_send_timeout 60s;
  158. proxy_connect_timeout 10s;
  159. }
  160. # HTML 文件处理(独立 location,修复嵌套问题)
  161. location ~* \.html$ {
  162. root /root/Lyrria/frontend/build;
  163. try_files $uri =404;
  164. # 处理 OPTIONS 预检请求
  165. if ($request_method = 'OPTIONS') {
  166. add_header Access-Control-Allow-Origin '*' always;
  167. add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS' always;
  168. add_header Access-Control-Allow-Headers 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;
  169. add_header Access-Control-Max-Age 1728000 always;
  170. add_header Content-Type 'text/plain; charset=utf-8' always;
  171. add_header Content-Length 0 always;
  172. return 204;
  173. }
  174. # HTML 文件不缓存
  175. add_header Cache-Control "no-cache, no-store, must-revalidate" always;
  176. add_header Pragma "no-cache" always;
  177. add_header Expires "0" always;
  178. # 安全头设置
  179. add_header X-Frame-Options "SAMEORIGIN" always;
  180. add_header X-Content-Type-Options "nosniff" always;
  181. add_header X-XSS-Protection "1; mode=block" always;
  182. add_header Referrer-Policy "strict-origin-when-cross-origin" always;
  183. # CORS 响应头
  184. add_header Access-Control-Allow-Origin '*' always;
  185. add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS' always;
  186. add_header Access-Control-Allow-Headers 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;
  187. }
  188. # 静态资源缓存配置(支持跨域)
  189. location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot|webp|avif)$ {
  190. # 处理 OPTIONS 预检请求
  191. if ($request_method = 'OPTIONS') {
  192. add_header Access-Control-Allow-Origin '*' always;
  193. add_header Access-Control-Allow-Methods 'GET, OPTIONS' always;
  194. add_header Access-Control-Allow-Headers 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;
  195. add_header Access-Control-Max-Age 1728000 always;
  196. add_header Content-Type 'text/plain; charset=utf-8' always;
  197. add_header Content-Length 0 always;
  198. return 204;
  199. }
  200. root /root/Lyrria/frontend/build;
  201. expires 1y;
  202. add_header Cache-Control "public, max-age=31536000, immutable" always;
  203. add_header X-Content-Type-Options nosniff always;
  204. # CORS 响应头
  205. add_header Access-Control-Allow-Origin '*' always;
  206. add_header Access-Control-Allow-Methods 'GET, OPTIONS' always;
  207. }
  208. # 前端静态文件服务(SPA 路由)
  209. location / {
  210. # 处理 OPTIONS 预检请求
  211. if ($request_method = 'OPTIONS') {
  212. add_header Access-Control-Allow-Origin '*' always;
  213. add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS' always;
  214. add_header Access-Control-Allow-Headers 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;
  215. add_header Access-Control-Max-Age 1728000 always;
  216. add_header Content-Type 'text/plain; charset=utf-8' always;
  217. add_header Content-Length 0 always;
  218. return 204;
  219. }
  220. root /root/Lyrria/frontend/build;
  221. try_files $uri $uri/ /index.html;
  222. # 安全头设置
  223. add_header X-Frame-Options "SAMEORIGIN" always;
  224. add_header X-Content-Type-Options "nosniff" always;
  225. add_header X-XSS-Protection "1; mode=block" always;
  226. add_header Referrer-Policy "strict-origin-when-cross-origin" always;
  227. # CORS 响应头
  228. add_header Access-Control-Allow-Origin '*' always;
  229. add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS' always;
  230. add_header Access-Control-Allow-Headers 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;
  231. }
  232. }
  233. }