nginx – php value / ini_set for specific url

Scenario – php.ini sets upload_max_filesize to 25MBs but on some upload urls you need higher value.

This can’t be done through ini_set() function – you can change max_execution_time, memory limits, but not upload limits through this function. If you are using nginx, you can’t rely on .htaccess either.

Luckily, there’s a way. Following real case scenario is for Laravel CMS, PHP-FPM 7.4, Nginx.

location ~ \.php$ {
    include snippets/fastcgi-php.conf;

    set $phpval "upload_max_filesize = 25M \n post_max_size=25M";

    if ($request_uri ~ ^/video/upload(.*)$) {
    	set $phpval "post_max_size=1536M \n upload_max_filesize=1536M";
    }


    fastcgi_param PHP_VALUE $phpval;
    fastcgi_pass unix:/run/php/php7.4-fpm.sock; 
}

What happens here is that we create a variable called $phpval and set default values – the same as in php.ini. Then we check if ‘/video/upload’ is in the url – if it is, change the value of variable $phpval. Before passing request to php socket, we set the PHP_VALUE.

Couple notes to keep in mind

  • fastcgi_param can not be set inside the if() {} block. Hence the additional variable
  • there must be only one fastcgi_param per location block
  • you can’t use variables to set nginx parameters such as clinet_max_body_size
  • separate php settings by \n
  • use PHP_ADMIN_VALUE instead of PHP_VALUE if you want to make sure the value won’t be overwritten by ini_set ( e.g. maximum execution time )

Write a Comment

Comment

Webmentions

  • générique kamagra canadien sans ordonnance

    acheter kamagra à sydney

    medicament kamagra medicament

  • na dobírku online recepty kamagra

    levné kamagra usa online bez lékařského předpisu

    levné léky kamagra

  • purchase itraconazole cheap with prescription

    cheapest buy itraconazole cheap online pharmacy

    generic itraconazole with no prescription

  • buy fildena generic cheapest

    discount fildena generic discount

    cheap fildena australia cheap

  • order gabapentin generic is it safe

    prices for gabapentin at walmart

    gabapentin without a prescription canada

  • discount flexeril cyclobenzaprine australia cheap

    Buy flexeril cyclobenzaprine no perscription needed

    purchase flexeril cyclobenzaprine uk buy cheap

  • ordering dutasteride purchase tablets

    buy dutasteride without rx online

    pharmacy canadian dutasteride

  • online order avodart uk buy online

    how to buy avodart buy online australia

    buy cheap avodart using mastercard

  • get staxyn generic pharmacy online

    how to order staxyn generic uae

    is generic staxyn effective

  • purchase xifaxan generic alternative

    buying xifaxan cheap europe

    online order xifaxan generic uk buy

  • how to buy rifaximin uk buy cheap

    cheapest buy rifaximin usa suppliers

    ordering rifaximin generic mexico

  • purchase low price enclomiphene

    how to buy enclomiphene cheap next day delivery

    buy cheap enclomiphene online no rx

  • buy androxal uk how to get

    free androxal canada

    how to order androxal generic information