nginx.conf 13 KB

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