我正在从Apache过渡到Nginx,我试图找出如何让我的子域重写规则工作。
阿帕奇
Apacheconfiguration我已经基本上将所有stream量转移到HTTPS并将.net和.org域更改为主.com域。 我们的一些用户仍然认为每个URL都应该以“www”开头,所以如果还有另一个子域名的话,安装程序会将其删除。 没有任何子域名的url会被redirect到https://www.example.com ,这对于网站的工作并不一定是强制性的。
# HTTP <VirtualHost *:80> RewriteEngine on # Redirect http://example.tld -> https://www.example.com RewriteCond %{SERVER_PORT} !^443$ RewriteCond %{HTTP_HOST} ^example\.(com|net|org) RewriteRule ^(.*)$ https://www.example.com$1 # Redirect http://www.subdomain.example.tld -> https://subdomain.example.com RewriteCond %{SERVER_PORT} !^443$ RewriteCond %{HTTP_HOST} ^www.([^\.]+)\.example\.(com|net|org) RewriteRule ^(.*)$ https://%1.example.com$1 # Redirect http://subdomain.example.tld -> https://subdomain.example.com RewriteCond %{SERVER_PORT} !^443$ RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.(com|net|org) RewriteRule ^(.*)$ https://%1.example.com$1 [NC,R,L] # ... </VirtualHost> # HTTPS <VirtualHost *:443> RewriteEngine on # Redirect https://example.tld -> https://www.example.com RewriteCond %{HTTP_HOST} ^example\.(com|net|org) RewriteRule ^(.*)$ https://www.example.com$1 # Redirect https://www.subdomain.example.tld -> https://subdomain.example.com RewriteCond %{HTTP_HOST} ^www.([^\.]+)\.example\.(com|net|org) RewriteRule ^(.*)$ https://%1.example.com$1 # Redirect https://subdomain.example.(net|org) -> https://subdomain.example.com RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.(net|org) RewriteRule ^(.*)$ https://%1.example.com$1 [NC,R,L] # Otherwise serve https://subdomain.example.com # ... </VirtualHost>
Nginx的
到目前为止我所使用的Nginxconfiguration在大多数情况下都能正常工作,但是当URL格式为http://www.subdomain.example.tld时,我不知道如何去除“www”。 根据https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#server-name-if “如果是邪恶”,所以我试图让他们摆脱最常见的用例。
# Redirect http://subdomain.example.com -> https://subdomain.example.com # Redirect http://example.com -> https://example.com server { listen 80; server_name .example.com; return 301 https://$host$request_uri; } # Redirect https://subdomain.example.(net|org) -> https://subdomain.example.com server { listen 80; server_name .example.net .example.org; if ($host ~* "^([^.]+(\.[^.]+)*)\.example\.(net|org)$") { set $subdomain $1; rewrite ^(.*)$ https://$subdomain.example.com$1 permanent; break; } return 301 https://www.example.com; } # Otherwise serve https://subdomain.example.com server { listen 443 ssl; server_name .example.com; # ... }
所以我的问题是,如果有另一个子域已经提供了正确的Nginx方式,我如何删除“www”部分? 在最佳情况下,Nginxconfiguration应该像我的旧Apacheconfiguration一样工作。 有许多问题和答案涵盖了可以在configuration文件中手动列出子域的简单情况。 这不是我的select。
意识到server_name 也可以是一个正则expression式,这是我现在所拥有的。 另一个积极的惊喜是同一个server块可以处理HTTP和HTTPS情况。 我只是不得不把这个ssl on;注释掉ssl on; 从我的ssl.incconfiguration文件行。 据我可以告诉这个设置似乎像我的旧的Apacheconfiguration工作。
# Redirect http://example.tld -> https://www.example.com server { listen 80; server_name www.example.com www.example.net www.example.org; return 301 https://www.example.com$request_uri; } # Redirect http(s)://example.tld -> https://www.example.com server { listen 80; listen 443 ssl; server_name example.com example.net example.org; return 301 https://www.example.com$request_uri; include conf.d/ssl.inc; } # Serve https://subdomain.example.com server { listen 443 ssl; server_name ~^[^\.]+\.example\.com$; include conf.d/ssl.inc; include conf.d/common.inc; } # Redirect http(s)://www.whatever.subdomain.example.tld -> https://subdomain.example.com server { listen 80; listen 443 ssl; server_name ~\.(?<subdomain>[^\.]+)\.example\.(com|net|org)$ ~^(?<subdomain>[^\.]+)\.example\.(net|org)$; return 301 https://$subdomain.example.com$request_uri; include conf.d/ssl.inc; }