Requests ending with / (slash) are returning 404

Igor Sysoev igor at sysoev.ru
Wed Apr 7 13:03:35 MSD 2010


On Wed, Apr 07, 2010 at 06:58:19PM +1000, Leonardo Crespo 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
> (http://wiki.nginx.org/NginxHttpCoreModule#try_files) 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

> Best,
> 
> Leonardo.
> 
> 2010/4/7 Igor Sysoev <igor at sysoev.ru>:
> > On Wed, Apr 07, 2010 at 06:24:36PM +1000, Leonardo Crespo wrote:
> >
> >> 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 (www.domain.com/page shows the content of
> >> www.domain.com/page.php)
> >>
> >> 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;
> >>     }
> >> }
> >> ============
> >
> >      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 Sysoev
> > http://sysoev.ru/en/
> >
> > _______________________________________________
> > nginx mailing list
> > nginx at nginx.org
> > http://nginx.org/mailman/listinfo/nginx
> >
> 
> _______________________________________________
> nginx mailing list
> nginx at nginx.org
> http://nginx.org/mailman/listinfo/nginx

-- 
Igor Sysoev
http://sysoev.ru/en/



More information about the nginx mailing list