nginx.conf 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. events {
  2. worker_connections 1024;
  3. }
  4. http {
  5. include /etc/nginx/mime.types;
  6. default_type application/octet-stream;
  7. # 日志格式
  8. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  9. '$status $body_bytes_sent "$http_referer" '
  10. '"$http_user_agent" "$http_x_forwarded_for"';
  11. access_log /var/log/nginx/access.log main;
  12. error_log /var/log/nginx/error.log;
  13. # 基础配置
  14. sendfile on;
  15. tcp_nopush on;
  16. tcp_nodelay on;
  17. keepalive_timeout 65;
  18. types_hash_max_size 2048;
  19. # Gzip压缩
  20. gzip on;
  21. gzip_vary on;
  22. gzip_min_length 1024;
  23. gzip_proxied any;
  24. gzip_comp_level 6;
  25. gzip_types
  26. text/plain
  27. text/css
  28. text/xml
  29. text/javascript
  30. application/json
  31. application/javascript
  32. application/xml+rss
  33. application/atom+xml
  34. image/svg+xml;
  35. # 上游服务器配置
  36. upstream ruoyi_backend {
  37. # 轮询负载均衡
  38. server ruoyi-admin-9900:9900 weight=1 max_fails=3 fail_timeout=30s;
  39. server ruoyi-admin-9901:9901 weight=1 max_fails=3 fail_timeout=30s;
  40. server ruoyi-admin-9902:9902 weight=1 max_fails=3 fail_timeout=30s;
  41. server ruoyi-admin-9903:9903 weight=1 max_fails=3 fail_timeout=30s;
  42. # 健康检查
  43. keepalive 32;
  44. }
  45. # 主服务器配置
  46. server {
  47. listen 80;
  48. server_name localhost;
  49. # 静态资源缓存
  50. location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
  51. expires 1y;
  52. add_header Cache-Control "public, immutable";
  53. proxy_pass http://ruoyi_backend;
  54. }
  55. # API请求
  56. location / {
  57. proxy_pass http://ruoyi_backend;
  58. proxy_set_header Host $host;
  59. proxy_set_header X-Real-IP $remote_addr;
  60. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  61. proxy_set_header X-Forwarded-Proto $scheme;
  62. # 超时设置
  63. proxy_connect_timeout 30s;
  64. proxy_send_timeout 30s;
  65. proxy_read_timeout 30s;
  66. # 缓冲设置
  67. proxy_buffering on;
  68. proxy_buffer_size 4k;
  69. proxy_buffers 8 4k;
  70. proxy_busy_buffers_size 8k;
  71. }
  72. # 健康检查端点
  73. location /health {
  74. access_log off;
  75. return 200 "healthy\n";
  76. add_header Content-Type text/plain;
  77. }
  78. # 状态监控页面
  79. location /nginx_status {
  80. stub_status on;
  81. access_log off;
  82. allow 127.0.0.1;
  83. deny all;
  84. }
  85. }
  86. # 监控和日志配置
  87. server {
  88. listen 8080;
  89. server_name localhost;
  90. location / {
  91. return 200 "Nginx Load Balancer Status\n";
  92. add_header Content-Type text/plain;
  93. }
  94. location /status {
  95. stub_status on;
  96. access_log off;
  97. }
  98. }
  99. }