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 )

Sudo stop asking for password

If you are working on local machine, it could become time consuming entering password after every sudo.

You can skip this by following these steps

sudo visudo

and add following at the end of file

USER ALL=(ALL) NOPASSWD: ALL

where USER is your actual username. You can see your username by entering command ‘whoami’

add-apt-repository: command not found

If you run into article mentioning add-apt-repository ( such as Docker installation guide ) chances are it won’t work on your linux, resulting in sudo: add-apt-repository: command not found – should that happen, don’t worry, just install the software-properties-common

sudo apt-get install software-properties-common

.. and you are good to go! The add-apt-repository command will work now

gpg: can’t connect to the agent: IPC connect call failed

If you are getting this error on Windows WML Ubuntu 20.04, then the shortest answer is that you made a mistake and should’ve installed 18.04 LTS instead ( at least for now – because this won’t be the last issue you run into .. )

Should you still insist on using 20.04, then all you have to do is uninstall the gpg that comes installed by default and install gnupg1 package instead

sudo apt remove gpg
sudo apt install gnupg1

Laravel in subdirectory – nginx vhost setup

Big credit goes to Hamid Naghipour on stackoverflow for this helpful piece of code https://stackoverflow.com/questions/27785372/config-nginx-for-laravel-in-a-subfolder

Let’s say we have a domain test.sk and we want to install 2 separate laravel installations in subfolders /2018 and 2019/.

Here is a basic setup that works


server {
    listen 80;
    server_name test.sk;	
    root /storage/web/test;


	location /2018 {

		alias /storage/web/test/2018/public;
		try_files $uri $uri/ @nested2018;
	        satisfy any;
	        allow all;
	        index index.php;

		location ~ \.php$ {
	        try_files $uri =404;
	        fastcgi_pass 127.0.0.1:9000;
	        index index.php;
	        fastcgi_index index.php;
	        fastcgi_param SCRIPT_FILENAME $request_filename;
	        include fastcgi_params;
		}
	}

	location @nested2018 {
		rewrite /2018/(.*)$ /2018/index.php?/$1 last;
	}


	location /2019 {

		alias /storage/web/test/2019/public;
		try_files $uri $uri/ @nested2019;
	        satisfy any;
	        allow all;
	        index index.php;

		location ~ \.php$ {
	        try_files $uri =404;
	        fastcgi_pass 127.0.0.1:9000;
	        index index.php;
	        fastcgi_index index.php;
	        fastcgi_param SCRIPT_FILENAME $request_filename;
	        include fastcgi_params;
		}
	}

	location @nested2019 {
		rewrite /2019/(.*)$ /2019/index.php?/$1 last;
	}


    location / {
    	rewrite ^(.*)$ https://$server_name/2018$1 permanent;
    }

}