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

Express/Nginx路由异常:无论访问哪个路由均返回错误HTML文件

问题:所有路由均返回admin.html文件

无论访问哪个路由,Express始终返回admin.html文件。删除admin.html后,/admin路由会抛出错误(可被错误处理器捕获),但其他路由仍显示admin页面,即便该文件已不存在。

文件结构

// /var/www/my-site:

  express-app
    |--public
    |    |--admin.html
    |    |--index.html
    |    |--index.bundle.[hash].js
    |    |--admin.bundle.[hash].js
    |    |--// more assets
    |
    |--app.js
    |--package.json
    |--package-lock.json
    |--node_modules

Express路由配置

const port = 3000;

app.use(express.static('public'));

app.get('/admin', (req, res, next) => {
  console.log('path:', path.join(__dirname, 'public', 'admin.html'));
  res.sendFile(path.join(__dirname, 'public', 'admin.html'));
});


app.get('/index', (req, res, next) => {
  console.log('path:', path.join(__dirname, 'public', 'index.html'));
  res.sendFile(path.join(__dirname, 'public', 'index.html'));
});


app.get('/', (req, res, next) => {
  console.log('path:', path.join(__dirname, 'public', 'index.html'));
  res.sendFile(path.join(__dirname, 'public', 'index.html'));
});

app.listen(port, () => console.log(`Listening on port ${port}`));

已尝试的排查步骤

  • 修改express.static路径写法为app.use(express.static('./public')),调整res.sendFile中的路径(如添加./前缀),问题依旧
  • 路径解析日志:
    • 访问/:无日志输出
    • 访问/index:输出path: /var/www/my-site/express-app/public/index.html
    • 访问/admin:输出path: /var/www/my-site/express-app/public/admin.html
  • 本地用live-server访问index.html可正常显示,确认文件未混淆,排除webpack配置问题
  • 从pm2切换为直接用node运行服务,重启服务器,排除缓存问题
  • Nginx流量转发配置:
/etc/nginx/sites-available/default:

     index index.html index.htm index.nginx-debian.html;

      server_name my-site www.my-site.io;

      location / {
          proxy_pass http://127.0.0.1:3000/;
      }

已花费7小时排查仍无进展,恳请帮助!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 14:15:26