GrepBoard/매뉴얼
605 편 ·

Nginx 413 Request Entity Too Large 해결

·시험 환경 Ubuntu 24.04 · Nginx 1.24.0 (curl 8.5.0 으로 시험)·시험 범위 ↓·8 단계

업로드가 413 으로 막히면 error.log 의 too large body 줄을 확인하고 client_max_body_size 를 올립니다. 기본값 1m, 설정 위치별 우선순위, 0 의 뜻까지 재현했습니다.

파일을 올리는 요청이 413 Request Entity Too Large 로 돌아오면 Nginx 가 본문 크기 한도에서 막은 것입니다. 한도는 client_max_body_size 하나이고, 어느 블록에 쓰느냐에 따라 결과가 갈립니다. 아래는 Nginx 를 포트 18611 로 띄워 2MiB(2097152바이트) 파일을 보내며 얻은 실제 출력입니다.

[1] 413 이 Nginx 에서 나온 것인지 error.log 로 확인합니다. 아래 줄이 있으면 맞습니다. N bytes 가 요청 본문의 크기이고, 클라이언트·서버 이름 부분은 … 로 줄였습니다.

console
# nginx -T 2>/dev/null | grep error_log# tail -n 5 /var/log/nginx/error.log… [error] … *1 client intended to send too large body: 2097152 bytes, …, request: "POST /upload HTTP/1.1", host: "…"

[2] 재현합니다. 설정에 client_max_body_size 가 없으면 기본값 1m 이 적용됩니다. 아래는 return 200 만 있는 서버에 2MiB 를 보낸 결과입니다.

console
$ head -c 2M /dev/zero > big.bin$ curl -si --data-binary @big.bin http://127.0.0.1:18611/upload | head -4HTTP/1.1 413 Request Entity Too LargeServer: nginx/1.24.0 (Ubuntu)Date: Mon, 19 Oct 2026 03:52:22 GMTContent-Type: text/html

[3] 한도의 경계를 확인합니다. 기본값 1m 은 1048576바이트입니다. 한도와 같은 크기는 통과하고 1바이트만 넘어도 막힙니다.

console
$ head -c 1048576 /dev/zero | curl -s -o /dev/null -w '%{http_code}\n' --data-binary @- http://127.0.0.1:18611/upload200$ head -c 1048577 /dev/zero | curl -s -o /dev/null -w '%{http_code}\n' --data-binary @- http://127.0.0.1:18611/upload413

[4] 현재 설정에서 이 지시어가 어디에 있는지 찾습니다. 없으면 기본값 1m 입니다.

console
# nginx -T 2>/dev/null | grep -n client_max_body_size

[5] server 블록에 값을 넣습니다. 지시어는 http·server·location 어디에든 쓸 수 있습니다. 파일 하나 크기의 상한보다 조금 넉넉하게 잡습니다.

/etc/nginx/conf.d/example.com.conf
server {    listen 18611;  client_max_body_size 10m;    location / { return 200 "ok\n"; }}

문법을 검사하고 다시 읽힙니다. 같은 요청이 이제 통과합니다(시험에서는 reload 대신 새로 띄웠습니다).

console
# nginx -t && systemctl reload nginx$ curl -s -o /dev/null -w '%{http_code}\n' --data-binary @big.bin http://127.0.0.1:18611/upload200

[6] 안쪽 블록이 이깁니다. http 에 10m 을 두고 location /small/ 에만 1m 을 걸면, 같은 2MiB 가 /upload 는 통과하고 /small/x 는 막힙니다.

conf
http {    client_max_body_size 10m;    server {        listen 18611;        location / { return 200 "ok\n"; }      location /small/ { client_max_body_size 1m; return 200 "ok\n"; }    }}
console
$ curl -s -o /dev/null -w '/upload %{http_code}\n' --data-binary @big.bin http://127.0.0.1:18611/upload/upload 200$ curl -s -o /dev/null -w '/small/x %{http_code}\n' --data-binary @big.bin http://127.0.0.1:18611/small/x/small/x 413

반대로 http 는 1m 으로 두고 업로드 경로에만 크게 열 수도 있습니다. 시험에서 http 1m · server 3m · location 2m 조합은 location 값이 적용되어 2MiB 가 통과했고, location 2097151(1바이트 모자람)은 413 이었습니다. 업로드 경로만 여는 쪽이 다른 경로까지 크게 열어 두는 것보다 낫습니다.

[7] 0 은 크기 검사를 끕니다. 공식 문서 문구는 "Setting size to 0 disables checking of client request body size." 입니다. 시험에서 http 1m 아래 location /big/ 에 0 을 두자 2MiB 가 /big/x 로 통과하고 /upload 는 413 이었습니다. 한도가 사라지므로 인증 뒤의 경로 같은 곳에만 씁니다.

console
$ curl -s -o /dev/null -w '/big/x %{http_code}\n' --data-binary @big.bin http://127.0.0.1:18611/big/x/big/x 200

[8] 그래도 막히면 Nginx 뒤의 앱이 자기 한도로 막고 있을 수 있습니다. PHP 라면 upload_max_filesize(기본 2M)와 post_max_size(기본 8M)가 따로 있고, 공식 문서에 post_max_size 는 upload_max_filesize 보다 커야 한다고 나와 있습니다. 두 값은 php.ini 에서 바꾸며, 앞단에 다른 프록시가 있으면 그쪽 한도도 봅니다. 이 글에서 PHP 는 재현하지 않았습니다.

값을 정하는 곳기본값넘으면
Nginx client_max_body_size1m413 · client intended to send too large body
PHP upload_max_filesize2M문서 기준 — 파일이 올라오지 않음
PHP post_max_size8M문서 기준 — $_POST·$_FILES 가 빔

출처: nginx.org — client_max_body_size · PHP 매뉴얼 — upload_max_filesize · post_max_size

Nginx 로그를 브라우저에서 바로 분석 ↗ — 아무것도 올라가지 않습니다.

#nginx#upload#413