Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

1 Answer 1

up vote 2 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
1  
Goddamn, reading all those guides of nginx have screwed my mind. Thanks, this did the trick! –  joltmode Jul 28 '12 at 22:06
1  
Some of those guides are just plain wrong. So be careful :) –  Michael Hampton Jul 28 '12 at 22:06
    
Remember to accept the answer that resolved your issue by clicking the outline of the check mark next to it. Welcome to Server Fault! –  Michael Hampton Jul 28 '12 at 22:14
    
Haha, thought I'd never get the "Rememeber to accept the answer". I'm used with SE, using SO and GameDev a lot. Accepted! –  joltmode Jul 28 '12 at 22:33

Your Answer

 
discard

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.