Saya menjalankan aplikasi Laravel saya di atas Nginx PHP-FPM. Saya memiliki permintaan fitur yang mengharuskan halaman web untuk mengizinkan unggahan video hingga 100 MB. Yang tidak saya inginkan adalah membuka seluruh permintaan untuk mengizinkan 100MB.

Ini adalah pengaturan nginx awal saya:

server {
    listen 80;
    listen [::]:80;

    root /path-to-web/public;
    index index.php index.html;

    server_name www.myweb.com;

    client_max_body_size 18m;
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    proxy_connect_timeout 180s;
    proxy_send_timeout 180s;
    proxy_read_timeout 180s;
    fastcgi_send_timeout 180s;
    fastcgi_read_timeout 180s;

    # pass the PHP scripts to FastCGI server
    #
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        fastcgi_param PHP_VALUE "newrelic.appname=www.myweb.com";
    }
}

server {
    listen 443;
    listen [::]:443;

    ssl on;
    ssl_certificate /path-to-cert/cert.pem;
    ssl_certificate_key /path-to-cert/cert.key;

    root /path-to-web/public;
    index index.php index.html;

    server_name www.myweb.com;

    client_max_body_size 18m;
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    proxy_connect_timeout 180s;
    proxy_send_timeout 180s;
    proxy_read_timeout 180s;
    fastcgi_send_timeout 180s;
    fastcgi_read_timeout 180s;

    # pass the PHP scripts to FastCGI server
    #
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        fastcgi_param PHP_VALUE "newrelic.appname=www.myweb.com";
    }
}

Inilah yang saya coba sejauh ini tanpa hasil (403 Entitas Permintaan terlalu besar):

server {
    listen 80;
    listen [::]:80;

    root /path-to-web/public;
    index index.php index.html;

    server_name www.myweb.com;

    proxy_connect_timeout 180s;
    proxy_send_timeout 180s;
    proxy_read_timeout 180s;
    fastcgi_send_timeout 180s;
    fastcgi_read_timeout 180s;
    
    location / {
        client_max_body_size 18m;
        try_files $uri $uri/ /index.php?$query_string;
    }    

    location ~ ^/path-to-video-upload/.+\.php$ {
        client_max_body_size 256M;
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        fastcgi_param PHP_VALUE "newrelic.appname=\"www.myweb.com\"upload_max_filesize=\"256M\"post_max_size=\"256M\"";
    }

    # pass the PHP scripts to FastCGI server
    #
    location ~ \.php$ {
        client_max_body_size 18m;
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        fastcgi_param PHP_VALUE "newrelic.appname=www.myweb.com";
    }
}

server {
    listen 443;
    listen [::]:443;

    ssl on;
    ssl_certificate /path-to-cert/cert.pem;
    ssl_certificate_key /path-to-cert/cert.key;

    root /path-to-web/public;
    index index.php index.html;

    server_name www.myweb.com;
    
    proxy_connect_timeout 180s;
    proxy_send_timeout 180s;
    proxy_read_timeout 180s;
    fastcgi_send_timeout 180s;
    fastcgi_read_timeout 180s;

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

    location ~ ^/path-to-video-upload/.+\.php$ {
        client_max_body_size 256M;
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        fastcgi_param PHP_VALUE "newrelic.appname=\"www.myweb.com\"upload_max_filesize=\"256M\"post_max_size=\"256M\"";
    }

    # pass the PHP scripts to FastCGI server
    #
    location ~ \.php$ {
        client_max_body_size 18m;
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        fastcgi_param PHP_VALUE "newrelic.appname=www.myweb.com";
    }
}

Terima kasih atas bantuannya.

Berkat jawaban @Darryl E. Clarke, saya berhasil membuatnya bekerja. LARUTAN:

server {
    listen 80;
    listen [::]:80;

    root /path-to-laravel-app/public;
    index index.php index.html;

    server_name www.myweb.com;

    proxy_connect_timeout 180s;
    proxy_send_timeout 180s;
    proxy_read_timeout 180s;
    fastcgi_send_timeout 180s;
    fastcgi_read_timeout 180s;

    client_body_buffer_size 16k;

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

    location /path-to/upload-file/ {
        client_max_body_size 256M;
        try_files $uri $uri/ /index.php?$query_string;
    }

    # pass the PHP scripts to FastCGI server
    #
    location ~ \.php$ {
        client_max_body_size 256M;
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        fastcgi_param PHP_VALUE "newrelic.appname=\"www.myweb.com\"
            upload_max_filesize=\"256M\"
            post_max_size=\"256M\"";
    }
}

server {
    listen 443;
    listen [::]:443;

    ssl on;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_certificate /path-to-cert/cert.pem;
    ssl_certificate_key /path-to-cert/cert.key;

    root /path-to-laravel-app/public;
    index index.php index.html;

    server_name www.myweb.com;

    proxy_connect_timeout 180s;
    proxy_send_timeout 180s;
    proxy_read_timeout 180s;
    fastcgi_send_timeout 180s;
    fastcgi_read_timeout 180s;

    client_body_buffer_size 16k;

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

    location /path-to/upload-file/ {
        client_max_body_size 256M;
        try_files $uri $uri/ /index.php?$query_string;
    }

    # pass the PHP scripts to FastCGI server
    #
    location ~ \.php$ {
        client_max_body_size 256M;
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        fastcgi_param PHP_VALUE "newrelic.appname=\"www.myweb.com\"
            upload_max_filesize=\"256M\"
            post_max_size=\"256M\"";
    }
}

Terima kasih!

0
kml 3 Maret 2020, 04:42

1 menjawab

Jawaban Terbaik

Kembali ke konfigurasi asli Anda dan hanya ubah ini:

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

 location /path-to-video-upload {
        client_max_body_size 256m;
        try_files $uri $uri/ /index.php?$query_string;
    }   

Anda tidak perlu mencocokkan regex .php apa pun, kecuali jika rute Anda secara eksplisit memiliki php di dalamnya. Ini akan cocok dengan rute Laravel lengkap dan menerapkan batas ukuran tubuh ke URL itu saja.

1
Darryl E. Clarke 3 Maret 2020, 02:07