Nginx + php-fpm without .php extension

This has happened several times on SO, but none of the solutions 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 enter the URL / foo.php and URL / foo through the final stanza. If I replace $uri.php

in the try_files

first stanza with @php

, then it should use the location @php

for the URL / foo.

For the location, @php

I 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 them worked.

When debugging, this line add_header

never produced anything for URL / foo. Content-Type is always set to application / octet-stream and oddly enough it always manages to send the content of the php file (it gets loaded).

I'm obviously doing something wrong, but I can't see what.

+3


source to share


1 answer


Sigh. I knew that if I asked about it, I would understand. 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 is the string fastcgi_param PATH_TRANSLATED "$document_root$uri.php";

. In the question, I described a scenario where all php requests went through the last stanza, which would eliminate the need for a middle stanza. It didn't work, location required @php

.

Also, please note that I switched to $document_root

. A number of questions that have discussed this have used the document root for the document root instead of a variable. This may not have been the case in older versions of nginx (I'm using 1.4.6), but this is the best way.

+3


source







All Articles