nginx泛解析_Nginx泛解析的匹配域名绑定到子目录配置的例子

更新时间:2020-03-05    来源:nginx    手机版     字体:

【www.bbyears.com--nginx】


网站的目录结构为:

# tree /home/wwwroot/111cn.net
/home/wwwroot/111cn.net
├── blog
│   └── index.html
└── file
    └── index.html
/home/wwwroot/111cn.net为nginx的安装目录下默认的存放源代码的路径。
blog为博客程序源代码路径
file为附件路径
把相应程序放入上面的路径通过
http://blog.111cn.net 访问博客
http://file.111cn.net 访问附件
其它二级域名类推。

方法一:

server {
listen 80;
server_name ~^(?.+).111cn.net$;
access_log /data/wwwlogs/111cn.net_nginx.log combined;
index index.html index.htm index.php;
root /home/wwwroot/111cn.net/$subdomain/;
...
}

方法二:

server {
listen 80;
server_name *.111cn.net;
access_log /home/wwwlogs/111cn.net.log combined;
index index.html index.htm index.php;

if ($host ~* ^([^\.]+)\.([^\.]+\.[^\.]+)$) {
    set $subdomain $1;
    set $domain $2;
}

location / {
    root /home/wwwroot/111cn.net/$subdomain/;
    index index.php index.html index.htm;
}

...
}
``

本文来源:http://www.bbyears.com/caozuoxitong/85790.html