Forum: NGINX Questions about Internal named location

Posted by heimdull (Guest)
on 2010-07-30 20:32
(Received via mailing list)
I have a problem I can't find an answer too and I'm wondering if I'm
doing the named internal location all wrong. Here is an example of my
config

location / {
    try_files $uri $uri/ @www;
}

location @www {
    include fastcgi_params;

    fastcgi_index   index.php;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass    myphp;
}

When I go to /index.php I get the index.php recognized as a binary file
and the browser just wants to download the file. The file that is
downloaded is correct html so I know that the file hit myphp before it
was sent to my browser.

If I add this location:

location ~ \.php$ {
     fastcgi_pass    myphp;
     fastcgi_param   SCRIPT_FILENAME
$document_root$fastcgi_script_name;
}

index.php is displayed in the browser correctly so my question is why
does the @www location not correctly handle the php file?

This all started when I tried to setup one nginx server that has
multiple php sites under one url that needs a alias for the other sites.
Something like this:

root /var/www/somesite;

location / {
    index.html index.php;
}
location /wordpress {
    alias /var/www/wordpress;
    try_files $uri $uri/ @wordpress;
}
....

Can anyone shed some light on how the try_files/named location works?
I'm using Nginx 0.8.36 btw

Posted at Nginx Forum: 
http://forum.nginx.org/read.php?2,114926,114926#msg-114926
Posted by Rob Schultz (schultz)
on 2010-07-30 21:38
(Received via mailing list)
Hi,

On Jul 30, 2010, at 8:31 PM, heimdull wrote:

> 
> index.php is displayed in the browser correctly so my question is why
> does the @www location not correctly handle the php file?
> 
its because index.php is a real file. It never gets passed off to @www. 
try_files is for testing real files on the filesystem. And returns that 
url if it is successful. If it is unsuccesful it is passed off to @www.

>    alias /var/www/wordpress;
>    try_files $uri $uri/ @wordpress;
> }
> .... 

from the wiki http://wiki.nginx.org/Wordpress
might try this format
location /wordpress {
  try_files $uri $uri/ /wordpress/index.php?q=$uri&args;
}

and then have your normal php location block for fastcgi settings.
Posted by Igor Sysoev (Guest)
on 2010-07-30 21:47
(Received via mailing list)
On Fri, Jul 30, 2010 at 09:26:42PM +0200, Rob Schultz wrote:

> >    alias /var/www/wordpress;
> and then have your normal php location block for fastcgi settings. 
It's better to set a script name and a query string directly in
fastcgi_param to avoid surplus copy operations:

location /wordpress {
    root  /var/www;
    try_files $uri $uri/ @wordpress;
}

location @wordpress {
    ...
    fastcgi_param   SCRIPT_FILENAME /var/www/wordpress/index.php;
    fastcgi_param   QUERY_STRING    q=$uri&$args;
}


--
Igor Sysoev
http://sysoev.ru/en/
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.