Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

For example:

    http://site/joomla_1/
    http://site/joomla_2/
    http://site/joomla_3/
    ...

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

2 Answers 2

Let's understand things:

http://wiki.nginx.org/HttpCoreModule#location

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!

http://wiki.nginx.org/HttpCoreModule#try_files

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_file is an internal url on wich 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 ...

nginx:

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:

http://yoursite.tld/site_1/....

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

 
discard

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.