nginx.conf 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. user root;
  2. worker_processes auto; # 自动匹配 CPU 核数
  3. events {
  4. worker_connections 8192; # 每进程最大连接数
  5. use epoll; # 使用高效事件模型
  6. multi_accept on; # 批量接受新连接
  7. }
  8. http {
  9. include mime.types;
  10. default_type application/octet-stream;
  11. # 启用gzip压缩
  12. gzip on;
  13. gzip_vary on;
  14. gzip_min_length 1024;
  15. gzip_proxied any;
  16. gzip_comp_level 6;
  17. gzip_types
  18. text/plain
  19. text/css
  20. text/js
  21. text/xml
  22. text/javascript
  23. application/javascript
  24. application/xml+rss
  25. application/json;
  26. # HTTP 端口 80 配置(支持 HTTP 访问)
  27. server {
  28. listen 80;
  29. server_name www.chengyantek.cn;
  30. # 处理favicon.ico请求
  31. location = /favicon.ico {
  32. root /root/Lyrria/frontend/dist;
  33. access_log off;
  34. log_not_found off;
  35. expires 1y;
  36. add_header Cache-Control "public, immutable";
  37. }
  38. # 处理robots.txt请求
  39. location = /robots.txt {
  40. access_log off;
  41. log_not_found off;
  42. return 200 "User-agent: *\nDisallow:\n";
  43. }
  44. # WebSocket连接代理(使用WS端口8766)
  45. location /ws {
  46. proxy_pass http://127.0.0.1:8766;
  47. proxy_http_version 1.1;
  48. proxy_set_header Upgrade $http_upgrade;
  49. proxy_set_header Connection "upgrade";
  50. proxy_set_header Host $host;
  51. proxy_set_header X-Real-IP $remote_addr;
  52. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  53. proxy_set_header X-Forwarded-Proto $scheme;
  54. proxy_read_timeout 3600s;
  55. proxy_send_timeout 3600s;
  56. proxy_connect_timeout 10s;
  57. }
  58. # API接口代理(如果有REST API需求)
  59. location ^~ /api/ {
  60. proxy_pass http://127.0.0.1:8766$request_uri;
  61. proxy_set_header Host $host;
  62. proxy_set_header X-Real-IP $remote_addr;
  63. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  64. proxy_set_header X-Forwarded-Proto $scheme;
  65. proxy_read_timeout 60s;
  66. proxy_send_timeout 60s;
  67. proxy_connect_timeout 10s;
  68. }
  69. # 客户端指标上报接口代理
  70. location = /metrics/client {
  71. proxy_pass http://127.0.0.1:8766/metrics/client;
  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 60s;
  77. proxy_send_timeout 60s;
  78. proxy_connect_timeout 10s;
  79. }
  80. # 静态资源缓存配置(支持跨域)
  81. location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
  82. # 处理 OPTIONS 预检请求
  83. if ($request_method = 'OPTIONS') {
  84. add_header Access-Control-Allow-Origin '*' always;
  85. add_header Access-Control-Allow-Methods 'GET, OPTIONS' always;
  86. add_header Access-Control-Allow-Headers 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;
  87. add_header Access-Control-Max-Age 1728000 always;
  88. add_header Content-Type 'text/plain; charset=utf-8' always;
  89. add_header Content-Length 0 always;
  90. return 204;
  91. }
  92. root /root/Lyrria/frontend/dist;
  93. expires 1y;
  94. add_header Cache-Control "public, immutable" always;
  95. add_header X-Content-Type-Options nosniff always;
  96. # CORS 响应头
  97. add_header Access-Control-Allow-Origin '*' always;
  98. add_header Access-Control-Allow-Methods 'GET, OPTIONS' always;
  99. }
  100. # 前端静态文件服务
  101. location / {
  102. # 处理 OPTIONS 预检请求
  103. if ($request_method = 'OPTIONS') {
  104. add_header Access-Control-Allow-Origin '*' always;
  105. add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS' always;
  106. add_header Access-Control-Allow-Headers 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;
  107. add_header Access-Control-Max-Age 1728000 always;
  108. add_header Content-Type 'text/plain; charset=utf-8' always;
  109. add_header Content-Length 0 always;
  110. return 204;
  111. }
  112. root /root/Lyrria/frontend/dist;
  113. try_files $uri $uri/ /index.html;
  114. # 安全头设置
  115. add_header X-Frame-Options "SAMEORIGIN" always;
  116. add_header X-Content-Type-Options "nosniff" always;
  117. add_header X-XSS-Protection "1; mode=block" always;
  118. add_header Referrer-Policy "strict-origin-when-cross-origin" always;
  119. # CORS 响应头
  120. add_header Access-Control-Allow-Origin '*' always;
  121. add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS' always;
  122. add_header Access-Control-Allow-Headers 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;
  123. # HTML文件不缓存
  124. location ~* \.html$ {
  125. add_header Cache-Control "no-cache, no-store, must-revalidate";
  126. add_header Pragma "no-cache";
  127. add_header Expires "0";
  128. }
  129. }
  130. }
  131. # HTTPS 端口 443 配置(支持 HTTPS 访问)
  132. server {
  133. listen 443 ssl;
  134. server_name www.chengyantek.cn;
  135. # SSL证书配置
  136. ssl_certificate "/root/.acme.sh/chengyantek.cn_ecc/fullchain.cer"; # SSL证书位置
  137. ssl_certificate_key "/root/.acme.sh/chengyantek.cn_ecc/chengyantek.cn.key"; # SSL私钥位置
  138. ssl_session_cache shared:SSL:10m;
  139. ssl_session_timeout 1h;
  140. ssl_protocols TLSv1.2 TLSv1.3;
  141. ssl_ciphers HIGH:!aNULL:!MD5;
  142. ssl_prefer_server_ciphers on;
  143. # 处理favicon.ico请求
  144. location = /favicon.ico {
  145. root /root/Lyrria/frontend/dist;
  146. access_log off;
  147. log_not_found off;
  148. expires 1y;
  149. add_header Cache-Control "public, immutable";
  150. }
  151. # 处理robots.txt请求
  152. location = /robots.txt {
  153. access_log off;
  154. log_not_found off;
  155. return 200 "User-agent: *\nDisallow:\n";
  156. }
  157. # WebSocket连接代理(使用WSS端口8766)
  158. location /ws {
  159. proxy_pass http://127.0.0.1:8766;
  160. proxy_http_version 1.1;
  161. proxy_set_header Upgrade $http_upgrade;
  162. proxy_set_header Connection "upgrade";
  163. proxy_set_header Host $host;
  164. proxy_set_header X-Real-IP $remote_addr;
  165. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  166. proxy_set_header X-Forwarded-Proto $scheme;
  167. proxy_read_timeout 3600s;
  168. proxy_send_timeout 3600s;
  169. proxy_connect_timeout 10s;
  170. }
  171. # API接口代理(如果有REST API需求)
  172. location ^~ /api/ {
  173. proxy_pass http://127.0.0.1:8766$request_uri;
  174. proxy_set_header Host $host;
  175. proxy_set_header X-Real-IP $remote_addr;
  176. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  177. proxy_set_header X-Forwarded-Proto $scheme;
  178. proxy_read_timeout 60s;
  179. proxy_send_timeout 60s;
  180. proxy_connect_timeout 10s;
  181. }
  182. # 客户端指标上报接口代理
  183. location = /metrics/client {
  184. proxy_pass http://127.0.0.1:8766/metrics/client;
  185. proxy_set_header Host $host;
  186. proxy_set_header X-Real-IP $remote_addr;
  187. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  188. proxy_set_header X-Forwarded-Proto $scheme;
  189. proxy_read_timeout 60s;
  190. proxy_send_timeout 60s;
  191. proxy_connect_timeout 10s;
  192. }
  193. # 静态资源缓存配置(支持跨域)
  194. location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
  195. # 处理 OPTIONS 预检请求
  196. if ($request_method = 'OPTIONS') {
  197. add_header Access-Control-Allow-Origin '*' always;
  198. add_header Access-Control-Allow-Methods 'GET, OPTIONS' always;
  199. add_header Access-Control-Allow-Headers 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;
  200. add_header Access-Control-Max-Age 1728000 always;
  201. add_header Content-Type 'text/plain; charset=utf-8' always;
  202. add_header Content-Length 0 always;
  203. return 204;
  204. }
  205. root /root/Lyrria/frontend/dist;
  206. expires 1y;
  207. add_header Cache-Control "public, immutable" always;
  208. add_header X-Content-Type-Options nosniff always;
  209. # CORS 响应头
  210. add_header Access-Control-Allow-Origin '*' always;
  211. add_header Access-Control-Allow-Methods 'GET, OPTIONS' always;
  212. }
  213. # 前端静态文件服务
  214. location / {
  215. # 处理 OPTIONS 预检请求
  216. if ($request_method = 'OPTIONS') {
  217. add_header Access-Control-Allow-Origin '*' always;
  218. add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS' always;
  219. add_header Access-Control-Allow-Headers 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;
  220. add_header Access-Control-Max-Age 1728000 always;
  221. add_header Content-Type 'text/plain; charset=utf-8' always;
  222. add_header Content-Length 0 always;
  223. return 204;
  224. }
  225. root /root/Lyrria/frontend/dist;
  226. try_files $uri $uri/ /index.html;
  227. # 安全头设置
  228. add_header X-Frame-Options "SAMEORIGIN" always;
  229. add_header X-Content-Type-Options "nosniff" always;
  230. add_header X-XSS-Protection "1; mode=block" always;
  231. add_header Referrer-Policy "strict-origin-when-cross-origin" always;
  232. # CORS 响应头
  233. add_header Access-Control-Allow-Origin '*' always;
  234. add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS' always;
  235. add_header Access-Control-Allow-Headers 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;
  236. # HTML文件不缓存
  237. location ~* \.html$ {
  238. add_header Cache-Control "no-cache, no-store, must-revalidate";
  239. add_header Pragma "no-cache";
  240. add_header Expires "0";
  241. }
  242. }
  243. }
  244. }