nginx.conf 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #user nobody;
  2. worker_processes 1;
  3. error_log logs/error.log debug;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;
  6. #pid logs/nginx.pid;
  7. events {
  8. worker_connections 1024;
  9. }
  10. http {
  11. include mime.types;
  12. default_type application/octet-stream;
  13. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  14. # '$status $body_bytes_sent "$http_referer" '
  15. # '"$http_user_agent" "$http_x_forwarded_for"';
  16. #access_log logs/access.log main;
  17. sendfile on;
  18. #tcp_nopush on;
  19. #keepalive_timeout 0;
  20. keepalive_timeout 65;
  21. # 全局优化
  22. client_max_body_size 100M; # 允许大文件上传
  23. # 压缩传输
  24. gzip on;
  25. server {
  26. listen 3443;
  27. server_name localhost;
  28. #charset koi8-r;
  29. #access_log logs/host.access.log main;
  30. # 指向构建后的静态文件目录(使用相对路径)
  31. location / {
  32. proxy_pass http://localhost:8080;
  33. proxy_set_header Host $host;
  34. proxy_set_header X-Real-IP $remote_addr;
  35. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  36. proxy_set_header X-Forwarded-Proto $scheme;
  37. }
  38. # 从 /auth 页面提取参数到变量
  39. location /auth {
  40. add_header Set-Cookie "auth_token=$arg_token; Path=/api/; HttpOnly";
  41. # 存储参数到变量(需确保前端页面和 API 在同一个请求会话中)
  42. set $auth_token $arg_token;
  43. proxy_pass http://localhost:8080/auth;
  44. }
  45. # 代理后端API请求(关键配置)
  46. location /api/ {
  47. proxy_pass http://localhost:8080/api/; # 后端服务地址
  48. # 连接后端超时(默认60秒)
  49. proxy_connect_timeout 60s;
  50. # 发送请求到后端的超时(默认60秒)
  51. proxy_send_timeout 600s;
  52. # 从后端读取响应的超时(默认60秒)
  53. proxy_read_timeout 600s;
  54. proxy_set_header Host $host;
  55. proxy_set_header X-Real-IP $remote_addr;
  56. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  57. # 将 Cookie 值作为请求头或查询参数传递
  58. proxy_set_header X-Trusted-Token $cookie_auth_token;
  59. }
  60. # 处理/ws路径的Socket.IO连接(需添加协议升级头)
  61. location /ws/ {
  62. proxy_pass http://localhost:8080;
  63. proxy_http_version 1.1;
  64. proxy_set_header Upgrade $http_upgrade;
  65. proxy_set_header Connection "Upgrade";
  66. proxy_set_header Host $host;
  67. proxy_set_header X-Real-IP $remote_addr;
  68. proxy_read_timeout 86400; # 保持长连接
  69. }
  70. # 其他路由代理(根据FastAPI挂载路径)
  71. location ~ ^/(ollama|openai|users|chats|models|files|retrieval)/ {
  72. proxy_pass http://localhost:8080;
  73. proxy_set_header Host $host;
  74. proxy_set_header X-Forwarded-Proto $scheme;
  75. }
  76. # 若静态文件通过FastAPI服务(如/static和/cache路径)
  77. location /static/ {
  78. proxy_pass http://localhost:8080/static/;
  79. expires 30d; # 缓存优化
  80. }
  81. location /cache/ {
  82. proxy_pass http://localhost:8080/cache/;
  83. expires 1h;
  84. }
  85. #error_page 404 /404.html;
  86. # redirect server error pages to the static page /50x.html
  87. #
  88. error_page 500 502 503 504 /50x.html;
  89. location = /50x.html {
  90. root html;
  91. }
  92. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  93. #
  94. #location ~ \.php$ {
  95. # proxy_pass http://127.0.0.1;
  96. #}
  97. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  98. #
  99. #location ~ \.php$ {
  100. # root html;
  101. # fastcgi_pass 127.0.0.1:9000;
  102. # fastcgi_index index.php;
  103. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  104. # include fastcgi_params;
  105. #}
  106. # deny access to .htaccess files, if Apache's document root
  107. # concurs with nginx's one
  108. #
  109. #location ~ /\.ht {
  110. # deny all;
  111. #}
  112. }
  113. # another virtual host using mix of IP-, name-, and port-based configuration
  114. #
  115. #server {
  116. # listen 8000;
  117. # listen somename:8080;
  118. # server_name somename alias another.alias;
  119. # location / {
  120. # root html;
  121. # index index.html index.htm;
  122. # }
  123. #}
  124. # HTTPS server
  125. #
  126. #server {
  127. # listen 443 ssl;
  128. # server_name localhost;
  129. # ssl_certificate cert.pem;
  130. # ssl_certificate_key cert.key;
  131. # ssl_session_cache shared:SSL:1m;
  132. # ssl_session_timeout 5m;
  133. # ssl_ciphers HIGH:!aNULL:!MD5;
  134. # ssl_prefer_server_ciphers on;
  135. # location / {
  136. # root html;
  137. # index index.html index.htm;
  138. # }
  139. #}
  140. }