0

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.

2

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)(.*)$

  • Ah thanks, I'll try this out. Maybe it's a basic mistake, but to me regexes are like a foreign language that I'm not really fluent in. – nnyby Feb 18 '15 at 19:12
  • Thanks! that worked. I also needed to change alias /home/$1/public_html; to alias /home/$1/public_html/;. – nnyby Feb 18 '15 at 19:51
  • Shouldn't the question mark be needed in case the url contains a second .php substring? Something like example.com/index.php?search=test.php or example.com/index.php/my_hosted_files/test.php would cause issues. – Gonzalo Feb 6 '17 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.