D
D
Dimon-zmey2016-06-30 11:13:21
CMS
Dimon-zmey, 2016-06-30 11:13:21

Apache + Nginx, how to search for a file in another directory?

I am writing a website (CMS) with subdomains, and therefore there are files that are in the root, and there are files for a specific domain. I want to make sure that if the file was not found, then search in the domain directory. For example, a file can be placed both in the root and along the path "/domains/cms.site.com/", thus, referring to cms.site.com/test.png , a search should be made in the root and if the file is not found there , then try to find it along the path "/domains/cms.site.com/test.png". I am using Apache 2.4 + Nginx 1.9. I'm not strong in setting up Nginx, but this is one of the customer's conditions. A bit of code:
In htaccess, this instruction looks like this (but it only works if nginx static processing is disabled, which I would not like to do):

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/domains/.*
RewriteRule ^(.*)$ /domains/%{HTTP_HOST}/$1 [L]

My attempts to set up Nginx (I read it on the Internet, it doesn't work, it gives 403 instead of a file):
location / {
    root "%hostdir%";
    try_files $uri $uri/ /domains/$http_host/$uri =403;
}

location /domains {
    root %hostdir%/domains/;
}

I would be very grateful for any hints!
Found a solution:
location ~* ^.+\.(?!php$|cgi$|html$|htm$|phtml$)\w*$ {
    root     "%hostdir%";
    try_files $uri /domains/$http_host/$uri =404;
    expires  7d;
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question