Nessuna descrizione

nginx.conf 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # 配置工作进程数,通常设置为 CPU 核心数
  2. worker_processes auto;
  3. # 错误日志配置
  4. error_log /var/log/nginx/error.log warn;
  5. pid /var/run/nginx.pid;
  6. events {
  7. worker_connections 1024;
  8. # 开启多路复用
  9. use epoll;
  10. }
  11. # 文件描述符限制 - 移到这里,在http块之前
  12. worker_rlimit_nofile 65535;
  13. http {
  14. # 日志格式定义
  15. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  16. '$status $body_bytes_sent "$http_referer" '
  17. '"$http_user_agent" "$http_x_forwarded_for"';
  18. # 访问日志配置
  19. access_log /var/log/nginx/access.log main;
  20. # 高效文件传输设置
  21. sendfile on;
  22. tcp_nopush on;
  23. tcp_nodelay on;
  24. # 连接超时设置
  25. keepalive_timeout 65;
  26. keepalive_requests 100;
  27. # gzip 压缩优化
  28. gzip on;
  29. gzip_vary on;
  30. gzip_comp_level 6;
  31. gzip_min_length 1000;
  32. gzip_buffers 16 8k;
  33. gzip_http_version 1.1;
  34. # 增加更多文件类型
  35. gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon;
  36. # 全局设置
  37. # 合理限制请求体大小,根据实际需求调整
  38. client_max_body_size 10m;
  39. client_body_buffer_size 128k;
  40. client_header_timeout 60s;
  41. client_body_timeout 60s;
  42. server {
  43. listen 80;
  44. server_name _;
  45. gunzip on;
  46. gzip_static always;
  47. include /etc/nginx/mime.types;
  48. absolute_redirect off;
  49. root /usr/share/nginx/html;
  50. # 安全相关响应头
  51. add_header X-Frame-Options SAMEORIGIN;
  52. add_header X-XSS-Protection "1; mode=block";
  53. add_header X-Content-Type-Options nosniff;
  54. # 根据实际情况调整 CSP
  55. # add_header Content-Security-Policy "default-src 'self'";
  56. # 处理 SPA 应用路由
  57. location / {
  58. try_files $uri $uri/ /index.html;
  59. index index.html index.htm;
  60. }
  61. # HTML 和 JSON 文件 - 短缓存策略
  62. location ~ .*\.(html|json)$ {
  63. add_header Cache-Control "public, max-age=300, must-revalidate";
  64. }
  65. # 静态资源 - 长缓存策略
  66. location ~ .*\.(jpg|jpeg|png|gif|bmp|webp|svg|ico|ttf|woff|woff2|eot|mp4|mp3|swf)$ {
  67. add_header Cache-Control "public, max-age=31536000, immutable";
  68. expires 365d;
  69. access_log off;
  70. }
  71. # JS 和 CSS - 带版本号的长缓存
  72. location ~ .*\.(js|css)$ {
  73. add_header Cache-Control "public, max-age=31536000, immutable";
  74. expires 365d;
  75. access_log off;
  76. }
  77. # 接口转发 - 替换为实际后端地址
  78. # location ^~ /fg-api {
  79. # proxy_http_version 1.1;
  80. # proxy_set_header X-Real-IP $remote_addr;
  81. # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  82. # proxy_set_header X-Forwarded-Proto $scheme;
  83. # proxy_set_header Host $host;
  84. # # 后端是HTTPS时的必要配置
  85. # proxy_ssl_server_name on;
  86. # proxy_ssl_protocols TLSv1.2 TLSv1.3;
  87. # proxy_ssl_session_reuse on;
  88. # # 对于生产环境,应该尽量使用有效的证书而不是依赖``proxy_ssl_verify off;`` ,因为这会带来安全风险
  89. # proxy_ssl_verify off;
  90. # # TODO:替换为实际后端服务地址
  91. # # 注意在URL末尾添加了斜杠,这样Nginx会去掉 /fg-api 前缀
  92. # # 前端请求 http://your-domain.com/fg-api/users 转发到 https://ukw0y1.laf.run/users
  93. # proxy_pass https://ukw0y1.laf.run/;
  94. # # 上面一行的效果与下面2行一样的效果,都是为了去掉 /fg-api 前缀
  95. # # 显式移除/fg-api前缀
  96. # # rewrite ^/fg-api(.*)$ $1 break;
  97. # # 域名末尾不需要斜杠了
  98. # # proxy_pass https://ukw0y1.laf.run;
  99. # proxy_connect_timeout 60s;
  100. # proxy_send_timeout 60s;
  101. # proxy_read_timeout 60s;
  102. # proxy_buffers 8 32k;
  103. # proxy_buffer_size 64k;
  104. # proxy_busy_buffers_size 128k;
  105. # proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
  106. # }
  107. # 错误页面配置
  108. error_page 404 /index.html;
  109. error_page 500 502 503 504 /50x.html;
  110. location = /50x.html {
  111. root /usr/share/nginx/html;
  112. }
  113. # 禁止访问隐藏文件
  114. location ~ /\. {
  115. deny all;
  116. access_log off;
  117. log_not_found off;
  118. }
  119. }
  120. }