# Another rewrite/try files issue

**URL:** <https://www.ruby-forum.com/t/another-rewrite-try-files-issue/207295>\
**Category:** NGINX\
**Created:** [May 22, 2011, 11:21pm UTC](https://www.ruby-forum.com/t/another-rewrite-try-files-issue/207295 "2011-05-22T23:21:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [May 22, 2011, 11:21pm UTC](https://www.ruby-forum.com/t/another-rewrite-try-files-issue/207295/1 "2011-05-22T23:21:11Z")

</div>

Hello,

I am having the following problem:

- I want to have a “virtual” url e.g. “/myapp” to have a totally  
different document root e.g. “/home/stelios/myapp” (instead of the  
default ‘/var/www’)
- locations of the type “/myapp/css/print.css” should respond with  
the specific file at the “/home/stelios/myapp” root (i.e. with  
“/home/stelios/myapp/css/print.css”
- if the the file requested does not exist, then the “index.php”  
should be called via FastCGI with args “?u=$uri” (e.g.  
“/myapp/users/12” should be rewritten to  
“/home/stelios/myapp/index.php?u=users/12”)

I have tried the following:

location ~ /myapp/(.\*) {  
root /home/stelios/myapp;  
try\_files /$1 /$1/ /index.php?u=$1;  
}

# and the “default” PHP/FastCGI

location ~ .php$ {  
fastcgi\_pass 127.0.0.1:9000;  
fastcgi\_index index.php;  
fastcgi\_param SCRIPT\_FILENAME  
$document\_root$fastcgi\_script\_name;  
include fastcgi\_params;  
}

but it seems that it doesn’t work for the non files. In particular it  
seems that after the internal redirect to index.php, the root is  
overwritten and instead of calling my /home/stelios/myapp/index.php  
the one at the default root ("/var/www/index.php") is beeing called.

Do you know what’s wrong? And possibl another way to achieve what I  
want?

Thank you very much in advance!

P.S. I am using nginx-0.8.4 on Ubuntu

St.

---

<div class="post-metadata">

**Author:** ![Stelianos\_GSSfakia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e99b99/32.png) [@Stelianos\_GSSfakia](https://www.ruby-forum.com/u/Stelianos_GSSfakia)\
**Post date:** [May 23, 2011, 12:06am UTC](https://www.ruby-forum.com/t/another-rewrite-try-files-issue/207295/2 "2011-05-23T00:06:14Z")

</div>

## Stelianos G. Sfakianakis Wrote:

> should respond with  
> I have tried the following:  
> fastcgi\_param SCRIPT\_FILENAME  
> /home/stelios/myapp/index.php  
> St.
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

Possibly this:

location /myapp/ {  
root /home/stelios;  
try\_files $uri @fallback;  
}

# and the “default” PHP/FastCGI

location @fallback {  
fastcgi\_pass 127.0.0.1:9000;  
fastcgi\_param SCRIPT\_FILENAME /home/stelios/myapp/index.php;  
include fastcgi\_params;  
}

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

---

<div class="post-metadata">

**Author:** ![Stelianos\_GSSfakia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e99b99/32.png) [@Stelianos\_GSSfakia](https://www.ruby-forum.com/u/Stelianos_GSSfakia)\
**Post date:** [May 23, 2011, 8:54am UTC](https://www.ruby-forum.com/t/another-rewrite-try-files-issue/207295/3 "2011-05-23T08:54:02Z")

</div>

On 23/5/2011 1:06 πμ, brianmercer wrote:

> fastcgi\_param SCRIPT\_FILENAME /home/stelios/myapp/index.php;  
> include fastcgi\_params;  
> }

Thanks Brian, that _almost_ works (although I don’t like repeating [most  
of] the fastcgi config..)

But I also need to pass _part_ of the original path as a query string. I  
tried to following :

location @fallback {  
fastcgi\_pass 127.0.0.1:9000;  
include fastcgi\_params;  
fastcgi\_param SCRIPT\_FILENAME /home/stelios/myapp/index.php;  
fastcgi\_param QUERY\_STRING u=$uri&$args;  
}

which works but now for the url “…/myapp/users” the script gets a  
query string like this: “u=/myapp/users” whereas I want it to be  
“u=/users” (i.e. remove the “base” path). Is there any simple solution  
to that? (e.g. can we pass parameters to a named location?)

Thanks!  
Stelios

---

<div class="post-metadata">

**Author:** ![Stelianos\_GSSfakia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e99b99/32.png) [@Stelianos\_GSSfakia](https://www.ruby-forum.com/u/Stelianos_GSSfakia)\
**Post date:** [May 23, 2011, 9:31pm UTC](https://www.ruby-forum.com/t/another-rewrite-try-files-issue/207295/4 "2011-05-23T21:31:56Z")

</div>

2011/5/23 brianmercer [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> include fastcgi\_params;  
> }

Thanks again!

I finally did the following which seems to work just fine:

location ~ /myapp/(.\*) {  
root /home/stelios/myapp;  
set $args u=/$1&$args;  
try\_files /$1 @fallback;  
}  
location @fallback {  
root /home/stelios/myapp;  
fastcgi\_pass 127.0.0.1:9000;  
include fastcgi\_params;  
fastcgi\_param SCRIPT\_FILENAME $document\_root/index.php;  
}

The “set $args …” directive augments the QUERY\_STRING with the  
original uri that is kept to the subsequent fastcgi call.

Best regards  
Stelios

---

<div class="post-metadata">

**Author:** ![Stelianos\_GSSfakia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e99b99/32.png) [@Stelianos\_GSSfakia](https://www.ruby-forum.com/u/Stelianos_GSSfakia)\
**Post date:** [May 23, 2011, 8:03pm UTC](https://www.ruby-forum.com/t/another-rewrite-try-files-issue/207295/5 "2011-05-23T20:03:28Z")

</div>

## Stelios Sfakianakis Wrote:

> > fastcgi\_pass 127.0.0.1:9000;  
> > But I also need to pass _part_ of the original
> 
> Thanks!  
> Stelios
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

Oh, I see. Maybe this:

location ~ /myapp/(?.\*) {  
root /home/stelios;  
try\_files $uri /myapp/index.php?u=/$myquery;  
}

location = /myapp/index.php {  
fastcgi\_pass 127.0.0.1:9000;  
fastcgi\_param SCRIPT\_FILENAME /home/stelios/myapp/index.php;  
include fastcgi\_params;  
}

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

---

<div class="post-metadata">

**Author:** ![Stelianos\_GSSfakia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e99b99/32.png) [@Stelianos\_GSSfakia](https://www.ruby-forum.com/u/Stelianos_GSSfakia)\
**Post date:** [May 23, 2011, 10:26pm UTC](https://www.ruby-forum.com/t/another-rewrite-try-files-issue/207295/6 "2011-05-23T22:26:24Z")

</div>

On 23 Mai 2011 20h31 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > fastcgi\_param SCRIPT\_FILENAME /home/stelios/myapp/index.php;  
> > try\_files /$1 @fallback;  
> > }

You can do it without any set directive. Try this.

location ~ /myapp/(?.\*)$ {  
root /home/stelios/myapp;  
try\_files /$myquery /index.php?u=/$myquery&$args;  
}

location = /index.php {  
root /home/stelios/myapp;  
fastcgi\_pass 127.0.0.1:9000;  
include fastcgi\_params;

## Remove the line below if already defined in fastcgi\_params.

fastcgi\_param SCRIPT\_FILENAME $document\_root$fastcgi\_script\_name;  
}

— appa

---

<div class="post-metadata">

**Author:** ![Stelianos\_GSSfakia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e99b99/32.png) [@Stelianos\_GSSfakia](https://www.ruby-forum.com/u/Stelianos_GSSfakia)\
**Post date:** [May 23, 2011, 10:02pm UTC](https://www.ruby-forum.com/t/another-rewrite-try-files-issue/207295/7 "2011-05-23T22:02:52Z")

</div>

On Mon, May 23, 2011 at 12:20:55AM +0300, Stelianos G. Sfakianakis  
wrote:

> - if the the file requested does not exist, then the “index.php”
> 
> # and the “default” PHP/FastCGI
> 
> seems that after the internal redirect to index.php, the root is  
> overwritten and instead of calling my /home/stelios/myapp/index.php  
> the one at the default root (“/var/www/index.php”) is beeing called.
> 
> Do you know what’s wrong? And possibl another way to achieve what I want?

location /myapp/css/ {  
root /home/stelios;  
}

location /myapp/ {  
root /home/stelios;  
try\_files $uri $uri/ @php;  
}

location @php {  
fastcgi\_pass 127.0.0.1:9000;  
fastcgi\_param SCRIPT\_FILENAME /home/stelios/myapp/index.php;  
fastcgi\_param QUERY\_STRING u=$uri;  
include fastcgi\_params0;  
}

fastcgi\_params0 is a copy of fastcgi\_params without QUERY\_STRING  
parameter.

–  
Igor S.
