nginx-prod.conf 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. # ============================================================
  2. # 蜀道安全AI系统 - 生产环境 Nginx 配置
  3. # ============================================================
  4. # 服务端口说明:
  5. # - 22000: Nginx SSL 入口
  6. # - 22001: shudao-go-backend (系统后端)
  7. # - 28000: 管理后台 API
  8. # - 28002: ReportGenerator (AI对话服务)
  9. # - 28004: auth-server (统一认证网关,集成原28003~28006服务)
  10. # - 24000: ChromaDB (向量搜索)
  11. # - 172.16.35.50:8000: TTS/语音服务
  12. # ============================================================
  13. # ==================== 限流配置 ====================
  14. limit_req_zone $binary_remote_addr zone=limit_by_ip:10m rate=10r/s;
  15. limit_req_zone $binary_remote_addr$request_uri zone=limit_ip_uri:10m rate=10r/s;
  16. limit_req_zone $binary_remote_addr zone=limit_login:10m rate=5r/m;
  17. limit_conn_zone $binary_remote_addr zone=conn_by_ip:10m;
  18. limit_req_log_level warn;
  19. limit_req_status 429;
  20. limit_conn_log_level warn;
  21. limit_conn_status 429;
  22. server {
  23. listen 22000 ssl;
  24. server_name aqai.shudaodsj.com;
  25. ssl_certificate /usr/local/openresty/nginx/conf.d/shudaodsj.com.pem;
  26. ssl_certificate_key /usr/local/openresty/nginx/conf.d/shudaodsj.com.key;
  27. client_max_body_size 50M;
  28. charset utf-8;
  29. access_log /usr/local/openresty/nginx/logs/shudao_access.log;
  30. error_log /usr/local/openresty/nginx/logs/shudao_error.log info;
  31. # ==================== JWT 配置 ====================
  32. set $jwt_secret "your-secret-key-change-in-production-2024";
  33. set $jwt_algorithm "HS256";
  34. set $user_accountID "";
  35. set $user_name "";
  36. set $user_userCode "";
  37. set $user_contactNumber "";
  38. set $user_jti "";
  39. # ==================== 管理后台 ====================
  40. location /admin {
  41. alias /opt/www/shudao_backstage/dist;
  42. try_files $uri $uri/ /admin/index.html;
  43. }
  44. location /admin/api/v1 {
  45. limit_req zone=limit_ip_uri burst=20 nodelay;
  46. limit_conn conn_by_ip 20;
  47. proxy_pass http://127.0.0.1:28000;
  48. proxy_set_header Host $host;
  49. proxy_set_header X-Real-IP $remote_addr;
  50. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  51. proxy_set_header X-Forwarded-Proto $scheme;
  52. }
  53. # ==================== 认证网关 (auth-server:28004) ====================
  54. # /auth/api/xxx -> http://127.0.0.1:28004/api/xxx
  55. location /auth/ {
  56. limit_req zone=limit_ip_uri burst=20 nodelay;
  57. limit_conn conn_by_ip 20;
  58. proxy_pass http://127.0.0.1:28004/;
  59. proxy_set_header Host $host;
  60. proxy_set_header X-Real-IP $remote_addr;
  61. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  62. proxy_set_header X-Forwarded-Proto $scheme;
  63. }
  64. # ==================== AI对话服务 (ReportGenerator:28002) ====================
  65. # /chatwithai/api/v1/xxx -> http://127.0.0.1:28002/api/v1/xxx
  66. location /chatwithai/ {
  67. limit_req zone=limit_ip_uri burst=20 nodelay;
  68. limit_conn conn_by_ip 20;
  69. access_by_lua_file /usr/local/openresty/nginx/conf.d/jwt-auth.lua;
  70. proxy_pass http://127.0.0.1:28002/;
  71. proxy_set_header Host $host;
  72. proxy_set_header X-Real-IP $remote_addr;
  73. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  74. proxy_set_header X-Forwarded-Proto $scheme;
  75. proxy_set_header X-User-AccountID $user_accountID;
  76. proxy_set_header X-User-Name $user_name;
  77. proxy_set_header X-User-UserCode $user_userCode;
  78. proxy_set_header X-User-ContactNumber $user_contactNumber;
  79. proxy_set_header X-User-JTI $user_jti;
  80. # SSE 流式响应
  81. proxy_buffering off;
  82. proxy_cache off;
  83. proxy_http_version 1.1;
  84. proxy_read_timeout 3600s;
  85. proxy_send_timeout 3600s;
  86. }
  87. # ==================== 系统后端 (shudao-go-backend:22001) ====================
  88. # OSS解析接口(无需JWT)
  89. location /apiv1/oss/parse {
  90. limit_req zone=limit_ip_uri burst=20 nodelay;
  91. limit_conn conn_by_ip 20;
  92. proxy_pass http://127.0.0.1:22001;
  93. proxy_set_header Host $host;
  94. proxy_set_header X-Real-IP $remote_addr;
  95. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  96. proxy_set_header X-Forwarded-Proto $scheme;
  97. }
  98. # 推荐问题接口(无需JWT,首页加载时调用)
  99. location /apiv1/recommend_question {
  100. limit_req zone=limit_ip_uri burst=20 nodelay;
  101. limit_conn conn_by_ip 20;
  102. proxy_pass http://127.0.0.1:22001;
  103. proxy_set_header Host $host;
  104. proxy_set_header X-Real-IP $remote_addr;
  105. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  106. proxy_set_header X-Forwarded-Proto $scheme;
  107. }
  108. # 系统后端API(需JWT鉴权)
  109. location /apiv1 {
  110. limit_req zone=limit_ip_uri burst=20 nodelay;
  111. limit_conn conn_by_ip 20;
  112. access_by_lua_file /usr/local/openresty/nginx/conf.d/jwt-auth.lua;
  113. proxy_pass http://127.0.0.1:22001;
  114. proxy_set_header Host $host;
  115. proxy_set_header X-Real-IP $remote_addr;
  116. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  117. proxy_set_header X-Forwarded-Proto $scheme;
  118. proxy_set_header X-User-AccountID $user_accountID;
  119. proxy_set_header X-User-Name $user_name;
  120. proxy_set_header X-User-UserCode $user_userCode;
  121. proxy_set_header X-User-ContactNumber $user_contactNumber;
  122. proxy_set_header X-User-JTI $user_jti;
  123. }
  124. # ==================== TTS 语音合成 ====================
  125. location /tts/ {
  126. limit_req zone=limit_ip_uri burst=20 nodelay;
  127. limit_conn conn_by_ip 20;
  128. access_by_lua_file /usr/local/openresty/nginx/conf.d/jwt-auth.lua;
  129. proxy_pass http://172.16.35.50:8000/tts/;
  130. proxy_set_header Host $host;
  131. proxy_set_header X-Real-IP $remote_addr;
  132. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  133. proxy_set_header X-Forwarded-Proto $scheme;
  134. proxy_set_header X-User-AccountID $user_accountID;
  135. proxy_set_header X-User-Name $user_name;
  136. proxy_set_header X-User-UserCode $user_userCode;
  137. proxy_set_header X-User-ContactNumber $user_contactNumber;
  138. proxy_set_header X-User-JTI $user_jti;
  139. }
  140. # ==================== 语音转文字 ====================
  141. location /audio_to_text {
  142. limit_req zone=limit_ip_uri burst=20 nodelay;
  143. limit_conn conn_by_ip 20;
  144. access_by_lua_file /usr/local/openresty/nginx/conf.d/jwt-auth.lua;
  145. proxy_pass http://172.16.35.50:8000;
  146. proxy_set_header Host $host;
  147. proxy_set_header X-Real-IP $remote_addr;
  148. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  149. proxy_set_header X-Forwarded-Proto $scheme;
  150. proxy_set_header X-User-AccountID $user_accountID;
  151. proxy_set_header X-User-Name $user_name;
  152. proxy_set_header X-User-UserCode $user_userCode;
  153. proxy_set_header X-User-ContactNumber $user_contactNumber;
  154. proxy_set_header X-User-JTI $user_jti;
  155. }
  156. # ==================== 默认路由 (前端静态资源) ====================
  157. location / {
  158. proxy_pass http://127.0.0.1:22001;
  159. proxy_set_header Host $host;
  160. proxy_set_header X-Real-IP $remote_addr;
  161. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  162. proxy_set_header X-Forwarded-Proto $scheme;
  163. # SSE 流式响应
  164. proxy_buffering off;
  165. proxy_cache off;
  166. proxy_http_version 1.1;
  167. proxy_read_timeout 3600s;
  168. proxy_send_timeout 3600s;
  169. }
  170. # ==================== 错误页面 ====================
  171. error_page 429 /429.json;
  172. location = /429.json {
  173. internal;
  174. default_type application/json;
  175. return 429 '{"detail":"请求过于频繁,请稍后重试","code":"RATE_LIMIT_EXCEEDED","retry_after":60}';
  176. add_header Retry-After 60;
  177. }
  178. error_page 404 /404.json;
  179. location = /404.json {
  180. internal;
  181. default_type application/json;
  182. return 404 '{"detail":"接口不存在"}';
  183. }
  184. error_page 400 /400.json;
  185. location = /400.json {
  186. internal;
  187. default_type application/json;
  188. return 400 '{"detail":"请求格式不正确"}';
  189. }
  190. error_page 500 502 503 504 /50x.json;
  191. location = /50x.json {
  192. internal;
  193. default_type application/json;
  194. return 500 '{"detail":"服务器内部错误"}';
  195. }
  196. }