Requests ending with / (slash) are returning 404

Hi all!

Can anyone with more experience give me hand? I’ve spent the last 4
hours trying many options and none of them worked, so I decided to ask
for help.

What i’m trying to do:
/foo/bar/page served by /foo/bar/page.php

This one is working fine, as I can remove the.php extension from the
request and the page loads o (Default title | Domain.com shows the content of
Default title | Domain.com)

However, requests ending in a / (slash) are not working
/foo/bar/ served by /foo/bar/index.php
(www.domain.com/portal/ should show the content of
www.domain.com/portal/index.php)

I can’t get this to work properly no matter what. I’ve tried if
conditions for the $request_uri, different location groups, flags for
the rewrite option and sadly i can’t get it to work.

Can anyone with more experience give me a hand? Here’s my conf file
(snippet)

============
server
{
   listen  80;
   server_name www.domain.com.au;
   access_log /home/public_html/domain.com.au/log/access.log;
   error_log /home/public_html/domain.com.au/log/error.log error;
   root /home/public_html/domain.com.au/public;

   #file size
   client_max_body_size 100m;

   location = /
   {
      root  /home/public_html/domain.com.au/public/;
      index index.php index.html;
   }

   location /
   {
      root  /home/public_html/domain.com.au/public/;
      index index.php index.html;
      ####### ---- allows /foo/bar/page to /foo/bar/page.php
      rewrite ^([^.?]+)$ $request_uri.php last;
   }

   # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
   location ~ .php$
   {
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_intercept_errors on;
      fastcgi_index index.php;
      include /usr/local/nginx/conf/fastcgi_params;
      fastcgi_param SCRIPT_FILENAME
/home/public_html/domain.com.au/public/$fastcgi_script_name;
   }
}

2010/4/7 Igor S. [email protected]:

   }

wouldn’t static files also passed to fastcgi in this case?

how about

location / {
try_files $uri @php;
}
location @php {
try_files $uri.php $uri/ =404;
…fastcgi stuff…
}

?

On Wed, Apr 07, 2010 at 03:50:47PM +0700, Edho P Arief wrote:

š š š}
š š š š š š š /home/public_html/domain.com.au/public/$fastcgi_script_name;
location @php {
try_files $uri.php $uri/ =404;
…fastcgi stuff…
}

?

Yes, you are right. Also you need to add

š šlocation ~ .php$ {
…fastcgi stuff…

to prevent handling PHP files as static.


Igor S.
http://sysoev.ru/en/

On Wed, Apr 07, 2010 at 06:24:36PM +1000, Leonardo C. wrote:

request and the page loads o (Default title | Domain.com shows the content of

ššš root /home/public_html/domain.com.au/public;
ššš location /
ššš ššš fastcgi_pass 127.0.0.1:9000;
ššš ššš fastcgi_intercept_errors on;
ššš ššš fastcgi_index index.php;
ššš ššš include /usr/local/nginx/conf/fastcgi_params;
ššš ššš fastcgi_param SCRIPT_FILENAME
/home/public_html/domain.com.au/public/$fastcgi_script_name;
ššš }
}

  location / {
      try_files  $uri  $uri.php  $uri/   =404;

ššš ššš fastcgi_pass 127.0.0.1:9000;
ššš ššš fastcgi_intercept_errors on;
ššš ššš fastcgi_index index.php;
ššš ššš include /usr/local/nginx/conf/fastcgi_params;
ššš ššš fastcgi_param SCRIPT_FILENAME
/home/public_html/domain.com.au/public/$fastcgi_script_name;
}

or

  location / {
      try_files  $uri  $uri.php  ${uri}index.php   =404;

ššš ššš fastcgi_pass 127.0.0.1:9000;
ššš ššš fastcgi_intercept_errors on;
ššš ššš include /usr/local/nginx/conf/fastcgi_params;
ššš ššš fastcgi_param SCRIPT_FILENAME
/home/public_html/domain.com.au/public/$fastcgi_script_name;
}


Igor S.
http://sysoev.ru/en/

Wow, very elegant Igor. Thank you, it works beautifully.

Could you help with the last bit?

I’d like to return 404 if the user tries /foo/page.php instead of
/foo/page

I read the nginx wiki for try_files
(Module ngx_http_core_module) and coudn’t find
the advanced syntax that you’ve given me to use (and that it works
great). Is there any other nginx info resource that you could share?

Best,

Leonardo.

2010/4/7 Igor S. [email protected]:

