nginx.conf重定向一个完整的URL

时间:2016-12-27 14:23:25

标签: nginx url-rewriting nginx-location

我想使用nginx重定向完整的网址。 这不起作用:

server {
    listen       80;
    server_name  www.domain1.com www.domain2.com www.domain3.com ;

if ($http_host$request_uri ~ www.domain2.com/hello.html) {
    rewrite ^  google.com  permanent;
   }

}

这样做的正确方法是什么?
我怀疑变量$ http_host $ request_uri是否可以匹配该地址。

1 个答案:

答案 0 :(得分:0)

显然if无法很好地处理表达式。我会像这样重写你的配置:

server {
    listen       80;
    server_name  www.domain1.com www.domain2.com www.domain3.com;

    set $full_url $http_host$request_uri;
    if ($full_url ~ ^www\.domain2\.com/hello\.html) {
       return 301 https://google.com;
    }
}

对你来说可能为时已晚,但它解决了我的问题,我希望它有助于某人......

相关问题