nginx.conf 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # 全局配置
  2. user nginx;
  3. worker_processes auto; # 自动根据 CPU 核心数设置 worker 进程数
  4. error_log /var/log/nginx/error.log warn;
  5. pid /var/run/nginx.pid;
  6. # 事件模块配置
  7. events {
  8. worker_connections 1024; # 每个 worker 进程的最大连接数
  9. }
  10. # HTTP 服务器配置
  11. http {
  12. include /etc/nginx/mime.types;
  13. default_type application/octet-stream;
  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. access_log /var/log/nginx/access.log main;
  19. # 性能优化
  20. sendfile on; # 高效文件传输模式
  21. tcp_nopush on; # 减少网络报文段数量
  22. keepalive_timeout 65; # 长连接超时时间(秒)
  23. gzip on; # 开启 Gzip 压缩
  24. gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
  25. # 反向代理配置
  26. upstream backend {
  27. server ruoyi_admin:18081; # 后端服务(K8s Service 名或 IP:Port)
  28. }
  29. # 虚拟主机配置(前端 + 反向代理 API)
  30. server {
  31. listen 80; # 监听端口
  32. server_name localhost; # 域名或 IP
  33. # 前端静态文件
  34. location / {
  35. root /usr/share/nginx/html; # Vue/React 打包后的 dist 目录
  36. index index.html;
  37. try_files $uri $uri/ /index.html; # 支持 Vue/React 前端路由
  38. }
  39. # 后端 API 代理
  40. location /api {
  41. proxy_pass http://backend; # 转发到后端服务
  42. proxy_set_header Host $host;
  43. proxy_set_header X-Real-IP $remote_addr;
  44. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  45. # 超时设置(可选)
  46. proxy_connect_timeout 60s;
  47. proxy_read_timeout 60s;
  48. }
  49. # 错误页面
  50. error_page 500 502 503 504 /50x.html;
  51. location = /50x.html {
  52. root /usr/share/nginx/html;
  53. }
  54. }
  55. }