nginx.conf 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. # 启用gzip压缩
  11. gzip on;
  12. gzip_vary on;
  13. gzip_min_length 1024;
  14. gzip_proxied any;
  15. gzip_comp_level 6;
  16. gzip_types
  17. text/plain
  18. text/css
  19. text/js
  20. text/xml
  21. text/javascript
  22. application/javascript
  23. application/xml+rss
  24. application/json;
  25. # HTTP 端口 80 配置,自动重定向到 HTTPS
  26. server {
  27. listen 80;
  28. server_name www.hikelly.chat;
  29. # verification.html 可以直接通过HTTP访问
  30. location = /verification.html {
  31. root /root/Lyrria/frontend/build;
  32. try_files $uri =404;
  33. add_header Cache-Control "no-cache, no-store, must-revalidate";
  34. add_header Pragma "no-cache";
  35. add_header Expires "0";
  36. }
  37. # 其他所有路径重定向到HTTPS
  38. location / {
  39. return 301 https://$host$request_uri;
  40. }
  41. }
  42. server {
  43. listen 443 ssl;
  44. server_name www.hikelly.chat;
  45. ssl_certificate "/etc/nginx/cert/www.hikelly.chat.pem"; # SSL证书位置
  46. ssl_certificate_key "/etc/nginx/cert/www.hikelly.chat.key"; # SSL证书位置
  47. ssl_session_cache shared:SSL:10m;
  48. ssl_session_timeout 1h;
  49. ssl_protocols TLSv1.2 TLSv1.3;
  50. ssl_ciphers HIGH:!aNULL:!MD5;
  51. ssl_prefer_server_ciphers on;
  52. # 处理favicon.ico请求
  53. location = /favicon.ico {
  54. root /root/Lyrria/frontend/build;
  55. access_log off;
  56. log_not_found off;
  57. expires 1y;
  58. add_header Cache-Control "public, immutable";
  59. }
  60. # 处理robots.txt请求
  61. location = /robots.txt {
  62. access_log off;
  63. log_not_found off;
  64. return 200 "User-agent: *\nDisallow:\n";
  65. }
  66. # WebSocket连接代理(使用WSS端口8766)
  67. location /ws {
  68. proxy_pass http://127.0.0.1:8766;
  69. proxy_http_version 1.1;
  70. proxy_set_header Upgrade $http_upgrade;
  71. proxy_set_header Connection "upgrade";
  72. proxy_set_header Host $host;
  73. proxy_set_header X-Real-IP $remote_addr;
  74. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  75. proxy_set_header X-Forwarded-Proto $scheme;
  76. proxy_read_timeout 3600s;
  77. proxy_send_timeout 3600s;
  78. proxy_connect_timeout 10s;
  79. }
  80. # API接口代理(如果有REST API需求)
  81. location ^~ /api/ {
  82. proxy_pass http://127.0.0.1:8766/;
  83. proxy_set_header Host $host;
  84. proxy_set_header X-Real-IP $remote_addr;
  85. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  86. proxy_set_header X-Forwarded-Proto $scheme;
  87. proxy_read_timeout 60s;
  88. proxy_send_timeout 60s;
  89. proxy_connect_timeout 10s;
  90. }
  91. # 客户端指标上报接口代理
  92. location = /metrics/client {
  93. proxy_pass http://127.0.0.1:8766/metrics/client;
  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. proxy_read_timeout 60s;
  99. proxy_send_timeout 60s;
  100. proxy_connect_timeout 10s;
  101. }
  102. # 静态资源缓存配置
  103. location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
  104. root /root/Lyrria/frontend/build;
  105. expires 1y;
  106. add_header Cache-Control "public, immutable";
  107. add_header X-Content-Type-Options nosniff;
  108. }
  109. # 前端静态文件服务
  110. location / {
  111. root /root/Lyrria/frontend/build;
  112. try_files $uri $uri/ /index.html;
  113. # 安全头设置
  114. add_header X-Frame-Options "SAMEORIGIN" always;
  115. add_header X-Content-Type-Options "nosniff" always;
  116. add_header X-XSS-Protection "1; mode=block" always;
  117. add_header Referrer-Policy "strict-origin-when-cross-origin" always;
  118. # HTML文件不缓存
  119. location ~* \.html$ {
  120. add_header Cache-Control "no-cache, no-store, must-revalidate";
  121. add_header Pragma "no-cache";
  122. add_header Expires "0";
  123. }
  124. }
  125. }
  126. }