본문 바로가기

리눅스

[draft] NGINX 및 PHP 파일 업로드 크기 제한 변경

728x90

NGINX 및 PHP(PHP-FPM) 파일 업로드 크기 제한 변경

NGINX와 PHP-FPM 환경에서 파일 업로드 용량을 50MB로 늘리는 방법입니다.

NGINX 설정 변경

최대 파일 업로드 크기를 50MB로 설정합니다.

 

설정 파일 수정(nginx.conf, default.conf)

  • NGINX에서는 client_max_body_size 값을 설정합니다.

http 블록 설정

vim /etc/nginx/nginx.conf
http {
    ...
    client_max_body_size 50M;
    ...
}

서버 블록 설정

vim /etc/nginx/conf.d/default.conf
server {
    listen 80;
    server_name sangchul.kr;
    
    location / {
        proxy_pass http://127.0.0.1:9000;
    }

    client_max_body_size 50M;
}

NGINX 재시작

sudo systemctl restart nginx

PHP(PHP-FPM) 설정 변경

최대 파일 업로드 크기를 50MB로 설정합니다.

 

php.ini 설정 수정

vim /etc/php/8.3/fpm/php.ini

upload_max_filesizepost_max_size 설정(upload_max_filesize보다 크거나 같아야 함)을 찾아 원하는 값으로 변경

upload_max_filesize = 50M
post_max_size = 50M

PHP(PHP-FPM) 재시작

sudo systemctl restart php-fpm
728x90

NGINX 및 PHP(PHP-FPM) 설정 확인

$ cat /etc/nginx/conf.d/default.conf | egrep client_max_body_size
    client_max_body_size 50M;
$ nginx -T | grep client_max_body_size
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
    client_max_body_size 50M;
$ cat /etc/php/8.3/fpm/php.ini | egrep -v "^;" | egrep 'upload_max_filesize|post_max_size'
post_max_size = 50M
upload_max_filesize = 50M
$ php-fpm8.3 -i | egrep 'upload_max_filesize|post_max_siz'
post_max_size => 60M => 60M
upload_max_filesize => 50M => 50M

파일 업로드 테스트

upload.php 샘플 코드

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $uploadDir = __DIR__ . '/uploads/';
    if (!is_dir($uploadDir)) {
        mkdir($uploadDir, 0777, true);
    }

    if (isset($_FILES['file'])) {
        $file = $_FILES['file'];
        $filePath = $uploadDir . basename($file['name']);

        if (move_uploaded_file($file['tmp_name'], $filePath)) {
            echo "✅ 파일 업로드 성공: " . htmlspecialchars($file['name']);
        } else {
            echo "❌ 파일 업로드 실패!";
        }
    } else {
        echo "⚠️ 업로드할 파일이 없습니다.";
    }
} else {
?>
<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <title>파일 업로드</title>
</head>
<body>
    <h2>파일 업로드</h2>
    <form action="upload.php" method="post" enctype="multipart/form-data">
        <input type="file" name="file" required>
        <button type="submit">업로드</button>
    </form>
</body>
</html>
<?php
}
?>

웹 브라우저로 확인

브라우저

cURL 명령으로 확인

dd 명령어로 파일 생성

dd if=/dev/urandom of=testfile.bin bs=1M count=50
$ ls -lh testfile.bin
-rw-rw-r-- 1 ubuntu ubuntu 50M Feb  3 20:03 testfile.bin

curl 명령어로 파일 업로드

curl -X POST -F "file=@testfile.bin" http://127.0.0.1/upload.php
✅ 파일 업로드 성공: testfile.bin

추가 설정(선택 사항)

FastCGI 버퍼 설정

  • NGINX에서 PHP-FPM으로 전달하는 요청이 크면 fastcgi_buffers 값을 조정해야 합니다.
vim /etc/nginx/conf.d/default.conf
server {
    listen 80;
    server_name sangchul.kr;

    client_max_body_size 50M;

    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;

        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
    }
}

PHP-FPM request_terminate_timeout 설정

  • 업로드 시간이 길어지면 PHP-FPM이 강제 종료될 수 있습니다.
vim /etc/php/8.3/fpm/pool.d/www.conf
request_terminate_timeout = 300

파일 업로드 실패 코드

  • NGINX의 client_max_body_size가 충분히 크지 않을 때 발생합니다.
413 Request Entity Too Large

 

이제 NGINX 및 PHP에서 파일 업로드 크기 제한이 변경되었습니다.

 

참고URL

- Runebook.dev : Module ngx_http_core_module

- Nginx Document : Module ngx_http_core_module

 

728x90