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

Express Gateway结合Nginx代理无法访问API问题求助

问题解决方案

你的问题核心是Express Gateway的API端点匹配规则与实际请求的Host头不匹配:

  • 本地访问时,请求的Host是localhost,和配置中apiEndpoints里的host: localhost一致,所以能正常匹配路由;
  • 通过服务器IP访问时,Nginx传递的Host头是服务器IP(xx.xxx.xxx.183),但网关配置里所有apiEndpoints只接受localhost的Host,导致路由匹配失败,返回404。

修复步骤:

1. 修改Express Gateway的YAML配置

将所有apiEndpoints中的host: localhost删除(默认匹配任意Host),或者替换为0.0.0.0,也可以添加服务器IP作为允许的Host值。

示例修改后的swagger端点配置:

swagger:
  paths: ['/api-docs*']

或者指定允许的多个Host:

swagger:
  host: ['localhost', 'xx.xxx.xxx.183']
  paths: ['/api-docs*']

批量修改所有apiEndpoints,确保每个端点都能匹配到服务器IP的Host请求。

2. 重启服务使配置生效

  • 重启Express Gateway(根据你的进程管理方式调整命令):
pm2 restart express-gateway
  • 重启Nginx服务:
sudo systemctl restart nginx

3. 额外检查项

  • 确认服务器4010端口已开放:
sudo ufw allow 4010
  • 确认.env文件中的PORT值为4010,Express Gateway正在监听该端口。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 13:00:30