# Performance question regarding regex

**URL:** <https://www.ruby-forum.com/t/performance-question-regarding-regex/191679>\
**Category:** NGINX\
**Created:** [July 16, 2010, 3:19am UTC](https://www.ruby-forum.com/t/performance-question-regarding-regex/191679 "2010-07-16T03:19:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![panni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dec6dc/32.png) [@panni](https://www.ruby-forum.com/u/panni)\
**Post date:** [July 16, 2010, 3:19am UTC](https://www.ruby-forum.com/t/performance-question-regarding-regex/191679/1 "2010-07-16T03:19:09Z")

</div>

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?

Thank you in advance!

Posted at Nginx Forum:

> **[Performance question regarding regex](https://forum.nginx.org/read.php?2,109438,109438#msg-109438)**
>
> 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|i

---

<div class="post-metadata">

**Author:** ![panni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dec6dc/32.png) [@panni](https://www.ruby-forum.com/u/panni)\
**Post date:** [July 16, 2010, 3:25am UTC](https://www.ruby-forum.com/t/performance-question-regarding-regex/191679/2 "2010-07-16T03:25:01Z")

</div>

Sorry, forgot to add that Zend Framework is used here, so any dynamic  
request would pass “if (!-e $request\_filename) {” because it’s ending  
with “/”.

Posted at Nginx Forum:

> **[Re: Performance question regarding regex](https://forum.nginx.org/read.php?2,109438,109439#msg-109439)**
>
> 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|i

---

<div class="post-metadata">

**Author:** ![panni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dec6dc/32.png) [@panni](https://www.ruby-forum.com/u/panni)\
**Post date:** [July 16, 2010, 11:29am UTC](https://www.ruby-forum.com/t/performance-question-regarding-regex/191679/3 "2010-07-16T11:29:41Z")

</div>

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

> ```
> {
> 
> ```
> 
> 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 S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![panni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dec6dc/32.png) [@panni](https://www.ruby-forum.com/u/panni)\
**Post date:** [July 30, 2010, 1:45am UTC](https://www.ruby-forum.com/t/performance-question-regarding-regex/191679/4 "2010-07-30T01:45:47Z")

</div>

Hey, thank you for your answer.

try\_files only seems to hit when a root directive is set in the config.  
try\_files only works on /home/www$uri files, not on /www/html$uri  
files.  
Is this intended? How can I make it work when a file called  
/www/html/asdf.php is requested by /asdf.php?

```auto
location / {
            root /home/www;
            try_files $uri "/www/html$uri" @ksphp;

            if ($request_filename ~* \.(js|css)$) {
                return 404;
            }

            index index.php;
            expires 30d;
        }
```

Thank you!

Posted at Nginx Forum:

> **[Re: Performance question regarding regex](https://forum.nginx.org/read.php?2,109438,114603#msg-114603)**
>
> 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|i

---

<div class="post-metadata">

**Author:** ![panni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dec6dc/32.png) [@panni](https://www.ruby-forum.com/u/panni)\
**Post date:** [July 30, 2010, 9:02am UTC](https://www.ruby-forum.com/t/performance-question-regarding-regex/191679/5 "2010-07-30T09:02:24Z")

</div>

On Thu, Jul 29, 2010 at 07:44:54PM -0400, panni wrote:

> Hey, thank you for your answer.
> 
> try\_files only seems to hit when a root directive is set in the config.  
> try\_files only works on /home/www$uri files, not on /www/html$uri  
> files.  
> Is this intended? How can I make it work when a file called  
> /www/html/asdf.php is requested by /asdf.php?

```
     root /;
     try_files home/www$uri www/html$uri @ksphp;

```

but probably it’s better to use:

location = /asdf.php {  
fastcgi\_pass …  
fastcgi\_param SCRIPT\_FILENAME /www/html$uri;  
…  
}

> ```
> }
> 
> ```

Do not use “if ($request\_filename”:

location / {  
root /home/www;  
try\_files $uri @ksphp;

```
index index.php;
expires 30d;

```

}

location ~\* .(js|css)$ {  
return 404;  
}

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![panni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dec6dc/32.png) [@panni](https://www.ruby-forum.com/u/panni)\
**Post date:** [July 30, 2010, 3:43pm UTC](https://www.ruby-forum.com/t/performance-question-regarding-regex/191679/6 "2010-07-30T15:43:29Z")

</div>

Thank you again for your quick answer.

## Igor S. Wrote:

> > Is this intended? How can I make it work when a  
> > fastcgi\_pass …  
> > fastcgi\_param SCRIPT\_FILENAME  
> > /www/html$uri;  
> > …  
> > }

Is try\_files over three possible locations that slow or why are you  
suggesting the directive for the literal string?

> > 
> 
> ```
> index index.php;
> expires 30d;
> 
> ```
> 
> }
> 
> location ~\* .(js|css)$ {  
> return 404;  
> }

Thank you. But why would you do this rather than an if clause? If its  
faster, or my way a no-go, do you have some sort of compendium of do’s  
and don’ts in NGINX?

> –  
> Igor S.  
> [Igor Sysoev](http://sysoev.ru/en/)
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,109438,114822#msg-114822](http://forum.nginx.org/read.php?2,109438,114822#msg-114822)
