nginx.conf 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. worker_processes auto; # 自动根据CPU核心数设置
  2. events {
  3. worker_connections 1024;
  4. multi_accept on;
  5. use epoll;
  6. }
  7. http {
  8. include /etc/nginx/mime.types;
  9. default_type application/octet-stream;
  10. # 基础优化配置
  11. sendfile on;
  12. tcp_nopush on;
  13. tcp_nodelay on;
  14. keepalive_timeout 65;
  15. types_hash_max_size 2048;
  16. server_tokens off; # 隐藏版本号
  17. # 性能优化配置
  18. client_max_body_size 20m;
  19. client_body_buffer_size 128k;
  20. proxy_buffer_size 4k;
  21. proxy_buffers 4 32k;
  22. proxy_busy_buffers_size 64k;
  23. # 全局 gzip 配置
  24. gzip on;
  25. gzip_vary on;
  26. gzip_min_length 10240;
  27. gzip_proxied expired no-cache no-store private auth;
  28. gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml application/javascript;
  29. gzip_disable "MSIE [1-6]\.";
  30. # 日志配置优化
  31. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  32. '$status $body_bytes_sent "$http_referer" '
  33. '"$http_user_agent" "$http_x_forwarded_for"';
  34. access_log /var/log/nginx/access.log main buffer=512k flush=1m;
  35. error_log /var/log/nginx/error.log warn;
  36. # 允许跨域访问
  37. map $http_origin $cors_origin {
  38. default "";
  39. "~^https?://[^/]+\.hht\.test(:[0-9]+)?$" "$http_origin";
  40. "~^https?://localhost(:[0-9]+)?$" "$http_origin";
  41. }
  42. # 通用安全头部配置
  43. map $http_upgrade $connection_upgrade {
  44. default upgrade;
  45. '' close;
  46. }
  47. # 通用配置块
  48. include /etc/nginx/conf.d/*.conf;
  49. # 通用安全头部
  50. add_header X-Frame-Options "SAMEORIGIN" always;
  51. add_header X-XSS-Protection "1; mode=block" always;
  52. add_header X-Content-Type-Options "nosniff" always;
  53. add_header Referrer-Policy "no-referrer-when-downgrade" always;
  54. add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always;
  55. add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
  56. add_header Permissions-Policy "geolocation=(), microphone=(), camera=()" always;
  57. # 错误页面配置
  58. error_page 404 /404.html;
  59. error_page 500 502 503 504 /50x.html;
  60. # Element2 子域名配置
  61. server {
  62. listen 80;
  63. server_name element2.hht.test;
  64. access_log /var/log/nginx/element2.access.log main;
  65. error_log /var/log/nginx/element2.error.log warn;
  66. location / {
  67. root /usr/share/nginx/html/element2;
  68. index index.html index.htm;
  69. try_files $uri $uri/ /index.html;
  70. # 针对不同类型的文件设置不同的缓存时间
  71. location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
  72. expires 7d;
  73. add_header Cache-Control "public, no-transform";
  74. }
  75. location ~* \.(html|htm)$ {
  76. expires 1h;
  77. add_header Cache-Control "public, no-transform";
  78. }
  79. # 跨域支持
  80. add_header 'Access-Control-Allow-Origin' $cors_origin always;
  81. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
  82. add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;
  83. add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
  84. }
  85. }
  86. # Vue2 子域名配置
  87. server {
  88. listen 80;
  89. server_name vue2.hht.test;
  90. access_log /var/log/nginx/vue2.access.log main;
  91. error_log /var/log/nginx/vue2.error.log warn;
  92. location / {
  93. alias /usr/share/nginx/html/v2.cn.vuejs.org/;
  94. index index.html index.htm;
  95. try_files $uri $uri/ /index.html;
  96. # 针对不同类型的文件设置不同的缓存时间
  97. location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
  98. expires 7d;
  99. add_header Cache-Control "public, no-transform";
  100. }
  101. location ~* \.(html|htm)$ {
  102. expires 1h;
  103. add_header Cache-Control "public, no-transform";
  104. }
  105. # 跨域支持
  106. add_header 'Access-Control-Allow-Origin' $cors_origin always;
  107. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
  108. add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;
  109. add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
  110. }
  111. }
  112. # Vant 子域名配置
  113. server {
  114. listen 80;
  115. server_name vant.hht.test;
  116. access_log /var/log/nginx/vant.access.log main;
  117. error_log /var/log/nginx/vant.error.log warn;
  118. location / {
  119. alias /usr/share/nginx/html/vant/;
  120. index index.html index.htm;
  121. try_files $uri $uri/ /index.html;
  122. # 针对不同类型的文件设置不同的缓存时间
  123. location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
  124. expires 7d;
  125. add_header Cache-Control "public, no-transform";
  126. }
  127. location ~* \.(html|htm)$ {
  128. expires 1h;
  129. add_header Cache-Control "public, no-transform";
  130. }
  131. # 跨域支持
  132. add_header 'Access-Control-Allow-Origin' $cors_origin always;
  133. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
  134. add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;
  135. add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
  136. }
  137. location /vant/ {
  138. alias /usr/share/nginx/html/vant/;
  139. try_files $uri $uri/ /tparking/index.html;
  140. index index.html;
  141. # 跨域支持
  142. add_header 'Access-Control-Allow-Origin' $cors_origin always;
  143. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
  144. add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;
  145. add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
  146. }
  147. }
  148. # Vue3 子域名配置
  149. server {
  150. listen 80;
  151. listen 8877; # 添加 8877 端口监听
  152. server_name vue3.hht.test;
  153. # 日志配置
  154. access_log /var/log/nginx/vue3.access.log main buffer=32k flush=5s;
  155. error_log /var/log/nginx/vue3.error.log warn;
  156. # 安全相关配置
  157. add_header X-Frame-Options "SAMEORIGIN" always;
  158. add_header X-XSS-Protection "1; mode=block" always;
  159. add_header X-Content-Type-Options "nosniff" always;
  160. # 性能优化
  161. client_max_body_size 10m;
  162. client_body_timeout 12;
  163. client_header_timeout 12;
  164. keepalive_timeout 15;
  165. send_timeout 10;
  166. # 字体文件 MIME 类型
  167. include /etc/nginx/mime.types;
  168. types {
  169. font/woff2 woff2;
  170. font/woff woff;
  171. font/ttf ttf;
  172. font/eot eot;
  173. }
  174. location / {
  175. alias /usr/share/nginx/html/docs-zh-cn/.vitepress/dist/;
  176. index index.html index.htm;
  177. try_files $uri $uri/ /index.html;
  178. # 针对不同类型的文件设置不同的缓存时间
  179. location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
  180. expires 7d;
  181. add_header Cache-Control "public, no-transform";
  182. access_log off;
  183. }
  184. location ~* \.(html|htm)$ {
  185. expires 1h;
  186. add_header Cache-Control "public, no-transform";
  187. }
  188. # 禁止访问隐藏文件
  189. location ~ /\. {
  190. deny all;
  191. access_log off;
  192. log_not_found off;
  193. }
  194. # 禁止访问 .git 目录
  195. location ~ /\.git {
  196. deny all;
  197. access_log off;
  198. log_not_found off;
  199. }
  200. }
  201. # 静态资源缓存优化
  202. location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
  203. root /usr/share/nginx/html/docs-zh-cn/.vitepress/dist;
  204. expires 7d;
  205. add_header Cache-Control "public, no-transform";
  206. access_log off;
  207. tcp_nodelay off;
  208. open_file_cache max=1000 inactive=20s;
  209. open_file_cache_valid 30s;
  210. open_file_cache_min_uses 2;
  211. open_file_cache_errors on;
  212. try_files $uri =404;
  213. }
  214. # 错误页面配置
  215. error_page 404 /404.html;
  216. error_page 500 502 503 504 /50x.html;
  217. location = /404.html {
  218. internal;
  219. }
  220. location = /50x.html {
  221. internal;
  222. }
  223. }
  224. # 2x ant-design 子域名配置
  225. server {
  226. listen 80;
  227. server_name 2x-ant-design.hht.test;
  228. access_log /var/log/nginx/2x-ant-design.access.log main;
  229. error_log /var/log/nginx/2x-ant-design.error.log warn;
  230. location / {
  231. root /usr/share/nginx/html/2x.ant.design;
  232. index index.html index.htm;
  233. try_files $uri $uri/ @router;
  234. # 针对不同类型的文件设置不同的缓存时间
  235. location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
  236. expires 7d;
  237. add_header Cache-Control "public, no-transform";
  238. }
  239. location ~* \.(html|htm)$ {
  240. expires 1h;
  241. add_header Cache-Control "public, no-transform";
  242. }
  243. # 跨域支持
  244. add_header 'Access-Control-Allow-Origin' $cors_origin always;
  245. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
  246. add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;
  247. add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
  248. }
  249. location @router {
  250. rewrite ^.*$ /index.html last;
  251. }
  252. }
  253. # 3x ant-design 子域名配置
  254. server {
  255. listen 80;
  256. server_name 3x-ant-design.hht.test;
  257. access_log /var/log/nginx/3x-ant-design.access.log main;
  258. error_log /var/log/nginx/3x-ant-design.error.log warn;
  259. location / {
  260. root /usr/share/nginx/html/3x.ant.design;
  261. index index.html index.htm;
  262. try_files $uri $uri/ @router;
  263. # 针对不同类型的文件设置不同的缓存时间
  264. location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
  265. expires 7d;
  266. add_header Cache-Control "public, no-transform";
  267. }
  268. location ~* \.(html|htm)$ {
  269. expires 1h;
  270. add_header Cache-Control "public, no-transform";
  271. }
  272. # 跨域支持
  273. add_header 'Access-Control-Allow-Origin' $cors_origin always;
  274. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
  275. add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;
  276. add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
  277. }
  278. location @router {
  279. rewrite ^.*$ /index.html last;
  280. }
  281. }
  282. # 4x ant-design 子域名配置
  283. server {
  284. listen 80;
  285. server_name 4x-ant-design.hht.test;
  286. # 日志配置
  287. access_log /var/log/nginx/4x-ant-design.access.log main buffer=32k flush=5s;
  288. error_log /var/log/nginx/4x-ant-design.error.log warn;
  289. # 安全相关配置
  290. add_header X-Frame-Options "SAMEORIGIN" always;
  291. add_header X-XSS-Protection "1; mode=block" always;
  292. add_header X-Content-Type-Options "nosniff" always;
  293. # 性能优化
  294. client_max_body_size 10m;
  295. client_body_timeout 12;
  296. client_header_timeout 12;
  297. keepalive_timeout 15;
  298. send_timeout 10;
  299. # 字体文件 MIME 类型
  300. include /etc/nginx/mime.types;
  301. types {
  302. font/woff2 woff2;
  303. font/woff woff;
  304. font/ttf ttf;
  305. font/eot eot;
  306. }
  307. location / {
  308. root /usr/share/nginx/html/4x.ant.design;
  309. index index.html index.htm;
  310. try_files $uri $uri/ @router;
  311. # 针对不同类型的文件设置不同的缓存时间
  312. location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
  313. expires 7d;
  314. add_header Cache-Control "public, no-transform";
  315. access_log off;
  316. }
  317. location ~* \.(html|htm)$ {
  318. expires 1h;
  319. add_header Cache-Control "public, no-transform";
  320. }
  321. # 禁止访问隐藏文件
  322. location ~ /\. {
  323. deny all;
  324. access_log off;
  325. log_not_found off;
  326. }
  327. # 禁止访问 .git 目录
  328. location ~ /\.git {
  329. deny all;
  330. access_log off;
  331. log_not_found off;
  332. }
  333. # 跨域支持
  334. add_header 'Access-Control-Allow-Origin' $cors_origin always;
  335. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
  336. add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;
  337. add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
  338. }
  339. # 静态资源缓存优化
  340. location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
  341. root /usr/share/nginx/html/4x-ant-design;
  342. expires 7d;
  343. add_header Cache-Control "public, no-transform";
  344. access_log off;
  345. tcp_nodelay off;
  346. open_file_cache max=1000 inactive=20s;
  347. open_file_cache_valid 30s;
  348. open_file_cache_min_uses 2;
  349. open_file_cache_errors on;
  350. try_files $uri =404;
  351. }
  352. location @router {
  353. rewrite ^.*$ /index.html last;
  354. }
  355. # 错误页面配置
  356. error_page 404 /404.html;
  357. error_page 500 502 503 504 /50x.html;
  358. location = /404.html {
  359. internal;
  360. }
  361. location = /50x.html {
  362. internal;
  363. }
  364. }
  365. # ant-design 子域名配置
  366. server {
  367. listen 80;
  368. server_name ant-design.hht.test;
  369. access_log /var/log/nginx/ant-design.access.log main;
  370. error_log /var/log/nginx/ant-design.error.log warn;
  371. location / {
  372. root /usr/share/nginx/html/ant-design;
  373. index index.html index.htm;
  374. try_files $uri $uri/ @router;
  375. # 针对不同类型的文件设置不同的缓存时间
  376. location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
  377. expires 7d;
  378. add_header Cache-Control "public, no-transform";
  379. }
  380. location ~* \.(html|htm)$ {
  381. expires 1h;
  382. add_header Cache-Control "public, no-transform";
  383. }
  384. # 跨域支持
  385. add_header 'Access-Control-Allow-Origin' $cors_origin always;
  386. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
  387. add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;
  388. add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
  389. }
  390. location @router {
  391. rewrite ^.*$ /index.html last;
  392. }
  393. }
  394. # ant-design-vue 子域名配置
  395. server {
  396. listen 80;
  397. server_name ant-design-vue.hht.test;
  398. access_log /var/log/nginx/ant-design-vue.access.log main;
  399. error_log /var/log/nginx/ant-design-vue.error.log warn;
  400. location / {
  401. root /usr/share/nginx/html/ant-design-vue;
  402. index index.html index.htm;
  403. try_files $uri $uri/ @router;
  404. # 针对不同类型的文件设置不同的缓存时间
  405. location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
  406. expires 7d;
  407. add_header Cache-Control "public, no-transform";
  408. }
  409. location ~* \.(html|htm)$ {
  410. expires 1h;
  411. add_header Cache-Control "public, no-transform";
  412. }
  413. # 跨域支持
  414. add_header 'Access-Control-Allow-Origin' $cors_origin always;
  415. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
  416. add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;
  417. add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
  418. }
  419. location @router {
  420. rewrite ^.*$ /index.html last;
  421. }
  422. }
  423. # better-scroll 子域名配置
  424. server {
  425. listen 80;
  426. server_name better-scroll-docs.hht.test;
  427. location / {
  428. #root /Users/sysadmin/code/vue_project/better-scroll-docs/zh-CN;
  429. #index index.html index.htm;
  430. alias /usr/share/nginx/html/better-scroll-docs/zh-CN/;
  431. try_files $uri $uri/ /index.html;
  432. index index.html;
  433. add_header Access-Control-Allow-Origin *;
  434. }
  435. location /docs/ {
  436. alias /usr/share/nginx/html/better-scroll-docs/;
  437. #try_files $uri $uri/ /tparking/index.html;
  438. #index index.html;
  439. #add_header Access-Control-Allow-Origin *;
  440. }
  441. }
  442. # 默认服务器配置
  443. server {
  444. listen 80 default_server;
  445. server_name _;
  446. return 404;
  447. }
  448. }