You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Nginx中实现访问/chat端点时跳转至指定build文件夹提供服务

Nginx 配置方案

配置说明

直接在你现有Nginx的server块中添加对应规则即可,以下是两种常见场景的可用配置:


场景1:访问/chat时地址栏URL保持不变,直接加载build目录产物

最常用的场景,用户无感知跳转,直接返回对应静态文件:

server {
    # 保留你原有server块的基础配置,比如监听端口、域名等,示例如下:
    listen 80;
    server_name 替换为你的实际域名或服务器IP;

    location ^~ /chat {
        alias /home/ubuntu/app/build;
        index index.html index.htm;
        # 如果是React/Vue等单页应用(SPA),新增下面一行处理前端路由
        try_files $uri $uri/ /chat/index.html;
    }

    # 你原有其他location规则正常保留即可
}
  • 这里选用alias而非root的原因:root会自动把location匹配的路径拼接到根目录后,会去/home/ubuntu/app/build/chat路径下找文件,不符合你的需求;alias会直接用指定路径替换匹配的路径,直接读取/home/ubuntu/app/build下的文件。

场景2:访问/chat时302跳转至build目录的根服务路径

如果需要用户访问/chat时地址栏直接变为build服务的根地址,用以下配置:

server {
    listen 80;
    server_name 替换为你的实际域名或服务器IP;

    # 配置build目录的正常根访问规则
    location / {
        root /home/ubuntu/app/build;
        index index.html index.htm;
        # SPA单页应用新增下面一行处理路由
        try_files $uri $uri/ /index.html;
    }

    # 配置/chat的跳转规则
    location = /chat {
        return 302 /;
    }
}

生效步骤

  1. 保存修改后的配置文件
  2. 执行nginx -t验证配置合法性,返回syntax is ok、test is successful代表配置无误
  3. 执行systemctl reload nginx重载配置,无需重启服务不影响现有业务

注意事项

  • 确保Nginx运行用户对/home/ubuntu/app/build目录有读取权限,可执行chmod -R 755 /home/ubuntu/app/build授权
  • 如果服务器开启了SELinux,额外执行chcon -Rt httpd_sys_content_t /home/ubuntu/app/build配置访问权限

内容的提问来源于stack exchange,提问作者Chirag Jain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 06:39:03