Make files without an extension go through the PHP parser on nginx
Solution 1
Try this. Note my try_files
is a slight reshuffling of what you have in your example. Also I added root
which should match your web root path.
location / {
root /your/web/root/path
try_files $uri $uri/ @extensionless-php;
index index.php index.html;
}
location @extensionless-php {
rewrite ^(.*)$ $1.php last;
}
Solution 2
Finally figured it out:
location /info {
root /usr/share/nginx/www;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/www/info.php;
}
Actually makes sense now as I've bothered to read the documentation...
Related videos on Youtube
Garry Welding
Updated on September 18, 2022Comments
-
Garry Welding almost 2 years
Due to legacy reasons I need to get the following scenario working on nginx and I seem to be struggling. Say I have a file in the document root just called 'info' without any file extension. Now this is a PHP file, but it doesn't have an extension, and can't be renamed for reasons I wont explain here. How would I write an nginx config to make this file be parsed by PHP.
I have actually tried renaming this file to info.php and then adding the following to my nginx config, I then tried to hit
http://{siteurl}/info
expecting it to internally re-write toinfo.php
, but all it did was fall through toindex.html
.location / { try_files @extensionless-php $uri $uri/ /index.html; } location @extensionless-php { rewrite ^(.*)$ $1.php last; }
Now I'm very new to nginx and so I assume I'm just missing something very basic. Previously in apache the config was like this to get this scenario to work...
<LocationMatch "^/(info|...a few other files...)/?.*$"> ForceType application/x-httpd-php </LocationMatch>
Any help is much appreciated.
-
Garry Welding over 10 yearsSorry, I should have said, I did have it that way round initially, I just tried it the way round it currently is out of desperation. Although I have root set outside of the location block I can also try setting it within the location block to see if that works.