nginx rewrite or internal redirection cycle

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.


Solution 1:

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.

Solution 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 ;