我configuration了我的第一个nginx服务器,我必须redirectsubdomain1.domain.com,subdomain2.domain.com和一些更多的domain.com
我在nginx中search了很多关于重写的内容,但找不到任何有用的东西。 谢谢!
Nginx只知道与服务器块中的server_name匹配的(子)域。 如果您有一个设置,则任何其他(子)域的请求都将在“默认”服务器块中结束。
因此,您需要创build一个与您感兴趣的子域相匹配的新服务器块。
server{ server_name subdomain1.domain.com subdomain2.domain.com; }
如果您有大量的子域,则还可以在server_name使用正则expression式匹配或通配符(例如*.domain.com )。
在该服务器块中,添加您的重写(实质上等于,任何打到这些子域名应重写到我的主域)。
rewrite ^ http://domain.com permanent;
把它放在一起,你会得到:
server{ server_name subdomain1.domain.com subdomain2.domain.com; rewrite ^ http://domain.com permanent; }
您可以将http://domain.com更改为http://domain.com$request_uri以保留path。