# Try-files and Gallery3 rewriting

**URL:** <https://www.ruby-forum.com/t/try-files-and-gallery3-rewriting/208046>\
**Category:** NGINX\
**Created:** [June 8, 2011, 11:43pm UTC](https://www.ruby-forum.com/t/try-files-and-gallery3-rewriting/208046 "2011-06-08T23:43:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [June 8, 2011, 11:43pm UTC](https://www.ruby-forum.com/t/try-files-and-gallery3-rewriting/208046/1 "2011-06-08T23:43:40Z")

</div>

Hiho,

I am currently running gallery3 on nginx/php-fpm. Works like a charm so  
far.  
But i’d like to get gallery3 also under the hood of try-files, but  
didn’t  
get it running up to now.

The working (and evil) if rewrite code looks like this:

```
 location /gallery3/ {
            if (!-e $request_filename) {
                 rewrite ^/gallery3/index.php/(.+)$

```

/gallery3/index.php?kohana\_uri=$1 last;  
rewrite ^/gallery3/(.+)$  
/gallery3/index.php?kohana\_uri=$1 last;  
rewrite ^/gallery3/$  
/gallery3/index.php?kohana\_uri=/  
last;  
}

```
    }

```

Could anybody supply a correct try\_files directive and explain how and  
why  
try\_files should do the stuff in this case?

Thank you very much!

## Andy

Andreas Lehr

mail: removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Andreas\_Lehr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bc8723/32.png) [@Andreas\_Lehr](https://www.ruby-forum.com/u/Andreas_Lehr)\
**Post date:** [June 9, 2011, 12:28am UTC](https://www.ruby-forum.com/t/try-files-and-gallery3-rewriting/208046/2 "2011-06-09T00:28:05Z")

</div>

On 8 Jun 2011 22h43 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> rewrite ^/gallery3/index.php/(.+)$  
> /gallery3/index.php?kohana\_uri=$1 last;  
> rewrite ^/gallery3/(.+)$  
> /gallery3/index.php?kohana\_uri=$1 last;  
> rewrite ^/gallery3/$ /gallery3/index.php?kohana\_uri=/  
> last;  
> }
> 
> }  
> Could anybody supply a correct try\_files directive and explain how  
> and why try\_files should do the stuff in this case?

Try this:

location /gallery3 {

location /gallery3(.\*)$ {  
try\_files $uri /gallery3/index.php$1 /gallery3$1  
/gallery3/index.php?kohana\_uri=$1;  
}

## You need this here to make it work with the nested location.

location /gallery3/index.php {  
(fastcgi or proxy\_pass stuff here)  
}  
}

— appa

---

<div class="post-metadata">

**Author:** ![Andreas\_Lehr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bc8723/32.png) [@Andreas\_Lehr](https://www.ruby-forum.com/u/Andreas_Lehr)\
**Post date:** [June 9, 2011, 12:36am UTC](https://www.ruby-forum.com/t/try-files-and-gallery3-rewriting/208046/3 "2011-06-09T00:36:42Z")

</div>

On 8 Jun 2011 23h26 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > location /gallery3/ {  
> > Could anybody supply a correct try\_files directive and explain how  
> > and why try\_files should do the stuff in this case?

Oops. There was a superfluous URI being tried. Here it is:

location /gallery3 {

location /gallery3(.\*)$ {  
try\_files $uri /gallery3/index.php$1  
/gallery3/index.php?kohana\_uri=$1;  
}

## You need this here to make it work with the nested location.

location /gallery3/index.php {  
(fastcgi or proxy\_pass stuff here)  
}  
}

— appa

---

<div class="post-metadata">

**Author:** ![Andreas\_Lehr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bc8723/32.png) [@Andreas\_Lehr](https://www.ruby-forum.com/u/Andreas_Lehr)\
**Post date:** [June 9, 2011, 9:40am UTC](https://www.ruby-forum.com/t/try-files-and-gallery3-rewriting/208046/4 "2011-06-09T09:40:15Z")

</div>

On Wed, Jun 08, 2011 at 11:43:16PM +0200, Andreas Lehr wrote:

> ```
> rewrite ^/gallery3/index.php/(.+)$
> 
> ```
> 
> try\_files should do the stuff in this case?  
> It seems that “rewrite ^/gallery3/$ …” is not used since “/gallery3”  
> directory should exist.

The way I prefer to use:

location /gallery3/ {

```
   root /path/to/gallery3;

   location /gallery3/index.php/(.+)$ {
       try_files $uri /gallery3/index.php?kohana_uri=$1;
   }

   location /gallery3(/.+)$ {
       try_files $uri /gallery3/index.php?kohana_uri=$1;
   }

   location = /gallery3/index.php {
       fastcgi_pass backend;
       fastcgi_param SCRIPT_FILENAME /path/to/gallery3/index.php;
       include fastcgi_params;
   }

```

}

–  
Igor S.

---

<div class="post-metadata">

**Author:** ![Andreas\_Lehr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bc8723/32.png) [@Andreas\_Lehr](https://www.ruby-forum.com/u/Andreas_Lehr)\
**Post date:** [June 9, 2011, 10:11am UTC](https://www.ruby-forum.com/t/try-files-and-gallery3-rewriting/208046/5 "2011-06-09T10:11:09Z")

</div>

On Thu, Jun 09, 2011 at 11:39:55AM +0400, Igor S. wrote:

> > ```
> > if (!-e $request_filename) {
> > 
> > ```
> > 
> > Could anybody supply a correct try\_files directive and explain how and why
> 
> ```
> fastcgi_param SCRIPT_FILENAME /path/to/gallery3/index.php;
> include fastcgi_params;
> }
> 
> ```
> 
> }

Or better

```
location /gallery3/ {

    root /path/to/gallery3;

    location /gallery3/(index\.php/)?(.+)$ {
        try_files $uri /gallery3/index.php?kohana_uri=$2;
    }

    location = /gallery3/index.php {
        fastcgi_pass backend;
        fastcgi_param SCRIPT_FILENAME 

```

/path/to/gallery3/index.php;  
include fastcgi\_params;  
}  
}

–  
Igor S.

---

<div class="post-metadata">

**Author:** ![Andreas\_Lehr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bc8723/32.png) [@Andreas\_Lehr](https://www.ruby-forum.com/u/Andreas_Lehr)\
**Post date:** [June 9, 2011, 10:13am UTC](https://www.ruby-forum.com/t/try-files-and-gallery3-rewriting/208046/6 "2011-06-09T10:13:39Z")

</div>

On Thu, Jun 09, 2011 at 12:10:51PM +0400, Igor S. wrote:

- 

```
    location /gallery3/(index\.php/)?(.+)$ {

```

- 

```
    location ~ /gallery3/(index\.php/)?(.+)$ {

```

–  
Igor S.
