ivo Welch
ivo Welch

Reputation: 2866

nginx: all named subdomains to same-name subdirectories, no info back to browser

I got tantalizingly close to finding the answer in many preceding posts, but nothing was perfect. so please forgive the imposition of asking it again. I think this could be a reasonably common question, so it's worth getting a concise answer.

(1) I (also) want to use subdirectories on my webserver, even if the browser continuously thinks it is dealing with subdomains. that is, I don't want the browser to receive a 301 from the server [so no return directive]. (2) I don't want to specify a list of domains. I want it to work for all.

specifically, I want nginx to interpret all $1.example.com as example.com/$1/ without specifying the list of what $1 could be. thus, http://funny.example.com/ in a browser should always retrieve the same exact page and http code as http://example.com/funny/ . this should also apply to all subfiles. http://funny.example.com/d1/p1.html should yield the same results as http://example/funny/d1/p1.html . this is not location remapping, but url remapping. but, if /var/www/example.com/ is my top directory, presumably this would then serve up /var/www/example.com/funny/d1/p1.html.

from the perspective of the browser, they just happen to see exactly the same page for http://funny.example.com/ and http://example.com/funny/ .

solution appreciated.

Upvotes: 2

Views: 43

Answers (1)

mergenchik
mergenchik

Reputation: 1139

Try below according to ngx_http_core module docs and Server names docs

server {
    # sub domains config, match with regular expression *.example.com
    server_name ~^(?<sub_domain>.+)\.example\.com$;
    location / {
        root /var/www/examples.com/funny/$sub_domain;
    }
}

server {
    # catch all config
    server_name _;

    location / {
        root /var/www/examples.com;
    }
}

Upvotes: 2

Related Questions