위치 블록당 Nginx client_max_body_size(php frontcontroller 패턴 사용)

위치 블록당 Nginx client_max_body_size(php frontcontroller 패턴 사용)

나는 해결책을 찾고 있습니다.이 문제. 해당 질문의 설정이 작동하지 않는 이유를 이해하지만 작동시킬 수 있는 솔루션을 찾으려고 노력합니다.

아이디어는 특정 URL에만 대용량 파일 업로드를 허용하는 것입니다. 이를 위해 블록을 사용할 수 있지만 location문제는 다음과 같습니다. PHP frontcontroller 패턴이 있습니다.

location ~ \.php {
    # ...
    fastcgi_pass unix:/tmp/php5-fpm.sock;
}

내 전체 구성은 다음과 같습니다.

# ...

http {
    # ...

    client_max_body_size 512K;

    server {
        server_name example.com;
        root        /var/www/example.com/public;

        location / { 
            try_files $uri /index.php?$query_string;
        }

        location /admin/upload {
            client_max_body_size 256M;
        }

        location ~ \.php {
            # ...

            fastcgi_pass unix:/tmp/php5-fpm.sock;
        }
    }
}

내가 아는 바로는 하나의 위치 블록만 적용됩니다. 따라서 기본 요청 크기가 512K인 경우 모든 요청이 frontcontroller 패턴을 통해 일치되므로 256M은 적용되지 않습니다 ~ \.php.

이 경우 제가 맞나요? 그렇다면 방문자가 에 업로드할 때를 제외하고는 아무것도 업로드할 수 없도록 어떻게 구성할 수 있습니까 admin/upload?

답변1

경로가 가상 이면 /admin/upload다음과 같이 작동하게 할 수 있습니다.

location / {
    try_files $uri /index.php?$args;
}

location /admin/upload {
    client_max_body_size 256M;

    include inc/php.conf;
    rewrite ^(.*)$ /index.php?$args break;
}

location ~ \.php$ {
    include inc/php.conf;
}

가장 예쁘지는 않지만 작동합니다.

답변2

두 개의 PHP 위치를 정의하시겠습니까?

location ~ ^/admin/upload/.+\.php$
{
    client_max_body_size 256M;
    include /etc/nginx/conf.d/php-fpm.conf;
}   
location ~ \.php
{
    include /etc/nginx/conf.d/php-fpm.conf;
}

아마도 가장 예쁘지는 않을 것입니다. 그래도 기능적이어야 합니다..

답변3

여러 위치를 사용하는 것이 가능하지만 약간 까다롭습니다.

위에서 설명한 대로 try_files또는 를 사용하면 실제로는 기대하는 위치 블록에 대한 컨텍스트가 아니라 더 높은 컨텍스트의 컨텍스트 로 설정됩니다 .rewriteclient_max_body_sizeclient_max_body_size

PHP FastCGI 구성을 포함할 수 있는 파일(예: php-conf.conf.

그런 다음 다음 구성을 사용하십시오.

    location / {
        # try to serve file directly, fallback to index.php
        try_files $uri /index.php$query_string;
    }

    location ~ ^/admin/upload$ {
        client_max_body_size 4m;
        include /etc/nginx/php-conf.conf;
        fastcgi_param SCRIPT_FILENAME $realpath_root/index.php;
    }

    location ~ ^/index\.php(/|$) {
        include /etc/nginx/php-conf.conf;
        internal;
    }

다른 스크립트 이름을 설정한 경우에는 덮어써서 SCRIPT_FILENAME사용해야 하므로 주의하세요 . index.php.fastcgi_split_path_info ^(.+\.php)(/.*)$;

관련 정보