4

This has come up a few times on SO but none of the solutions have worked. This is what I currently have:

    location / {
            try_files $uri $uri/ $uri.php;
    }

    location @php {
            default_type application/x-httpd-php;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            fastcgi_param QUERY_STRING    $args;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }

    location ~ \.php$ {
            #fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            fastcgi_param QUERY_STRING    $args;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }

This example contains two options. First, the first stanza is written to push URL/foo.php and URL/foo through the final location stanza. If I replace the $uri.php in the try_files of the first stanza with @php, it should then use the @php location for URL/foo.

For the @php location I have tried to set SCRIPT_FILENAME to:

     fastcgi_param SCRIPT_FILENAME "/usr/share/nginx/html$uri.php";
     fastcgi_param SCRIPT_FILENAME "$request_filename.php";

None of these have worked.

In debugging this, no add_header line has ever emitted anything for URL/foo. The Content-Type is always set to application/octet-stream and, amusingly, it always manages to send back the content of the php file (it gets downloaded).

I'm obviously doing something wrong, but I'm not seeing what.

1 Answer 1

4

Sigh. I knew if I asked this, I'd figure it out. OK, here goes, this was the solution:

    location / {
            try_files $uri $uri/ @php;
    }

    location @php {
            fastcgi_param SCRIPT_FILENAME "$document_root$uri.php";
            fastcgi_param PATH_TRANSLATED "$document_root$uri.php";
            fastcgi_param QUERY_STRING    $args;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }

    location ~ \.php$ {
            #fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }

The key thing I was missing here was the fastcgi_param PATH_TRANSLATED "$document_root$uri.php"; line. In the question I described a scenario where all php requests went through the last stanza - which would obviate the need for the middle stanza. That didn't work, the @php location is needed.

In addition note that I switched to $document_root. A number of questions discussing this used the literal document root for the document root instead of the variable. Maybe it didn't exist in the older versions of nginx (I'm using 1.4.6), but this is a better way.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.