我读过这篇文章http://bneijt.nl/blog/post/name-based-virtual-hosting-with-nginx/
摘录如下:
server { server_name ~^((?<subdomain>.*)\.)?(?<domain>[^.]+)\.(?<tld>[^.]+)$; if ($subdomain = "") { set $subdomain "_"; } location / { index index.html; root /srv/http/vhost/${domain}.${tld}/${subdomain}; } }
我模仿它,写下我的configuration:
server { server_name ~^((?<subdomain>.*)\.)aa\.com$; if ($subdomain = "") { set $subdomain "www"; } location / { root /var/www/${subdomain}.aa.com/public; index index.html index.htm; } }
题:
如果inputwww.aa.com
,它可以工作,但inputaa.com
,它不能工作,域名parsing是好的,有什么问题?
你有2个选项:
1.使用redirect为example.net创build单独的虚拟主机
server { listen *:80; server_name example.net; return 301 http://www.example.net; } server { listen *:80; server_name ~^((?<subdomain>.*)\.)example\.net$; location / { root /vhosts/${subdomain}.example.net/public_html; index index.html index.htm; } }
2.修正正则expression式
server_name〜^((?<subdomain>。*)\。)?example \ .net $
我已经添加了?
在例子的开头签字