![正则表达式匹配非www和非指定主机(nginx)](https://linux22.com/image/802246/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%8C%B9%E9%85%8D%E9%9D%9Ewww%E5%92%8C%E9%9D%9E%E6%8C%87%E5%AE%9A%E4%B8%BB%E6%9C%BA%EF%BC%88nginx%EF%BC%89.png)
server {
listen 80;
server_name ~(?!^www\.)(?!thehost.org);
return 301 $scheme://www.$host$request_uri;
}
我希望 server_name 与 otherhost.org 和 differenthost.org 匹配,然后分别向 www.otherhost.org 和 www.differenthost.org 返回 301。
答案1
server {
listen 80;
servername ~^(?<domain>otherhost|differenthost)\.org$;
return 301 $scheme://www.$domain.org$request_uri;
}