Redirect 域到www.domain重定向

Redirect 域到www.domain重定向,redirect,nginx,varnish,Redirect,Nginx,Varnish,我在varnish后面有一个nginx服务器,我正在尝试进行域到www.domain的重定向 我尝试在nginx上使用这些规则 rewrite ^(.*) http://www.domain.com$1 permanent; return 301 ^ $scheme://www.domain.com$request_uri; return 301 http://www.domain.com$request_uri; 但我在chrome中遇到了一个错误,因为网站陷入了重定向循环

我在varnish后面有一个nginx服务器,我正在尝试进行域到www.domain的重定向

我尝试在nginx上使用这些规则

   rewrite ^(.*) http://www.domain.com$1 permanent;
   return 301 ^ $scheme://www.domain.com$request_uri;
   return 301 http://www.domain.com$request_uri;
但我在chrome中遇到了一个错误,因为网站陷入了重定向循环

由于about解决方案不起作用,我尝试了另一种清漆书写规则

sub vcl_recv {
  // ...
  if ( req.http.host == "domain.com" ) {
    error 750 "http://www." + req.http.host + req.url;
  }
  // ...
}

sub vcl_error {
  // ...
  if (obj.status == 750) {
    set obj.http.Location = obj.response;
    # Set HTTP 301 for permanent redirect
    set obj.status = 301;
    return(deliver);
  }
  // ...
}
我正在使用varnish 4,我得到一个错误,varnish无法编译代码

Message from VCC-compiler:
Expected an action, 'if', '{' or '}'
('input' Line 29 Pos 3)
  error 750 regsub(req.http.host, "^www\.(.*)", "http://\1"); 
--#####------------------------------------------------------------------------------------------------------------------------
谁能帮我修一下这个吗

我的服务器块如下所示:

server {
        listen  127.0.0.1:8080;
        root /home/webadmin/html/livesite;
        index index.php index.html index.htm;

        server_name www.domain.com;
#       rewrite ^(.*) http://www.domain.com$1 permanent;
#       return 301 ^ $scheme://www.domain.com$request_uri;
#       return 301 http://www.domain.com$request_uri;     
        location / {
                try_files $uri $uri/ /index.php?q=$uri&$args;
        }

        error_page 404 403 /error/eror.html;
        location = /error/error.html {
                root /home/webadmin/html/livesite;
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
             root /home/webadmin/html/livesite;
        }
        #pass the PHP scripts to FastCGI server listening on 127.0.0.1:9$
        location ~ \.php$ {
                #fastcgi_pass 127.0.0.1:9000;
                # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;

                 }
}

在保留uri和查询字符串的同时,这是一种正确的方法

return 301 $scheme://www.domain.com$request_uri$is_args$query_string;
问题不在这一部分,问题是你要重定向到哪里,可能重定向到另一个位置,也会进行另一个重定向,我猜你没有另一个服务器块来单独处理
www
服务器,所以你不断地重定向到同一个位置,在你发布配置的其余部分之前,我无法确定

编辑:

正如我所说的问题是,您正在将
www
重定向到
www
服务器,以避免您应该创建一个不使用
www
的新服务器来执行重定向

server { # the redirecting server
  listen 8080; # according to your config
  server_name domain.com; #without www
  return 301 $scheme://www.domain.com$request_uri$is_args$query_string;
}
server { # the actual serving server
  listen 8080;
  server_name www.domain.com;
  # the rest of your actual settings
}

使用varnish代替,并跳过apache重写。 本例总是重定向到https,但语义是相同的

在子vcl_recv中:

if ( req.http.host ~ "^(?i)domain.com" )
{
    set req.http.X-Redir-Url = "https://domain.com" + req.url;
    error 750 req.http.x-Redir-Url;
}
然后在子vcl_错误中:

if (obj.status == 750) {
    set obj.http.Location = obj.response;
    set obj.status = 302;
    return (deliver);
}

你可以发布你的nginx配置块这是我的服务器块这是
domain.com
这台服务器吗?是的,我刚刚删除了实际的域名,并将其与domain.com放在一起检查我编辑的答案