1

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?

Alex Netkachov
  • 585
  • 1
  • 6
  • 9

2 Answers2

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_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 ...

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/....
Uyghur Lives Matter
  • 226
  • 1
  • 4
  • 15
rastrano
  • 61
  • 2
0

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

location ~ ^/joomla_(\d+) {
  try_files $uri $uri/ /joomla_$1/index.php?q=$uri&$args;
}
Kyle
  • 1,589
  • 9
  • 14