Another rewrite/try files issue

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.

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
[email protected]
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

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

2011/5/23 brianmercer [email protected]:

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

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
[email protected]
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

On 23 Mai 2011 20h31 WEST, [email protected] 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

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs