Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MirasMustimuly's avatar

nginx configuration fast cgi vs index.php

Hello!

Here is my nginx config. See the try_files $uri $uri/ /index.php?$args; inside location / block.

I added that try_files directive, but other try_files directives use @php instead index.php.

It seems like @php is proxying requests to fast cgi? I dont know much about proxying and fast cgi, so my questions are: Do i need to change the try_files $uri $uri/ /index.php?$args; that i added to try_files $uri $uri/ @php Would it be better for performance?

server {
	index index.php index.html;
	disable_symlinks if_not_owner from=$root_path;
	ssi on;
	gzip on;
	gzip_comp_level 5;
	gzip_disable "msie6";
	gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;
	location / {
	    # this line added by me to default config
        try_files $uri $uri/ /index.php?$args;
	    
		location ~ [^/]\.ph(p\d*|tml)$ {
			try_files /does_not_exists @php;
		}

		location ~* ^.+\.(jp1g|jp1eg|gif|pn1g|svg|js|css|mp13|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf)$ {
			expires 24h;
		}
	}
	location @php {
		fastcgi_index index.php;
		fastcgi_param PHP_ADMIN_VALUE "sendmail_path = /usr/sbin/sendmail -t -i -f [email protected]";
		fastcgi_pass unix:/var/www/php-fpm/4.sock;
		fastcgi_split_path_info ^((?U).+\.ph(?:p\d*|tml))(/?.+)$;
		try_files $uri =404;
		include fastcgi_params;
	}
	listen X.X.X.X:443 ssl http2;
}
0 likes
1 reply
LaryAI's avatar
Level 58

The try_files directive is used to check if a file exists and serve it, or fallback to the next option. In this case, the try_files $uri $uri/ /index.php?$args; directive is checking if the requested URI exists as a file or directory, and if not, it is serving the index.php file with any query string parameters.

The @php location block is used as a fallback for any PHP files that do not exist. It passes the request to the PHP FastCGI process for handling.

If you are not using any other @php location blocks in your configuration, you can safely remove the try_files /does_not_exists @php; line from the location ~ [^/]\.ph(p\d*|tml)$ block.

If you are using other @php location blocks, you can keep the try_files /does_not_exists @php; line, as it will ensure that any PHP files that do not exist are passed to the PHP FastCGI process for handling.

In terms of performance, using @php instead of index.php may be slightly faster, as it avoids the overhead of checking if the index.php file exists. However, the difference is likely to be negligible in most cases.

Here is the updated configuration without the try_files /does_not_exists @php; line:

server {
    index index.php index.html;
    disable_symlinks if_not_owner from=$root_path;
    ssi on;
    gzip on;
    gzip_comp_level 5;
    gzip_disable "msie6";
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;
    location / {
        try_files $uri $uri/ /index.php?$args;
        
        location ~ [^/]\.ph(p\d*|tml)$ {
            # Remove this line
        }

        location ~* ^.+\.(jp1g|jp1eg|gif|pn1g|svg|js|css|mp13|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf)$ {
            expires 24h;
        }
    }
    location @php {
        fastcgi_index index.php;
        fastcgi_param PHP_ADMIN_VALUE "sendmail_path = /usr/sbin/sendmail -t -i -f [email protected]";
        fastcgi_pass unix:/var/www/php-fpm/4.sock;
        fastcgi_split_path_info ^((?U).+\.ph(?:p\d*|tml))(/?.+)$;
        try_files $uri =404;
        include fastcgi_params;
    }
    listen X.X.X.X:443 ssl http2;
}

Please or to participate in this conversation.