最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

网站源码admin33浏览0评论

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

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

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 结尾,但是在本地一切正常。

这是实例部署的结果

{"statusCode":404,"error":"Not Found","message":"Cannot GET /ui/"}

和本地

回答如下:

我将 ingress-nginx 配置为将访问

/api/
的所有流量发送到我的后端(NestJS 应用程序)。

问题是

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

我的解决方法是在“api”的 NestJS 中添加一个global 前缀,并手动将 graphql 路径设置为

/api/graphql
.

您也可以使用 ingress-nginx 重写从通过

ingress-nginx 重写目标发送到后端的路由中删除
/api/

发布评论

评论列表(0)

  1. 暂无评论