首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当使用nginx部署时,NestJs在所有路由上都会返回404,但在本地一切运行正常

当使用nginx部署时,NestJs在所有路由上都会返回404,但在本地一切运行正常
EN

Stack Overflow用户
提问于 2020-09-19 20:50:07
回答 1查看 446关注 0票数 0
代码语言:javascript
运行
复制
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { CoreConfig } from './config.const';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { NestApplicationOptions } from '@nestjs/common';

async function bootstrap() {
  const httpOptions ={
    requestCert: false,
    rejectUnauthorized: true,
  };
  const app = await NestFactory.create(AppModule);
  const options = new DocumentBuilder()
    .addServer(`http://localhost:${CoreConfig.app.port}`)
    .setTitle('test UI/AI')
    .setDescription('UI meta service')
    .setVersion('1.0')
    .addTag('test')
    .addBearerAuth({type:'http', bearerFormat: 'authorization'})
    .build();
  const document = SwaggerModule.createDocument(app, options);
  SwaggerModule.setup('docs', app, document);
  app.enableCors();
  app.setGlobalPrefix('ui');
  await app.listen(CoreConfig.app.port);
}

bootstrap().then();

当我的应用程序使用nginx部署时,我们有一个前缀用于路由到这个NestJs应用程序(UI),所有请求都以404结尾,但在本地一切正常。

以下是部署实例的结果

代码语言:javascript
运行
复制
{"statusCode":404,"error":"Not Found","message":"Cannot GET /ui/"}

和本地http://prntscr.com/uk2dgj

EN

回答 1

Stack Overflow用户

发布于 2021-03-27 11:43:18

我将ingress nginx配置为将所有访问/api/的流量发送到我的后端(NestJS应用程序)。

问题是/api/部件被发送到我的NestJS应用程序,而这些路由都不存在。例如,我的graphql端点位于/graphl,但使用ingress nginx时,所有流量都将发送到/api/graphql,因此是404'ing。

我的修复方法是添加一个"api“的global prefix within NestJS,并手动将graphql路径设置为/api/graphql

您也可以使用ingress nginx重写从通过ingress-nginx rewrite targets发送到后端的路由中删除/api/

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63968993

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档