繁体   English   中英

nestjs 413 负载太大 MULTIPART

[英]nestjs 413 payload too large MULTIPART

我正在使用 nestjs,端点仅通过 multipart 接受文件,当我尝试上传小于 10MB 的文件时,一切正常,但是当我尝试使用 >10Mb 的文件时,我总是得到 413。我读了很多关于这个的帖子

 app.use(bodyParser.json({ limit: '50mb' }));
  app.use(bodyParser.urlencoded({
    limit: '50mb',
    extended: true,
    parameterLimit: 50000
  }));

但对我不起作用,我正在使用 multer 所以我也会离开配置。

主要.ts

async function bootstrap() {
  Sentry.init({ dsn: 'https://5265e36cb9104baf9b3109bb5da9423e@sentry.io/1768434' });
  const app = await NestFactory.create(AppModule);
  app.useGlobalPipes(new ValidationPipe());
  app.use(helmet());
  app.use(bodyParser.json({ limit: '50mb' }));
  app.use(bodyParser.urlencoded({
    limit: '50mb',
    extended: true,
    parameterLimit: 50000
  }));
  app.enableCors();
  app.use(
    rateLimit({
      windowMs: 15 * 60 * 1000, // 15 minutes
      max: 20000, // limit each IP to 20000 requests per windowMs
    }),
  );
  app.use(compression());
  const options = new DocumentBuilder()
    .setTitle('Latineo Api')
    .setDescription('Api documentation for latineo')
    .setVersion('1.0')
    .addBearerAuth()
    .build();
  const document = SwaggerModule.createDocument(app, options);
  SwaggerModule.setup('api', app, document);

  await app.listen(3000);
}

具有多重配置的 MyModule

@Module({
  controllers: [RestaurantsController],
  imports: [
    RestaurantsModule,
    MongooseModule.forFeature([{ name: 'Restaurant', schema: RestaurantSchema }]),
    GlobalModule,
    UsersModule,
    ConfigModule,
    MulterModule.registerAsync({
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService) => ({
        storage: diskStorage({
          destination: `./${configService.uploadImages}`,
          filename: (req, file, cb) => {
            cb(null, `${uuid()}${extname(file.originalname)}`);
          },
        }),
        fileFilter: (req, file, cb) => {
          if (file.mimetype.match(/\/(jpg|jpeg|png)$/)) {
            cb(null, true);
          } else {
            cb(
              new BadRequestException(
                'The image format is not valid only jpg, png are a valid format',
              ),
              false,
            );
          }
        },
        limits: {
          fileSize: 104857600,
          files: 10
        },
      }),
      inject: [ConfigService],
    }),
  ],
  providers: [UtilsService, RestaurantsService],
  exports: [RestaurantsService],
})

请求的信息在此处输入图像描述

端点 header

@ApiBearerAuth()
  @UseGuards(FirebaseAuthGuard)
  @UseInterceptors(FilesInterceptor('imageUrls'))
  @ApiConsumes('multipart/form-data')
  @ApiFiles('imageUrls')
  @Put(':id/images')
  async uploadImagesRestaurant(
    @Param('id') id: string,
    @UploadedFiles() imageUrls,
    @Req() request: any,
  ): Promise<RestaurantI> {

就我而言,我已经尝试了一切,直到我意识到它来自我服务器上的 nginx。 所以我这样固定它:

  1. 纳米/etc/nginx/nginx.conf
  2. 添加了这一行: client_max_body_size 5M
  3. 保存文件,重新加载 Nginx,重启 Nginx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM