0

I have nginx rewrite rule - it redirects all subdomain requests from sub to folder:

server {
listen      77.77.77.77:80;
server_name domaincom *.domaincom;

root /home/admin/web/domaincom/public_html/subs/$subdomain;
set $subdomain "";
if ($host ~* ^([a-z0-9-\.]+)\.domaincom$) {
    set $subdomain $1;
    #rewrite $request_uri $1.$request_uri;
    #rewrite ^(.*txt)$ $subdomain.$request_uri;   just testing here
#rewrite ^(.*\.txt)$ $subdomain.$request_uri/$1;

}
if ($host ~* ^www.domaincom$) {
    set $subdomain "";
}

 # location $subdomain {
    # # # An example rewrite
    # rewrite ^/(.*txt)$ $request_uri;
# }

index       index.php;
access_log  off;
error_log /dev/null;


location / {

root        /home/admin/web/domaincom/public_html;
    location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js)$ {
        expires     max;
    }

    location ~ [^/]\.php(/|$) {
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        if (!-f $document_root$fastcgi_script_name) {
            return  404;
        }

        fastcgi_pass    127.0.0.1:9002;
        fastcgi_index   index.php;
        include         /etc/nginx/fastcgi_params;
    }
}

error_page  403 /error/404.html;
error_page 404 = @foobar;

location @foobar {
return 301 /;
}
error_page  500 502 503 504 /error/50x.html;

location /error/ {
    alias   /home/admin/web/domaincom/document_errors/;
}

                # Access deny for bots
# See bots list in the /etc/nginx/nginx.conf
if ($limit_bots = 1) {
          return 403;
    }


location ~* "/\.(htaccess|htpasswd)$" {
    deny    all;
    return  404;
}

location /vstats/ {
    alias   /home/admin/web/domaincom/stats/;
    include /home/admin/conf/web/domaincom.auth*;
}

include     /etc/nginx/conf.d/phpmyadmin.inc*;
include     /etc/nginx/conf.d/phppgadmin.inc*;
include     /etc/nginx/conf.d/webmail.inc*;

include     /home/admin/conf/web/nginx.domaincom.conf*;
}

It work well, but i have one problem.

And in domain and subdomains i have a same php-script that gets data from txt file, like this:

file(key.txt);

My php-fpm module, i think, doesn't know about nginx rules and gets data in SUB from ROOT domain - it's wrong. Please help me to add nginx exceptions or add rule to get txt's data in SUB from SUBs. Not_from_root_domain. Thanks.

  • When i request key.txt - it gets from root domain, not subdomain – Benjamin Coleman Dec 18 '18 at 18:10
  • Have you tried using __DIR__ constant, e.g. file( __DIR__ . 'key.txt' )? – Ivan Shatsky Dec 18 '18 at 19:32
  • Thanks. it's a good idea, but doesn't work, i've checked. When i just manually open in browser: http://subdomain.domaincom/key.txt - it opens http://domaincom/key.txt (there is subdomain.domaincom with key.txt from root domain in browser adress). It rewrites strongly. – Benjamin Coleman Dec 18 '18 at 21:12
  • Can you post your whole server block? – Ivan Shatsky Dec 18 '18 at 21:27
  • Sure. I can add it here in comments, so i've added in pastebin.com/1Qn3MSj6 – Benjamin Coleman Dec 18 '18 at 21:54
0

Some optimizations of your config:

1) Defining your subdomain root with the help of map directive:

map $http_host $root_suffix {
    ~^(?:www\.)?domain\.com$ "";
    ~^(.+)\.domain\.com$ /subs/$1;
}

2) Defining expire time based on request URI suffix:

map $uri $expires {
    default off;
    ~*.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js)$ max;
}

(This map directives must be defined outside the server { } block, at the http { } context level.)

Now your server { } block:

server {
    listen      77.77.77.77:80;
    server_name .domaincom;
    root        /home/admin/web/domaincom/public_html$root_suffix;
    expires     $expires;

    index       index.php;
    access_log  off;
    error_log   /dev/null;

    # your locations here
    location ~ [^/]\.php(/|$) {
        ...
    }
    ...
  • Everything works as expected? – Ivan Shatsky Dec 18 '18 at 23:20
  • There is a problem: i must define map directive in http section, but in your solution domain.com (one domain) defined. But in my configuration i have different configs for each domain and one main nginx.conf (with http). I have to use map, but i can to do it only in main config... Is there regex or something like that for each_domain in map and http? – Benjamin Coleman Dec 19 '18 at 11:19
  • I don't see any problems, put map directives into the same file where domaincom is defined, but before the server block. – Ivan Shatsky Dec 19 '18 at 11:33
  • Also, do you know, how to add ?mydomain\.com$ into Vesta control panel default config? There is template and domains looks like server_name %domain_idn% %alias_idn%; - but in my map section (according to your solution) i should to backslash the . dots. – Benjamin Coleman Dec 19 '18 at 12:08
  • I think this is too complex configuration for a template to be created for it. ^(?:www\.)?domain\.com$ is a regex matches only domain.com or www.domain.com, all other subdomains does not match this regex and falls to the second one. .domain.com is a special nginx server name form which matches domain.com and *.domain.com (note that server name *.domain.com matches www.domain.com and any other subdomain under domain.com, but not a domain.com itself). – Ivan Shatsky Dec 19 '18 at 13:12

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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