I've posted an excerpt from my Nginx configuration with the hope that someone can give me advice on anything I've done wrong or could do better. Please note that the below config works, but has been edited for brevity.
The area I'd like you to focus on is TEST1 and TEST2.
TEST1 is redirecting any requests to /folder/downloads/
to /usr/share/nginx/downloads/redirected.php
Example:
When I visit http://192.168.1.100/folder/downloads/imaginary-file.pdf
I am being redirected to /usr/share/nginx/downloads/redirected.php
TEST2 is allowing PHP to be executed in /usr/share/nginx/downloads/
Previously it was being redirected to /usr/share/nginx/html/
because of the root directive in the server block.
server {
root /usr/share/nginx/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$args;
}
#TEST1
location /folder/downloads/ {
root /usr/share/nginx/downloads;
rewrite ^/(.*)$ /redirected.php last;
}
#TEST2 ALLOW PHP TO EXECUTE FOR #TEST1
location ~* ^/redirected(.*\.php)$ {
root /usr/share/nginx/downloads;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
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;
}
}
Miscellaneous information: In the future, I may want PHP files other than redirected.php to execute.