(Nginx的)正则expression式模式,以配合一切,但www

基本上,我试图用正则expression式来设置nginx

  • 匹配pyronexus.com&notoriouspyro.com的所有子域名,但不是www(www被redirect到pyronexus.com)。
  • 给我一个variables,我可以用它来确定input的子域和域是什么(例如,如果有人进入space.pyronexus.com,我想有两个variables$ subdomain和$域包含空间和pyronexus)。

到目前为止,我有这个: ~^(.*)\.(?:pyronexus|notoriouspyro)\.com$

但我似乎无法找出其他的东西! 任何帮助将不胜感激。

编辑:也许这将有助于显示我的nginxconfiguration文件:

 server { server_name pyronexus.com notoriouspyro.com; listen 127.0.0.1:80 default_server; root /home/nginx/pyronexus.com/public; index index.html index.php; access_log /home/nginx/pyronexus.com/logs/access.log; error_log /home/nginx/pyronexus.com/logs/error.log; include php.conf; } server { server_name ~^(www\.)?(.+)$; listen 127.0.0.1:80; return 301 $scheme://$2$request_uri; } 

第一部分是我需要正则expression式的服务器,第二部分是试图捕获所有域名登陆www并redirect他们没有www。

这很容易,就像@Melvyn说的,你已经在想这个了,你需要抓住所有的服务器来处理所有的域,然后创建一个特定的服务器来重定向www。

你想知道你正在访问的主机的最佳变量是$http_host

 server { listen 80 default_server; # here handle all subdomains, this will also match the non-www domains of # the both domains } server { listen 80; server_name www.pyronexus.com; return 301 http://pyronexus.com$request_uri; } server { listen 80; server_name www.notoriouspyro.com; return 301 http://notoriouspyro.com$request_uri; } 

这种模式似乎是这样做的:

 ^((?!www).+?)\.(?:pyronexus|notoriouspyro)\.com$ 

正则表达式匹配一个不包含单词的行吗?

测试在这里:

http://regex101.com/r/yK7oE2/1

如果你需要这个域名,只需要把这个?:

 ^((?!www).+?)\.(pyronexus|notoriouspyro)\.com$ 

退后一步。 任务是:

  • 在pyronexus.com和notoriouspyro.com上提供服务
  • 将子域名重定向到各自的域名
  • 将www子域名重定向到pyronexus.com

所以不要用太复杂的正则表达式来摆弄,而应该制作三个服务器模块。 在任务列表中的第二个是捕获所有。