Server - How to set a directory as a second-level domain name in nginx?
巴扎黑
巴扎黑 2017-05-16 17:15:55
0
3
470

Assume the domain name is domain.com,

How to configure nginx.conf, so that when accessing abc.domain.com, it is actually the content of domain.com/abc
(browse The server's address still appears as abc.domain.com )

The configuration I searched online is useless:

location / { 
    set $domain default; 
    if ( $http_host ~* "^(.*)\.domain\.com$") { 
                  set $domain ; 
    } 
    rewrite ^/(.*)    /$domain/ last; 
}
巴扎黑
巴扎黑

reply all(3)
过去多啦不再A梦

Key part:

server
{
    listen   80;
    server_name   www.domain.com;
    root   /var/www/html;
    index  index.php index.html index.htm;

    location /abc
    {
        if ( $host !~* "abc.domain.com" )
        {
            return 404;  #防止有人访问www.domain.com/abc看到abc二级域名的页面,只允许访问abc.domain.com查看
        }
    }
}

server
{
    listen   80;
    server_name   abc.domain.com;
    root   /var/www/html/abc;
    index  index.php index.html index.htm;

    location /
    {
        #一些配置
    }
}
小葫芦
location / { 
    #申明domain变量 
    set $domain default;
    #正则提取出二级域名
    if ( $http_host ~* "^(.*)\.domain\.com$") { 
        #把二级域名赋值给domain变量
        set $domain ; 
        #设置rewrite规则,把/下的所有请求转发到http://domain.com/$domain/下。
        rewrite ^/(.*)$    http://domain.com/$domain/ last; 
    } 

}
phpcn_u1582

Just build another server! It's so complicated, both regular and rewritten.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!