On Wed, Apr 7, 2010 at 3:58 PM, Leonardo C. [email protected]
wrote:

Wow, very elegant Igor. Thank you, it works beautifully.

Could you help with the last bit?

I’d like to return 404 if the user tries /foo/page.php instead of /foo/page

location / {
try_files $uri @php;
}
location @php {
try_files $uri.php $uri/ =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_intercept_errors on;
fastcgi_index index.php;
include /usr/local/nginx/conf/fastcgi_params;
fastcgi_param SCRIPT_FILENAME
/home/public_html/domain.com.au/public/$fastcgi_script_name;
}
location ~ .php$ {
return 404;
}


O< ascii ribbon campaign - stop html mail - www.asciiribbon.org

On Wed, Apr 07, 2010 at 06:58:19PM +1000, Leonardo C. wrote:

Wow, very elegant Igor. Thank you, it works beautifully.

Could you help with the last bit?

I’d like to return 404 if the user tries /foo/page.php instead of /foo/page

location ~ .php$ {
return 404;
}

I read the nginx wiki for try_files
(Module ngx_http_core_module) and coudn’t find
the advanced syntax that you’ve given me to use (and that it works
great). Is there any other nginx info resource that you could share?

This wiki article is invalid, it should be fixed.
Here is one example of try_files usage:
http://nginx.org/en/docs/http/converting_rewrite_rules.html#converting_mongrel_rules

hours trying many options and none of them worked, so I decided to ask
/foo/bar/ served by /foo/bar/index.php

ššš ššš ####### ---- allows /foo/bar/page to /foo/bar/page.php
ššš ššš fastcgi_param SCRIPT_FILENAME
šššš ššš fastcgi_index index.php;
šššš ššš fastcgi_pass 127.0.0.1:9000;


nginx mailing list
[email protected]
nginx Info Page


nginx mailing list
[email protected]
nginx Info Page


Igor S.
http://sysoev.ru/en/

Hi Edho, thanks.

Igor mentioned i should keep this to avoid serving php as static files.

location ~ .php$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_intercept_errors on;
fastcgi_index index.php;
include /usr/local/nginx/conf/fastcgi_params;
fastcgi_param SCRIPT_FILENAME
/home/public_html/boysdownunder.com.au/public/$fastcgi_script_name;
}

Will it conflict with this?

location ~ .php$ {
return 404;
}

Since i don’t want direct request to .php files anyway, I assume it
won’t but just to be safe.

Cheers

On Wed, Apr 7, 2010 at 4:10 PM, Leonardo C. [email protected]
wrote:

        fastcgi_param SCRIPT_FILENAME
/home/public_html/boysdownunder.com.au/public/$fastcgi_script_name;
    }

Will it conflict with this?

location ~ .php$ {
 return 404;
}

