nestjs 413 payload too large MULTIPART

I'm using nestjs and an endpoint accept only files via multipart, when I try to upload files < 10MB all works perfectly but when I try with >10Mb files I always get 413. I read many post with this

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

But not works for me, I'm using multer so I will leave the config too.

Main.ts

async function bootstrap() {
  Sentry.init({ dsn: 'https://[email protected]/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 with multer config

@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],
})

info of the request enter image description here

endpoint 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> {

Upvotes: 4

Views: 3642

Answers (1)

ezeegege
ezeegege

Reputation: 21

For me, the issue was the nginx server:

  1. Add client_max_body_size 5M to the file /etc/nginx/nginx.conf.
  2. Reload & Restart nginx.

Upvotes: 2

Related Questions