Using fastcgi_split_path_info for a userdir in nginx
In fact that's a very basic regex mistake you did there.
As your second capture group in the fastcgi_split_path_info
directive is (/.*)
then the whole regular expression ^/~nik/(.+?\.php)(/.*)$
won't match /~nik/t.php
as it's not followed by a slash. The question mark is also useless here while that's not the cause of the behaviour you see.
The correct regex should be ^/~nik/(.+\.php)(.*)$
Related videos on Youtube
![nnyby](https://i.stack.imgur.com/N1fXR.jpg?s=256&g=1)
nnyby
Updated on September 18, 2022Comments
-
nnyby almost 2 years
I'm trying to execute a PHP file on my server at the url
/~nik/t.php
(actual file is at/home/nik/public_html/t.php
). Here's what my PHP settings look like:location ~ ^/~(.+?)(/.*)\.php$ { alias /home/$1/public_html; # What should this regex be? fastcgi_split_path_info ^/~nik/(.+?\.php)(/.*)$; if (!-f $document_root$fastcgi_script_name) { return 404; } include fastcgi.conf; fastcgi_pass unix:/var/run/php5-fpm.sock; }
I keep getting a 404, and when I comment out the clause around
return 404;
, I'm able to see the error:Unable to open primary script: /home/nik/public_html/~nik/t.php (No such file or directory)
Can you see what's wrong with my configuration, or point me to some docs on how to do this? It's unfortunate that an advanced knowledge of regexes is a requirement to configure nginx.
-
nnyby over 9 yearsThanks! that worked. I also needed to change
alias /home/$1/public_html;
toalias /home/$1/public_html/;
. -
Gonzalo over 7 yearsShouldn't the question mark be needed in case the url contains a second
.php
substring? Something likeexample.com/index.php?search=test.php
orexample.com/index.php/my_hosted_files/test.php
would cause issues.