Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Yet another redirection cycle, yes.

Have looked at other questions, but just can't seem to get it to work.

server {
    listen          80;
    server_name     localhost;

    charset         utf-8;

    access_log      /srv/http/localhost/log/access.log;
    error_log       /srv/http/localhost/log/error.log;

    location / {
        root        /srv/http/localhost/www;
        index       index.html index.php;

    # prevent access to hidden files
    location ~ /\. {
        access_log      off;
        log_not_found   off;
        deny            all;

    # do not log assets
    location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
        access_log      off;
        log_not_found   off;
        expires         360d;

    # pass the PHP scripts to PHP-FPM socket
    location ~* \.php$ {
        try_files       $uri /index.php;
        fastcgi_pass    unix:/var/run/php-fpm/php-fpm.sock;
        include         fastcgi_params;

This is my full server{} block for main host, that results in:

2012/07/29 00:34:52 [error] 14810#0: *10 rewrite or internal redirection cycle while internally redirecting to "/index.php", client: xx.xx.xx.xx, server: localhost, request: "GET /phpinfo.php HTTP/1.1", host: "xx.xx"

When accessing <anything>.php - index.php, phpinfo.php, i.php etc.

This results in 500 Internal Server Error, when I experimented with try_files, I ended up with 404 from time to time, but mainly the redirection cycle.

What is the problem here?

share|improve this question
up vote 3 down vote accepted

You've put try_files in the wrong location. It should not be in the location where you send requests upstream to php-fpm, but in your location /.

You also have root in the wrong place. It should be within the server block, not within the location / block.

Finally, you need a fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_filename; in the location ~* \.php$ block.

share|improve this answer
Goddamn, reading all those guides of nginx have screwed my mind. Thanks, this did the trick! – joltmode Jul 28 '12 at 22:06
Some of those guides are just plain wrong. So be careful :) – Michael Hampton Jul 28 '12 at 22:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.