首页 > 技术文章 > Nginx泛解析的匹配域名绑定到子目录配置

doseoer 2016-07-12 13:20 原文

网站的目录结构为:

# tree /home/wwwroot/lvtao.net
/home/wwwroot/lvtao.net
├── blog
│   └── index.html
└── file
    └── index.html

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

方法一:

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

方法二:

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

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

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

...
}
``

推荐阅读