# How to remove the "IF" in this fcgi config

**URL:** <https://www.ruby-forum.com/t/how-to-remove-the-if-in-this-fcgi-config/226809>\
**Category:** NGINX\
**Created:** [February 21, 2013, 6:49pm UTC](https://www.ruby-forum.com/t/how-to-remove-the-if-in-this-fcgi-config/226809 "2013-02-21T18:49:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ewildgoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bcef8e/32.png) [@ewildgoose](https://www.ruby-forum.com/u/ewildgoose)\
**Post date:** [February 21, 2013, 6:49pm UTC](https://www.ruby-forum.com/t/how-to-remove-the-if-in-this-fcgi-config/226809/1 "2013-02-21T18:49:06Z")

</div>

Hi, I’m trying to setup a php app using fpm (owncloud).

I am trying to match urls which can all over the filesystem and of the  
form: something.php/some/path?params

So far I have something like this:

```
             location / {
                     try_files $uri $uri/ index.php;
             }

             location ~ ^(?P<script_name>.+\.php)(/|$) {
                     fastcgi_split_path_info ^(.+\.php)(/.*)$;
                     if (!-f $script_name) {
                             #return 404;
                             break;
                     }
                     include fastcgi2.conf;
                     fastcgi_pass 127.0.0.1:9000;
             }

```

where:  
fastcgi2.conf is a copy of fastcgi.conf with one change:  
fastcgi\_param REQUEST\_URI $uri$is\_args$args;

How do I avoid using an IF here to check that the php file really  
exists? Also, why does uncommenting the return 404 cause some kind of  
breakage (not even sure I understand exactly what happens? Seems like  
the paths get broken?)

Thanks for any help?

Ed W

---

<div class="post-metadata">

**Author:** ![ewildgoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bcef8e/32.png) [@ewildgoose](https://www.ruby-forum.com/u/ewildgoose)\
**Post date:** [February 21, 2013, 6:55pm UTC](https://www.ruby-forum.com/t/how-to-remove-the-if-in-this-fcgi-config/226809/2 "2013-02-21T18:55:33Z")

</div>

On Feb 21, 2013, at 21:48 , Ed W wrote:

> where:  
> fastcgi2.conf is a copy of fastcgi.conf with one change:  
> fastcgi\_param REQUEST\_URI $uri$is\_args$args;
> 
> How do I avoid using an IF here to check that the php file really exists? Also,  
> why does uncommenting the return 404 cause some kind of breakage (not even sure I  
> understand exactly what happens? Seems like the paths get broken?)

```
           location ~ ^(?<script_name>.+\.php)(?<path_info>/|$) {
                   try_files $script_name =404;

                   include fastcgi2.conf;
                   fastcgi_param PATH_INFO $path_info;
                   fastcgi_pass 127.0.0.1:9000;
           }

```

–  
Igor S.

> **[F5 NGINX: Built for cloud-native apps. Ready for AI traffic.](https://www.f5.com/products/nginx)**
>
> Deliver modern applications with NGINX. Explore high-performance load balancing, API gateways, and web server solutions trusted by top global enterprises.

---

<div class="post-metadata">

**Author:** ![ewildgoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bcef8e/32.png) [@ewildgoose](https://www.ruby-forum.com/u/ewildgoose)\
**Post date:** [February 21, 2013, 8:09pm UTC](https://www.ruby-forum.com/t/how-to-remove-the-if-in-this-fcgi-config/226809/3 "2013-02-21T20:09:12Z")

</div>

On 21/02/2013 17:54, Igor S. wrote:

> ```
> location ~ ^(?<script_name>.+\.php)(?<path_info>/|$) {
> try_files $script_name =404;
> 
> include fastcgi2.conf;
> fastcgi_param PATH_INFO $path_info;
> fastcgi_pass 127.0.0.1:9000;
> }
> 
> ```

Thanks!!  
Can I ask you to confirm the correction of a typo in your answer. Do I  
want this:

```
 ....(?<path_info>.*) {

```

ie is this ammended version correct in the face of a URL such as:

```
 blah.php/some/path?param=2

```

Thanks

Ed W

---

<div class="post-metadata">

**Author:** ![ewildgoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bcef8e/32.png) [@ewildgoose](https://www.ruby-forum.com/u/ewildgoose)\
**Post date:** [February 22, 2013, 11:06pm UTC](https://www.ruby-forum.com/t/how-to-remove-the-if-in-this-fcgi-config/226809/4 "2013-02-22T23:06:16Z")

</div>

On Saturday 23 February 2013 00:11:26 Francis D. wrote:

> You probably want
> 
> ```
> location ~ ^(?<script_name>.+\.php)(?<path_info>/.*|$) {
> 
> ```
> 
> because you want to match /X.php or /X.php/X but not /X.phpX.

IMHO,

location ~ ^(?\<script\_name\>.+.php)(?\<path\_info\>/.\*)?$ {

looks better.

% pcretest -b -d -m -s+  
PCRE version 8.30 2012-02-04

## re\> !^(?\<script\_name\>.+.php)(?\<path\_info\>/.\*|$)! Memory allocation (code space): 42 Memory allocation (JIT code): 1739

## 0 38 Bra 3 ^ 4 15 CBra 1 9 Any+ 11 .php 19 15 Ket 22 9 CBra 2 27 / 29 Any\* 31 4 Alt 34 $ 35 13 Ket 38 38 Ket 41 End

Capturing subpattern count = 2  
Named capturing subpatterns:  
path\_info 2  
script\_name 1  
Options: anchored  
No first char  
Need char = ‘p’  
Subject length lower bound = 5  
No set of starting bytes  
data\>

## re\> !^(?\<script\_name\>.+.php)(?\<path\_info\>/.\*)?$! Memory allocation (code space): 40 Memory allocation (JIT code): 1732

## 0 36 Bra 3 ^ 4 15 CBra 1 9 Any+ 11 .php 19 15 Ket 22 Brazero 23 9 CBra 2 28 / 30 Any\* 32 9 Ket 35 $ 36 36 Ket 39 End

Capturing subpattern count = 2  
Named capturing subpatterns:  
path\_info 2  
script\_name 1  
Options: anchored  
No first char  
Need char = ‘p’  
Subject length lower bound = 5  
No set of starting bytes

wbr, Valentin V. Bartenev

–

> **[F5 NGINX: Built for cloud-native apps. Ready for AI traffic.](https://www.f5.com/products/nginx)**
>
> Deliver modern applications with NGINX. Explore high-performance load balancing, API gateways, and web server solutions trusted by top global enterprises.

> **[nginx: donation](http://nginx.org/en/donation.html)**

---

<div class="post-metadata">

**Author:** ![ewildgoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bcef8e/32.png) [@ewildgoose](https://www.ruby-forum.com/u/ewildgoose)\
**Post date:** [February 22, 2013, 9:12pm UTC](https://www.ruby-forum.com/t/how-to-remove-the-if-in-this-fcgi-config/226809/5 "2013-02-22T21:12:17Z")

</div>

On Thu, Feb 21, 2013 at 07:08:56PM +0000, Ed W wrote:

> On 21/02/2013 17:54, Igor S. wrote:

Hi there,

> > ```
> > location ~ ^(?<script_name>.+\.php)(?<path_info>/|$) {
> > 
> > ```

> Can I ask you to confirm the correction of a typo in your answer. Do I  
> want this:
> 
> ```
> ....(?<path_info>.*) {
> 
> ```

You probably want

```
           location ~ ^(?<script_name>.+\.php)(?<path_info>/.*|$) {

```

because you want to match /X.php or /X.php/X but not /X.phpX.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ewildgoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bcef8e/32.png) [@ewildgoose](https://www.ruby-forum.com/u/ewildgoose)\
**Post date:** [February 23, 2013, 2:42pm UTC](https://www.ruby-forum.com/t/how-to-remove-the-if-in-this-fcgi-config/226809/6 "2013-02-23T14:42:08Z")

</div>

On Sat, Feb 23, 2013 at 02:05:56AM +0400, Valentin V. Bartenev wrote:

> On Saturday 23 February 2013 00:11:26 Francis D. wrote:

> > ```
> > location ~ ^(?<script_name>.+\.php)(?<path_info>/.*|$) {
> > 
> > ```

> location ~ ^(?\<script\_name\>.+.php)(?\<path\_info\>/.\*)?$ {
> 
> looks better.

Agreed.

And it means I don’t have to think about whether AB|C groups as (AB)|(C)  
or (A)(B|C) in this regex implementation 🙂

> % pcretest -b -d -m -s+

Using smaller Memory allocation numbers is an extra bonus.

Cheers,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ewildgoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bcef8e/32.png) [@ewildgoose](https://www.ruby-forum.com/u/ewildgoose)\
**Post date:** [February 24, 2013, 8:41pm UTC](https://www.ruby-forum.com/t/how-to-remove-the-if-in-this-fcgi-config/226809/7 "2013-02-24T20:41:09Z")

</div>

On 2013-02-22 23:05, Valentin V. Bartenev wrote:

> IMHO,
> 
> location ~ ^(?\<script\_name\>.+.php)(?\<path\_info\>/.\*)?$ {
> 
> looks better.

In order to put yet another iteration into the game, I prefer the  
script\_name part to be non-greedy

```
 location ~ ^(?<script_name>.+?\.php)(?<path_info>/.*)?$ {

```

Of course it still depends on someone’s preference what to match in  
case of

```
 /foo.php/bar.php/quux

```

-cs

---

<div class="post-metadata">

**Author:** ![ewildgoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bcef8e/32.png) [@ewildgoose](https://www.ruby-forum.com/u/ewildgoose)\
**Post date:** [February 25, 2013, 10:20pm UTC](https://www.ruby-forum.com/t/how-to-remove-the-if-in-this-fcgi-config/226809/8 "2013-02-25T22:20:20Z")

</div>

On 24/02/2013 19:40, Christoph Schug wrote:

> ```
> location ~ ^(?<script_name>.+?\.php)(?<path_info>/.*)?$ {
> 
> ```
> 
> Of course it still depends on someone’s preference what to match in  
> case of
> 
> ```
> /foo.php/bar.php/quux
> 
> ```
> 
> -cs

You beat me - I was just about to post the same!

I think the non greedy is probably the more common case

Thanks!

Ed W
