Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python
5.4 KB
Raw Blame
# =====================================================================
# Web(User Web)nginx 配置模板
#
# 由 nginx 官方镜像 entrypoint 在启动时 envsubst 渲染:
#   /etc/nginx/templates/default.conf.template → /etc/nginx/conf.d/default.conf
# 环境变量与 web.template.yaml 注入的同名 env 一致(必须全部提供):
#   WEB_HTTP_PORT / GATEWAY_URL / GATEWAY_WEB_HTTP_URL /
#   USER_WEB_IDP_TARGET / USER_WEB_MANAGER_TARGET /
#   JIUWENSWARM_EDITION / LOGIN_AUTH_SIMULATE / WEB_TRANSPORT / WORKSPACE_QUOTA_ENABLED
#
# 语义对齐 jiuwenswarm/channels/web/app_web.py,已知差异:
#   1) /api 分流:/api/sessions*、/api/v1* → Gateway Web HTTP;其余 /api/* →
#      Gateway WebChannel(与 _is_web_http_route/_is_api_route 一致)
#   2) index.html 的运行时配置注入由 sub_filter 完成(占位符与 Python 版相同,
#      值来自部署期 env,部署期内固定;Python 版是逐请求注入,语义等价)
#   3) 非常规方法(如 POST /chat/* 类静态路径)会被转发而非 405
#   4) 上游 Service DNS 需在本容器启动时已可解析(nginx 启动时解析
#      proxy_pass 域名);部署工具先建 Service 后起 pod,天然满足
# =====================================================================

map $http_upgrade $connection_upgrade {
    default upgrade;
    ""      "";
}

server {
    listen ${WEB_HTTP_PORT};
    server_name _;

    # Python 版对请求体大小无限制(文件上传走 /file-api)
    client_max_body_size 0;

    # 上游不做压缩(中继语义:响应原文透传),出口压缩由 nginx 负责
    proxy_set_header Accept-Encoding "";
    proxy_http_version 1.1;
    proxy_connect_timeout 10s;
    proxy_send_timeout 30s;
    proxy_read_timeout 30s;

    root /app/jiuwenswarm/jiuwenswarm/channels/web/frontend/dist;
    index index.html;

    gzip on;
    gzip_vary on;
    gzip_types text/css application/javascript application/json image/svg+xml;

    # ---------- index.html 运行时配置注入(_inject_user_web_runtime_config) ----------
    sub_filter '__JIUWENSWARM_EDITION_VALUE__' '${JIUWENSWARM_EDITION}';
    sub_filter '__JIUWEN_LOGIN_AUTH_SIMULATE_VALUE__' '${LOGIN_AUTH_SIMULATE}';
    sub_filter '__JIUWEN_WEB_TRANSPORT_VALUE__' '${WEB_TRANSPORT}';
    sub_filter '__WORKSPACE_QUOTA_ENABLED_VALUE__' '${WORKSPACE_QUOTA_ENABLED}';
    sub_filter_once on;
    sub_filter_types text/html;

    # ---------- 可选自定义上游(/idp、/manager-api) ----------
    # 客户自定义认证中心/管理面的挂载点:由 entrypoint 脚本
    include /etc/nginx/optional-upstreams/*.conf;

    # ---------- Gateway Web HTTP 组(gateway-api/file-api/share-api/sessions/v1) ----------
    location = /gateway-api { return 307 /gateway-api/; }
    location ^~ /gateway-api/ {
        rewrite ^/gateway-api/(.*)$ /api/$1 break;
        proxy_pass ${GATEWAY_WEB_HTTP_URL};
        proxy_buffering off;
        proxy_cache off;
        proxy_read_timeout 3600s;
        proxy_send_timeout 3600s;
    }
    location ^~ /file-api/ {
        proxy_pass ${GATEWAY_WEB_HTTP_URL};
        proxy_read_timeout 300s;
        proxy_send_timeout 300s;
    }
    location ^~ /share-api/ {
        proxy_pass ${GATEWAY_WEB_HTTP_URL};
        proxy_read_timeout 300s;
        proxy_send_timeout 300s;
    }
    location ^~ /api/sessions {
        proxy_pass ${GATEWAY_WEB_HTTP_URL};
    }
    location = /api/v1  { proxy_pass ${GATEWAY_WEB_HTTP_URL}; }
    location ^~ /api/v1/ {
        proxy_pass ${GATEWAY_WEB_HTTP_URL};
    }

    # ---------- 其余 /api → Gateway WebChannel ----------
    location = /api { proxy_pass ${GATEWAY_URL}; }
    location /api/ {
        proxy_pass ${GATEWAY_URL};
        proxy_buffering off;
        proxy_read_timeout 3600s;
        proxy_send_timeout 3600s;
    }

    # ---------- /ws → Gateway WebChannel WebSocket(upgrade 隧道) ----------
    location = /ws {
        proxy_pass ${GATEWAY_URL};
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header Accept-Encoding "";
        proxy_read_timeout 3600s;
        proxy_send_timeout 3600s;
    }
    location /ws/ {
        proxy_pass ${GATEWAY_URL};
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header Accept-Encoding "";
        proxy_read_timeout 3600s;
        proxy_send_timeout 3600s;
    }

    # ---------- SPA 静态资源 ----------
    location = /index.html {
        add_header Cache-Control "no-cache, must-revalidate";
    }
    # SPA 子路径刷新的相对资源兼容(vite base:'./'):刷新 /chat/<会话> 时
    # 浏览器把 index.html 里的 ./assets/x.js 解析为 /chat/assets/x.js,
    # dist 下不存在。Python 版 app_web.py 对静态扩展名逐级剥前缀重试;
    # nginx 等价实现:剥掉第一段 SPA 路由前缀。仅匹配静态资源扩展名
    # (对齐 Python 版 static_exts);剥前缀后仍找不到 → 404 而非回落
    # index.html,避免 HTML 冒充 JS module 导致白屏。
    # 注:实际 vite 产物只有 assets/ 一层,剥一段即可;若未来 SPA 挂更深
    # 前缀需按需增加层级。
    location ~* ^/[^/]+(?<spa_asset>/assets/.+\.(?:js|mjs|css|map|svg|png|jpe?g|ico|webp|gif|woff2?|ttf|eot|json|webmanifest))$ {
        try_files $spa_asset =404;
    }
    location / {
        try_files $uri $uri/ /index.html;
        add_header Cache-Control "no-cache, must-revalidate";
    }
}