as all php requests (usin are passed to @php block (or / block in
Igor’s example), there shouldn’t be any problem.

And in my example it should be

try_files $uri.php ${uri}index.php =404;

instead of

try_files $uri.php $uri/ =404;


O< ascii ribbon campaign - stop html mail - www.asciiribbon.org

On Wed, Apr 07, 2010 at 07:10:42PM +1000, Leonardo C. wrote:

fastcgi_param SCRIPT_FILENAME

won’t but just to be safe.
You should use the later variant.
The former should be used if you want to handle something like
“/foo/page.php”.

š š/home/public_html/domain.com.au/public/$fastcgi_script_name;
nginx mailing list
[email protected]
nginx Info Page


nginx mailing list
[email protected]
nginx Info Page


Igor S.
http://sysoev.ru/en/

I can’t make sense of those 2.

try_files $uri.php ${uri}index.php =404;

This means: if the uri is xxx, try to serve xxx.php. If you can’t,
try serving xxxindex.php. If you can’t still, return 404.

try_files $uri.php $uri/ =404;

This means: if the uri is xxx, try to serve xxx.php. If you can’t,
try serving xxx/ . If you can’t still, return 404.

Correct? They both work fine, what is the difference between them?

Just curious,

Why the {} in ${uri}index.php and not in $uri.php?

I’m assuming it’s to identify uri as a keyword, but wouldn’t it be
necessary on the $uri.php as well?

On Wed, Apr 07, 2010 at 07:26:31PM +1000, Leonardo C. wrote:

I can’t make sense of those 2.

try_files $uri.php ${uri}index.php =404;

This means: if the uri is xxx, try to serve xxx.php. If you can’t,
try serving xxxindex.php. If you can’t still, return 404.

Yes. Note, that ${uri}index.php is for “xxx/”, i.e. “xxx/index.php”.

try_files $uri.php $uri/ =404;

This means: if the uri is xxx, try to serve xxx.php. If you can’t,
try serving xxx/ . If you can’t still, return 404.

Correct? They both work fine, what is the difference between them?

The trailing slash in “$uri/” tests that $uri, i.e., “xxx” or “xxx/”
is a directory. Further directory handling depends on directives.

If you use

location / {
    try_file  ... $uri/  ...
    index     index.php;
}

then nginx will test index file for request, look
http://nginx.org/en/docs/http/request_processing.html#simple_php_site_configuration

If you use

location / {
    try_file  ... $uri/  ...
    fastcgi_pass      ...
    fastcgi_index     index.php;
}

then nginx will add index.php to $uri in $fastcgi_script_name
for request with trailing slash.

š š š š š š š šfastcgi_index index.php;

štry_files $uri.php $uri/ =404;


nginx mailing list
[email protected]
nginx Info Page


Igor S.
http://sysoev.ru/en/

Thanks Igor, it makes sense now. Thanks for the link it was helpful too.

Cheers

Leo

2010/4/7 Igor S. [email protected]:

On Wed, Apr 07, 2010 at 07:30:22PM +1000, Leonardo C. wrote:

Just curious,

Why the {} in ${uri}index.php and not in $uri.php?

I’m assuming it’s to identify uri as a keyword, but wouldn’t it be
necessary on the $uri.php as well?

You may use ${uri}.php, but “.” means already the end of keyword.

This means: if the uri is xxx, try to serve xxx.php. šIf you can’t,

location ~ .php$

štry_files $uri.php ${uri}index.php =404;
nginx mailing list
[email protected]
nginx Info Page


nginx mailing list
[email protected]
nginx Info Page


Igor S.
http://sysoev.ru/en/

I’d to enable the php extension (/foo/page.php) when the url is
domain.com.au/portal/admin*

Here’s my failed attempt and the error I’m getting. I’ll make sure to
keep reading as much as possible and not to ask questions that are
already answered.


location / {
try_files $uri @php;
}
location @php {
try_files $uri.php ${uri}index.php =404;

fastcgi_pass 127.0.0.1:9000;
fastcgi_intercept_errors on;
fastcgi_index index.php;
include /usr/local/nginx/conf/fastcgi_params;
fastcgi_param SCRIPT_FILENAME
/home/public_html/domain.com.au/public/$fastcgi_script_name;
}

location ~ .php$ {
#if the uri is not /portal/admin, return 404, disabling the php
extension on the rest of the site
if (!$uri ~* /portal/admin) {
return 404;
}
}

Starting nginx: [emerg]: invalid condition “!$request_uri” in
/usr/local/nginx/sites-enabled/domain.com.au:37

Not sure if the syntax is valid and also if I’m putting it in the
right place as to avoid conflict with fastcgi.

Thanks again for the help.

Leonardo.

2010/4/7 Igor S. [email protected]:

When i put this

location ~ ^/portal/admin/.*.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_intercept_errors on;
fastcgi_index index.php;
include /usr/local/nginx/conf/fastcgi_params;
fastcgi_param SCRIPT_FILENAME
/home/public_html/domain.com.au/public/$fastcgi_script_name;
}

i get this error:

Starting nginx: [emerg]: directive “try_files” is not terminated by
“;” in /usr/local/nginx/sites-enabled/domain.com.au:26

line 26 is this: try_files $uri.php ${uri}index.php =404;

When i remove this:

location ~ ^/portal/admin/.*.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_intercept_errors on;
fastcgi_index index.php;
include /usr/local/nginx/conf/fastcgi_params;
fastcgi_param SCRIPT_FILENAME
/home/public_html/boysdownunder.com.au/public/$fastcgi_script_name;
}

The error dissapears… No idea what’s going on as one thing should
affect the other and the error message is wrong, as I’m definitely
ending the statement with a ;

On Wed, Apr 7, 2010 at 5:40 PM, Leonardo C. [email protected]
wrote:

}

location ~ .php$ {
return 404;
}
location ~ ^/portal/admin/.*.php$ {
…fastcgi stuff…
}

Not sure if the syntax is valid and also if I’m putting it in the
right place as to avoid conflict with fastcgi.


O< ascii ribbon campaign - stop html mail - www.asciiribbon.org