Search code examples
regexnginxnginx-location

nginx location block disables rendering of php files


I have this location block within my website.conf:

 location ~ ^/([^/?&:'"]+)$ {
        try_files $uri @root_path;
    }

This part of the file hinders the execution of php files, it always serves them as download.

  • How can I exclude *.php files from this regex expression?
  • What does that regex even mean?
  • Is it even good style to just exclude *.php files in this case?

If somebody could elaborate I would be very grateful.

Some more context: I'm trying out jitsi-meet and I would like to add some php-based functionality to the website.


Solution

  • The regular expression ^/([^/?&:'"]+)$ matches anything that begins with a / and contains any character except /, ?, &, :, ' and ".

    The first character is significant as it means it will only match URIs that contain a single path element (e.g. /foo and /index.php) but will not match URIs that contain another / (e.g. /foo/, /foo/bar and /foo/index.php).

    The other characters are probably a mistake as ? and & is unlikely to be encountered by a location statement as Nginx uses a normalised URI which has had the query string removed.


    You do not need to exclude .php from this regular expression.

    The URIs ending with .php are usually processed by another regular expression location block (e.g. location ~ \.php$).

    Regular expression location blocks are evaluated in order until a match is found, so you need to make sure that the location ~ \.php$ block is placed above the regular expression location block in your question.