Home / News / TIL snippets/fastcgi-php.conf

TIL snippets/fastcgi-php.conf

# 🤖 sarcasm: Why did the PHP location in Nginx look like that for so many years? 🤖

Ever since the PHP location in Nginx looked like this:
“`plaintext
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
“`

I never questioned it, it’s just been copy-pasted a billion times over and over from thousands of online resources with PHP and WordPress tutorials. But today, while writing the Nginx configuration lesson, I decided to delve into the details and understand why it’s necessary.

Turns out, the `fastcgi-php.conf` snippet was created for administrators to avoid having to define a `SCRIPT_FILENAME` (and `PATH_INFO` in some older tutorials) manually, as well as include the default `fastcgi_params`. However, with the introduction of this snippet, many tutorials seemed to have added it without removing the redundant bits.

My new location block now looks like this:
“`plaintext
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix

For many many years, my PHP location in Nginx looked like this:

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

I never questioned it, it’s just been copy-pasted a billion times over and over from thousands of online resources with PHP and WordPress tutorials.

Today, while writing the Nginx configuration lesson, I decided to question it, and tried to understand what every single line does, and why it’s necessary.

Turns out, it’s mostly redundant. The fastcgi-php.conf snippet was created for admins to avoid having to define a SCRIPT_FILENAME (and PATH_INFO in some older tutorials) manually, as well as include the default fastcgi_params.

Not sure why so many tutorials decided to add this snippet, without removing the redundant bits. My new location block now looks like this:

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php-fpm.sock;
}

And this makes me happy.

Sometimes it’s worth questioning things we do a certain way, simply because we’ve been doing them this way for many years.

Tagged:

Leave a Reply

Your email address will not be published. Required fields are marked *