Multiple sites under same domain with one app codebase

Hello nginx gurus,

First post but I have been reading this board for over a year!

My scenario:

I have many separate “sites” to run and they all use the same back end
application(opencart). They also will be under the same domain, in
folders.
I don’t want to duplicate the application code in each subdirectory.

I’ve got it working with a few sites and it works well. However, I will
have
~200 sites soon.

Example sites:

domain.com/site1/
domain.com/site2/
etc…

My main code base is located at the root of domain.com.

I am currently using location rewrite blocks for each site.

That will get ugly when I have 200 sites, and a pain when I want to add
new
sites.

Can anyone help me with a “dynamic” configuration so I don’t have to
edit
conf files each time?

===========================

server {
listen 80;
server_name domain.com;
root /etc/nginx/html/development;
error_log /var/log/nginx/dev.error.log;
index index.php;

rewrite ^([^.]*[^/])$ $1/ permanent; # trailing slash

HERE IS THE CODE I WANT TO FIX

location ^~ /site1/ {
rewrite ^/site1/(.) /$1;
}
location ^~ /site2/ {
rewrite ^/site2/(.
) /$1;
}
location ^~ /site3/ {
rewrite ^/site3/(.*) /$1;
}

}

=============================

Could I use map module, or possibly a single location regex?

Thanks!

Posted at Nginx Forum:

I have been experimenting with this:

            location ^~ /[a-zA-Z]+/ {
                    rewrite ^/(.*)/(.*) /$2;
            }

Of course I am getting 404 errors…

Posted at Nginx Forum:

On Fri, Jul 18, 2014 at 09:40:43AM -0400, martyparish wrote:

Hi there,

I have many separate “sites” to run and they all use the same back end
application(opencart). They also will be under the same domain, in folders.
I don’t want to duplicate the application code in each subdirectory.

It is not clear to me exactly what you have, and what you have working,
and what you have not working.

Could you give some specific examples?

So:

If I request /site1/img/file.png, what file on the filesystem should
nginx send me; or what file on the filesystem should nginx invite a
fastcgi server to process?

/usr/local/nginx/html/site1/img/file.png; or
/usr/local/nginx/html/img/file.png, or something else?

If I request /site2/dir/file.php – same question?

And in each case: if that file does not exist, what should happen? 404
or other failure, or fall back to a specific other file?

f

Francis D. [email protected]

Thanks Francis!

Sorry for not being clear enough.

My code base is at:

/etc/nginx/html/production/

The “site1, site2, etc…” folders do not exists at all. They are only
seen
and used in the public URLs.

The original config I posted in the first post works, I just don’t like
it
because there will be hundreds of “sitesX”. It is using a location block
with rewrite for each site.

so, if I request

domain.com/site1/index.php

The file that is served is

/etc/nginx/html/production/index.php (not
/etc/nginx/html/production/site1/index.php)

Same for ALL sites. PHP processed the /site1/ path and loads the
configuration for that particular site.

So, really nothing is “broken”, it is just going to be extremely ugly
and
possibly slow to process 200+ location blocks.

I’d like to use a location regex that could handle ALL sites in one
location
block if possible.

Or, I was thinking “map” may work but not sure how I could configure it.

Thanks again for the help and let me know if this makes it more clear.

Marty

Posted at Nginx Forum:

Thanks itpp2012

I was actually just experimenting with that approach! I was beginning to
have a bit of success too! Not quite there yet though.

map $uri $site_folder {
#~^/(?P[a-zA-Z]+)/.* $folder;
~^/(?P[a-z]+)/.* $folder;
}

I saw an example very similar(or same) as what you gave me on a
wordpress
multi site page.

I think my regex is a bit messed up but I will keep on going down this
route. Looks very promising!!!

** I think my pcre is old, as I have to use the ?P syntax.

At one point, I was able to capture the folder name into the variable
but
ONLY if there was no characters after the slash (domain.com/site1/)

Thanks again! (to everyone)

Posted at Nginx Forum:

Okay, I finally got the mapped variable figured out!

map $uri $site_folder {
~^/(?P[a-zA-Z]+)/.* $folder ;
}

Now, I just need to figure out how to use it in the location block???

This is what I am trying:

location ^~ /$site_folder/ {
  #rewrite ^/$site_folder/(.*) /$1;
}

Is it okay to use the named variable in the location like that? It’s not
picking it up…

If I use this:

location ^~ / {
  return 301 $scheme://www.google.com?q=$site_folder;
}

It DOES redirect to google with the proper variable in there.

Posted at Nginx Forum:

Maybe not exactly what your looking for but should give enough to
rewrite
this for what you want with map.

Posted at Nginx Forum:

Enable debugging and check the logs, or add Lua and dump variables to
see
what value is doing what (this is how I debug a flow).

Posted at Nginx Forum:

I will do that.

I guess my main question is:

can I use a variable in the location URI?

location ^~ /$site_folder/ {

It appears that the answer is no. I have confirmed the variable is set
from
the map block. However, when I use it in location like above, it gets
skipped over.

I appreciate the time you have spent with me!

Posted at Nginx Forum:

For anyone who comes across this scenario, here is how I got it working:

get the first folder name into a variable ($site_folder)

map $uri $site_folder {
~^/(?P[a-zA-Z]+)/.* $folder ;
}

server {

if (!-f $site_folder) {
  rewrite ^/[^/]+/(.*) /$1;
}

}

I am aware of the “if is evil” thing but it states that it is safe
outside
of “location” block. Supposedly safe in “server” context.

Performance seems good so far.

Posted at Nginx Forum:

actually this part was wrong;
if (!-f $site_folder) {
rewrite ^/[^/]+/(.*) /$1;
}

needs to be:

if (!-d /etc/nginx/html/production/$site_folder) {
rewrite ^/[^/]+/(.*) /$1;
}

  • changed -f to -d
    ** had to add root path before $site_folder

Posted at Nginx Forum:

Maybe you should set location as fixed generic and then use root to
change
where ever it needs to, don’t forget to tell php where stuff is if you
change root.

Posted at Nginx Forum:

Unfortunately it did not. I was really hoping to do this with try_files
instead of “if” and rewrite!

Posted at Nginx Forum:

On Sat, Jul 19, 2014 at 11:34 PM, martyparish [email protected]
wrote:

}

  • changed -f to -d
    ** had to add root path before $site_folder

I wonder if this works:

server {

location ~ ^/.+(/.+) {
try_files $uri $1;
}
}

On Sat, Jul 19, 2014 at 11:51 PM, martyparish [email protected]
wrote:

Unfortunately it did not. I was really hoping to do this with try_files
instead of “if” and rewrite!

what error did you get

Sorry for the delay. It wasn’t an error but the web pages were out of
whack.
Some of the files were now found.

Posted at Nginx Forum: