问题描述
在配置nginx的时候,server_name需要配置多个二级域名,参照的这篇文章,原文http://bneijt.nl/blog/post/name-based-vi...
摘录如下:
server { server_name ~^((?<subdomain>.*).)?(?<domain>[^.]+).(?<tld>[^.]+)$; if ($subdomain = '') {set $subdomain '_'; } location / {index index.html;root /srv/http/vhost/${domain}.${tld}/${subdomain}; }}
我的配置参照这个稍作修改,内容如下:
server { server_name ~^((?<subdomain>.*).)aa.com$; if ($subdomain = '') {set $subdomain 'www'; } location / {root /var/www/${subdomain}.aa.com/public;index index.html index.htm; }}
问题: 在subdomain为空的时候,不能转换为www,也就是,在浏览器中输入www.aa.com,可以访问,但是直接输入aa.com,就不能访问,是正则表达式~^((?<subdomain>.*).)aa.com$的问题还是其他问题?
问题解答
回答1:正则改成:~^((?<subdomain>.*).)?aa.com$试试
注意,没有在环境中验证哈。
还有就是域名访问,需要域名解析配置A记录的。www.aa.com和aa.com需要两条A记录。
最好,两个地方都检查一下。