13

Im banging my head against a table trying to figure out what is causing redirection cycle in my nginx configuration when trying to access URL which does not exist Configuration goes as follows:

server {
        listen       127.0.0.1:8080;
        server_name  .somedomain.com;
    root  /var/www/somedomain.com;

        access_log /var/log/nginx/somedomain.com-access.nginx.log;
    error_log  /var/log/nginx/somedomain.com-error.nginx.log debug;

        location ~* \.php.$ {
        # Proxy all requests with an URI ending with .php*
        # (includes PHP, PHP3, PHP4, PHP5...)
        include /etc/nginx/fastcgi.conf;
        }

        # all other files
        location / {
            root  /var/www/somedomain.com;
        try_files $uri $uri/ ;
        }

    error_page 404 /errors/404.html;
        location /errors/ {
                alias /var/www/errors/;
        }       

        #this loads custom logging configuration which disables favicon error logging
        include /etc/nginx/drop.conf;
}

this domain is a simple STATIC HTML site just for some testing purposes. I'd expect that the error_page directive would kick in in response to PHP-FPM not being able to find given files as I have fastcgi_intercept_errors on; in http block and nave error_page set up, but I'm guessing the request fails even before that somewhere on internal redirects. Any help would be much appreciated.

3
  • Is the client browser reporting a redirect loop, or is nginx? If it's the client, what location are the redirects to? Jul 7, 2012 at 5:54
  • both are reporting it. Client eventually ends up with the URL of /errors//errors//errors//errors//errors/...404.html Jul 7, 2012 at 13:44
  • What's the log entry from nginx look like? Jul 7, 2012 at 20:39

2 Answers 2

11

The culprit is: try_files $uri $uri/ ;

http://nginx.org/r/try_files (note that the last parameter is return code or URI to internal redirect)

If none of the files were found, an internal redirect to the uri specified by the last parameter is made.

1
  • Thanks. Just had one of these errors with a Roots Trellis LEMP setup. Happened after a WooCommerce data import. Never had it before. What would you recommend in these cases? Remove the parameter $uri/? See parameter here:github.com/roots/trellis/blob/…
    – rhand
    Dec 16, 2016 at 7:22
2

As others have stated, this is the culprit:

    try_files $uri $uri/ ;

It creates a redirection loop, since the last parameter of try_files should point to the location if the file isn't found. I solved it by adding a =404, like this:

    try_files $uri $uri/ =404 ;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .