I'm trying to configure my nginx server such that any php file will be accessible without giving it's extension.
I'm already aware of many similar questions/answers like https://stackoverflow.com/questions/7760883/nginx-setting-a-default-file-extension/7761779#7761779 https://stackoverflow.com/questions/7760883/nginx-setting-a-default-file-extension and https://stackoverflow.com/questions/19975218/remove-php-from-url-with-rewrite-rule but I want to go a bit further :
If the request contains the .php file, we will return a 404.
So for example say I have in my folder :
- index.php
- search.php
- history.php
I need to acces them using
- index.php => / OR /index
- search.php => /search
- history.php => /history
and if the user tries /search.php, he will get a 404 in returns.
The last part is the tricky one for me. Moreover, when I access files without the PHP extension, NGinx downloads the file for me, without compiling it (so I have access to the code source, containing database access :/)
How can I do ?
Here's my actual configuration :
server {
listen 80 default_server;
root /srv/www/status/;
index index.php;
access_log /var/log/nginx/default-access_log;
error_log /var/log/nginx/default-error_log;
location / {
try_files $uri $uri.php =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_index index.php;
fastcgi_pass php5-fpm-sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
}
Thanks for the help! :)