Network

[nginx] 413 Request Entity Too Large 오류

Spring boot를 활용해 이미지 파일을 주고 받는 api를 구성하였다.

이 때, MultipartFile을 활용하고 yaml 파일을 이용해서 file 크기 등을 명시해주었었다.

spring:
  servlet:
    multipart:
      file-size-threshold: 1MB
      location: C:/Temp
      max-file-size: 10MB
      max-request-size: 100MB
      enabled: true

 

yaml 파일에 작성된 옵션과 관련된 자세한 내용은 여기를 참고하면 된다.

 

413 Request Entity Too Large

아무튼, 저렇게 10MB로 설정을 해 주었는데, 1MB 이상의 파일을 업로드하니 알 수 없는 에러가 발생했다.

메시지 내용은 별도의 ExceptionHandler가 없는 경우 다음과 같이 나온다.

HTTP/1.1 413 Request Entity Too Large

blocked by CORS policy: 
No 'Access-Control-Allow-Origin' header is present on the request resource.

 

문제는, nginx의 client_max_body_size 설정 때문이다.

nginx를 reverse proxy로 사용하고 있는 경우 nginx를 통해 들어오는 client max body size는 기본값 1MB로 지정되어있다. 따라서 yaml에서 아무리 10MB로 늘려주었다 한들, 1MB 이상의 파일이 들어오면 에러가 발생하는 것이다.

 

따라서 이를 해결, 또는 원하는 크기의 파일을 nginx를 통해 넘겨주기 위해서는 nginx.conf에 다음과 같은 설정을 추가하면 된다.

http {
  // default 1M
  client_max_body_size 10M;
}

client_max_body_size 옵션에 원하는 크기를 M단위로 작성해주면 된다.

이때, 제한을 두고 싶지 않은 경우에는 0을 작성하면 된다.

 

nginx.conf 파일을 수정하고, nginx를 reload 해 주면 변경사항의 반영이 완료된다.

 

참고 자료