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

NestJS部署Nginx后全路由404,本地运行正常求助

Fixing 404 Errors When Deploying NestJS App with /ui Prefix on Nginx

Let’s break down why you’re hitting that 404 when deploying to Nginx, even though everything works locally. Your NestJS code correctly sets the global prefix with app.setGlobalPrefix('ui'), so the issue is almost certainly in how Nginx is proxying requests to your service.

Common Nginx Configuration Mistakes & Fixes

1. Missing or Incorrect Proxy Pass Path

The most likely culprit is that your Nginx config isn’t passing the full request path (including the /ui prefix) to your NestJS service. For example, if you have a config like this:

location /ui/ {
    proxy_pass http://your-nestjs-service:3000/;
}

The trailing slash in proxy_pass strips the /ui prefix before sending the request to NestJS—so NestJS receives a request for / instead of /ui/, which doesn’t match any routes, hence the 404.

Correct Configuration:
Remove the trailing slash from proxy_pass to preserve the full path:

server {
    listen 80;
    server_name your-domain.com;

    location /ui/ {
        proxy_pass http://your-nestjs-service:3000; # No trailing slash here
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Now when a request comes in for /ui/, Nginx forwards it to http://your-nestjs-service:3000/ui/, which matches your NestJS global prefix perfectly.

2. Handle Trailing Slash Consistency

If users might access /ui (without the trailing slash) instead of /ui/, add a redirect to keep the prefix consistent:

location /ui {
    return 301 /ui/;
}

3. Verify NestJS Service is Reachable

Double-check that Nginx can actually connect to your NestJS service. Test this directly from the Nginx server using:

curl http://your-nestjs-service:3000/ui/

If this also returns a 404, the issue might be with your NestJS deployment—for example, incorrect environment variables for CoreConfig.app.port, or the app isn’t listening on the expected interface (use 0.0.0.0 in app.listen if it’s running in a container or separate server).

Quick Additional Checks

  • Clear your browser cache or use incognito mode to rule out cached 404 responses.
  • Confirm your NestJS app is running and listening on the port specified in CoreConfig.app.port.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 11:32:56