Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I dynamic number of Joomla installations in subfolders of the domain.

For example:


Currently I have the follwing config that works:

index index.php;

location / {
    index index.php index.html index.htm;

location /joomla_1/ {
    try_files $uri $uri/ /joomla_1/index.php?q=$uri&$args;

location /joomla_2/ {
    try_files $uri $uri/ /joomla_2/index.php?q=$uri&$args;

location ~ \.php$ {
    fastcgi_pass unix:/var/run/php5-fpm/joomla.sock;

I'm trying to combine joomla_N rules in one:

location ~ ^/(joomla_[^/]+)/ {
    try_files $uri $uri/ /$1/index.php?q=$uri&$args;

but server starts to return index.php as is (does not call the php-fpm).

It looks like the nginx stops the processing of the regex rules after the first match.

Is there any way to combine this rules with something like regex?

share|improve this question

Let's understand things:

To determine which location directive matches a particular query, the literal strings are checked first. Literal strings match the beginning portion of the query - the most specific match will be used. Afterwards, regular expressions are checked in the order defined in the configuration file. The first regular expression to match the query will stop the search. If no regular expression matches are found, the result from the literal string search is used.

So first regex stop searching!

Checks for the existence of files in order, and returns the first file that is found. A trailing slash indicates a directory - $uri /. In the event that no file is found, an internal redirect to the last parameter is invoked. The last parameter is the fallback URI and must exist, or else an internal error will be raised.

So the last parameter of the try_files is an internal url on which the chain is reinvoked if no static file is found.

So answer 1 works because the .php$ regexp is matched only when the internal redirect is invoked on the url, instead joomla_[^/] is matching always also on the internal php url.

To understand better also this works on a shell:

ln -s joomla_1 site_1
ln -s joomla_2 site_2 ...


location ~ ^/site_(\d+) {
    try_files $uri $uri/ /joomla_$1/index.php?q=$uri&$args;
location ~ \.php$ {
    fastcgi_pass unix:/var/run/php5-fpm/joomla.sock;

User urls:

share|improve this answer

Put the .php location first, and then put this after:

location ~ ^/joomla_(\d+) {
  try_files $uri $uri/ /joomla_$1/index.php?q=$uri&$args;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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