Performance question regarding regex

Igor Sysoev igor at sysoev.ru
Fri Jul 16 13:28:40 MSD 2010


On Thu, Jul 15, 2010 at 09:18:33PM -0400, panni wrote:

> Dear forums/mailing list,
> 
> just a quick question:
> 
> [code]location / {
>             root /home/folder/www;
>             index  index.php;
>             #if (!-e $request_filename) {
>             if ($request_filename !~*
> ^.+\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|swf)$)
>             {
>                 #rewrite ^(.*)$ /index.php?q=$1 last;
>                 rewrite ^.*$ /index.php last;
>                 break;
>             }
>             root /home/folder/www;
>             expires 30d;
>             break;
>         }
> 
> [/code]
> 
> What would be faster here. Using the commented "#if (!-e
> $request_filename) {" or the explicit filetype matching?

The better will be

root  /home/folder/www;

location / {
     index  index.php;
     fastcgi_pass   ...
     fastcgi_param  SCRIPT_FILENAME  /home/folder/www/index.php;
     include   fastcgi_params;
}

location ~* \.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|swf)$ {
     expires 30d;
}

or

root  /home/folder/www;

location / {
     try_files   $uri  $uri/   @php;
     index  index.php;
     expires 30d;
}

location \.php$ {
     fastcgi_pass   ...
     fastcgi_param  SCRIPT_FILENAME  /home/folder/www$uri;
     include   fastcgi_params;
}

location @php {
     fastcgi_pass   ...
     fastcgi_param  SCRIPT_FILENAME  /home/folder/www/index.php;
     include   fastcgi_params;
}


-- 
Igor Sysoev
http://sysoev.ru/en/



More information about the nginx mailing list