Using fastcgi_split_path_info for a userdir in nginx

9,981

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

Share:
9,981

Related videos on Youtube

nnyby
Author by

nnyby

Updated on September 18, 2022

Comments

  • nnyby
    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
    nnyby over 9 years
    Thanks! that worked. I also needed to change alias /home/$1/public_html; to alias /home/$1/public_html/;.
  • Gonzalo
    Gonzalo over 7 years
    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.