使用清漆4.1和WordPress 4.4后端获取失败

时间:2016-01-14 22:02:37

标签: wordpress varnish varnish-4

varnish 4 WordPress 4.4中有多个主机,我遇到503错误。

虚拟服务器, server_1 server_2 。 (我只复制一个,因为两者的VS相同,x等于服务器编号)。

server {
  listen 81;
  server_name server_x.localhost;

  root   /var/www/server_x;
  index  index.html index.php;
  error_page 404 /404.html;

  # Use gzip compression
  # .....

  location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
    expires 365d;
  }

  location / {
    try_files $uri $uri/ /index.php?$args;
  }
  location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass unix:/var/run/php5-fpm-server_x.sock;
    fastcgi_index index.php;
    include fastcgi_params;
  }
  location ~* \.(eot|ttf|woff)$ {
    add_header Access-Control-Allow-Origin *;
  }
}

default.vcl

vcl 4.0;

backend server_1{
  .host = "127.0.0.1";
  .port = "81";
  .probe = {
    .url = "/";
    .interval = 5s;
    .timeout = 2s;
    .window = 5;
    .threshold = 3;
  }
}

backend server_2{
  .host = "127.0.0.1";
  .port = "81";
  .probe = {
  # the same of server_1
  }
}

acl purge {
  "localhost";
  "127.0.0.1";
}

sub vcl_recv {
  if (req.http.host ~ "server_1.localhost") {
    set req.backend_hint = server_1;
  }
  elseif (req.http.host ~ "server_2.localhost") {
    set req.backend_hint = server_2;
  }

  if (req.method == "PURGE") {
    if (!client.ip ~ purge) {
      return(synth(405,"Not allowed."));
    }
      return (purge);
  }

  if (req.url ~ "\.(gif|jpg|jpeg|swf|ttf|css|js|flv|mp3|mp4|pdf|ico|png)(\?.*|)$") {
    unset req.http.cookie;
    set req.url = regsub(req.url, "\?.*$", "");
  }

  if (req.url ~ "\?(utm_(campaign|medium|source|term)|adParams|client|cx|eid|fbid|feed|ref(id|src)?|v(er|iew))=") {
    set req.url = regsub(req.url, "\?.*$", "");
  }

  if (req.url ~ "wp-(login|admin)" || req.url ~ "preview=true" || req.url ~ "xmlrpc.php") {
    return (pass);
  }

  if (req.http.cookie) {
    if (req.http.cookie ~ "(wordpress_|wp-settings-)") {
      return(pass);
     } else {
     unset req.http.cookie;
     }
  }
return(hash);
}

sub vcl_backend_response {
  if ( (!(bereq.url ~ "(wp-(login|admin)|login)")) || (bereq.method == "GET") ) {
    unset beresp.http.set-cookie;
    set beresp.ttl = 1h;
  }
  if (bereq.url ~ "\.(gif|jpg|jpeg|swf|ttf|css|js|flv|mp3|mp4|pdf|ico|png)(\?.*|)$") {
    set beresp.ttl = 365d;
  }
} 

sub vcl_deliver {
   if (obj.hits > 0) {
     set resp.http.X-Cache = "HIT";
   } else {
     set resp.http.X-Cache = "MISS";
   }
   set resp.http.X-Hits = obj.hits;
}

当我尝试在任何服务器中输入时,我得到 503后端提取失败,但当我通过单独的服务器(http://server_1.localhosthttp://server_2.localhost进入)时返回200代码。

1 个答案:

答案 0 :(得分:0)

Varnish通过GET /测试后端健康。而后端的IP是127.0.0.1 尝试登录您的服务器并通过控制台Web浏览器“get /”,如elinks(apt-get install elinks或yum install elinks,如果您没有): elinks http://127.0.0.1:81 当我在我的服务器上完成该操作时,遇到同样的问题,我注意到127.0.0.1没有默认网页,而Varnish收到错误404并将后端标记为坏。 希望这会有所帮助