nginx.conf 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. user nginx;
  2. worker_processes auto;
  3. error_log /var/log/nginx/error.log warn;
  4. pid /var/run/nginx.pid;
  5. events {
  6. worker_connections 1024;
  7. use epoll;
  8. multi_accept on;
  9. }
  10. http {
  11. include /etc/nginx/mime.types;
  12. default_type application/octet-stream;
  13. # 日志格式
  14. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  15. '$status $body_bytes_sent "$http_referer" '
  16. '"$http_user_agent" "$http_x_forwarded_for"';
  17. access_log /var/log/nginx/access.log main;
  18. # 基本配置
  19. sendfile on;
  20. tcp_nopush on;
  21. tcp_nodelay on;
  22. keepalive_timeout 65;
  23. types_hash_max_size 2048;
  24. client_max_body_size 100m;
  25. # Gzip 压缩
  26. gzip on;
  27. gzip_vary on;
  28. gzip_proxied any;
  29. gzip_comp_level 6;
  30. gzip_types
  31. text/plain
  32. text/css
  33. text/xml
  34. text/javascript
  35. application/json
  36. application/javascript
  37. application/xml+rss
  38. application/atom+xml
  39. image/svg+xml;
  40. # 上游后端服务器
  41. upstream backend {
  42. server localhost:6666;
  43. }
  44. server {
  45. listen 80;
  46. server_name localhost;
  47. root /app/web/dist;
  48. index index.html;
  49. # 静态资源缓存
  50. location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
  51. expires 1y;
  52. add_header Cache-Control "public, immutable";
  53. add_header Access-Control-Allow-Origin "*";
  54. }
  55. # API 代理到后端
  56. location /api {
  57. proxy_pass http://backend;
  58. proxy_http_version 1.1;
  59. proxy_set_header Upgrade $http_upgrade;
  60. proxy_set_header Connection 'upgrade';
  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_cache_bypass $http_upgrade;
  66. proxy_connect_timeout 30s;
  67. proxy_send_timeout 30s;
  68. proxy_read_timeout 30s;
  69. }
  70. # Swagger 文档代理
  71. location /docs {
  72. proxy_pass http://backend;
  73. proxy_http_version 1.1;
  74. proxy_set_header Upgrade $http_upgrade;
  75. proxy_set_header Connection 'upgrade';
  76. proxy_set_header Host $host;
  77. proxy_set_header X-Real-IP $remote_addr;
  78. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  79. proxy_set_header X-Forwarded-Proto $scheme;
  80. proxy_cache_bypass $http_upgrade;
  81. }
  82. # 静态文件代理(上传的文件)
  83. location /static {
  84. proxy_pass http://backend;
  85. proxy_http_version 1.1;
  86. proxy_set_header Host $host;
  87. proxy_set_header X-Real-IP $remote_addr;
  88. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  89. proxy_set_header X-Forwarded-Proto $scheme;
  90. }
  91. # 健康检查
  92. location /health {
  93. proxy_pass http://backend;
  94. proxy_http_version 1.1;
  95. proxy_set_header Host $host;
  96. proxy_set_header X-Real-IP $remote_addr;
  97. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  98. proxy_set_header X-Forwarded-Proto $scheme;
  99. }
  100. # Vue Router 历史模式支持
  101. location / {
  102. try_files $uri $uri/ /index.html;
  103. add_header Cache-Control "no-store, no-cache, must-revalidate";
  104. }
  105. # 安全头
  106. add_header X-Frame-Options "SAMEORIGIN" always;
  107. add_header X-XSS-Protection "1; mode=block" always;
  108. add_header X-Content-Type-Options "nosniff" always;
  109. add_header Referrer-Policy "no-referrer-when-downgrade" always;
  110. add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always;
  111. # 错误页面
  112. error_page 404 /index.html;
  113. error_page 500 502 503 504 /50x.html;
  114. location = /50x.html {
  115. root /usr/share/nginx/html;
  116. }
  117. }
  118. }