Lỗi cài đặt varnish multisite


(phong) #1

E cài đặt varnish cho 2 site là https://hostingvippro.xyzhttps://api.hostingvippro.xyz. Khi truy cập vào trang api.hostingvippro.xyz thì bị trả về nội dung của trang hostingvippro.xyz mặc dù ko bị redirect gì cả. Check status varnish thì ko báo lỗi gì. Đây là cấu hình fle vcl của e:

vcl 4.0;
backend pop{
    .host = "127.0.0.1";
    .port = "8080";
    .first_byte_timeout = 300s;
}
backend api{
    .host = "127.0.0.1";
    .port = "8081";
    .first_byte_timeout = 300s;
}

acl purge {
    "127.0.0.1";
}

sub vcl_recv {
        # Ask Varnish to fire 750 status for HTTP requests from external IPs and port 80,
        # and not from SSL Termination Proxy (Nginx).
        if (req.http.host ==  "hostingvippro.xyz" && req.http.X-Forwarded-Proto !~ "(?i)https") {
                set req.backend_hint = pop;
				set req.http.location = "https://hostingvippro.xyz" + req.url;
				return (synth(750, ""));
        }
		if (req.http.host == "api.hostingvippro.xyz" && req.http.X-Forwarded-Proto !~ "(?i)https") {
                set req.backend_hint = api;
				set req.http.location = "https://api.hostingvippro.xyz" + req.url;
				return (synth(750, ""));
        }
}

sub vcl_synth {
        # Listen to 750 status from vcl_recv.
        if (resp.status == 750) {
                set resp.status = 301;
                set resp.http.Location = req.http.location;
                return(deliver);
        }
}

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

                if (req.http.X-Purge-Method == "regex") {
                        ban("req.url ~ " + req.url + " && req.http.host ~ " + req.http.host);
                        return (synth(200, "Banned."));
                } else {
                        return (purge);
                }
        }

        ### Do not Authorized requests.
        if (req.http.Authorization) {
                return(pass); // DO NOT CACHE
        }

        ### Pass any requests with the "If-None-Match" header directly.
        if (req.http.If-None-Match) {
                return(pass); // DO NOT CACHE
        }

        ### Do not cache AJAX requests.
        if (req.http.X-Requested-With == "XMLHttpRequest") {
                return(pass); // DO NOT CACHE
        }

        ### Only cache GET or HEAD requests. This makes sure the POST (and OPTIONS) requests are always passed.
        if (req.method != "GET" && req.method != "HEAD") {
                return (pass); // DO NOT CACHE
        }

        ### Static files: Do not cache PDF, XML, ... files (=static & huge and no use caching them - in all Vary: variations!)
        if (req.url ~ "\.(doc|mp3|pdf|tif|tiff|xml)(\?.*|)$") {
                return(pass); // DO NOT CACHE
        }

        # Unset the header for static files
        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) {
                # Google Analytics
                set req.http.Cookie = regsuball( req.http.Cookie, "(^|;\s*)(__utm[a-z]+)=([^;]*)", "");
                set req.http.Cookie = regsuball( req.http.Cookie, "(^|;\s*)(_ga)=([^;]*)", "");

                # Quant Capital
                set req.http.Cookie = regsuball( req.http.Cookie, "(^|;\s*)(__qc[a-z]+)=([^;]*)", "");

                # __gad __gads
                set req.http.Cookie = regsuball( req.http.Cookie, "(^|;\s*)(__gad[a-z]+)=([^;]*)", "");

                # Google Cookie consent (client javascript cookie)
                set req.http.Cookie = regsuball( req.http.Cookie, "(^|;\s*)(displayCookieConsent)=([^;]*)", "");

                # Other known Cookies: remove them (if found).
                set req.http.Cookie = regsuball( req.http.Cookie, "(^|;\s*)(__CT_Data)=([^;]*)", "");
                set req.http.Cookie = regsuball( req.http.Cookie, "(^|;\s*)(WRIgnore|WRUID)=([^;]*)", "");


                # PostAction: Remove (once and if found) a ";" prefix followed by 0..n whitespaces.
                # INFO \s* = 0..n whitespace characters
                set req.http.Cookie = regsub( req.http.Cookie, "^;\s*", "" );

                # PostAction: Unset the header if it is empty or 0..n whitespaces.
                if ( req.http.cookie ~ "^\s*$" ) {
                        unset req.http.Cookie;
                }
        }
}

sub vcl_backend_response {
        if ( (!(bereq.url ~ "(wp-(login|admin)|login)")) || (bereq.method == "GET") ) {
                //unset beresp.http.set-cookie;
                set beresp.ttl = 1h;
        }
        # Remove some headers we never want to see
        unset beresp.http.Server;
        unset beresp.http.X-Powered-By;

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

        set beresp.ttl = 10s;
        set beresp.grace = 1h;
}

sub vcl_deliver {
    if (obj.hits > 0) {
      set resp.http.X-Cache = "HIT";
    } else {
      set resp.http.X-Cache = "MISS";
    }
    set resp.http.Access-Control-Allow-Origin = "*";
    set resp.http.Server = "HocVPS Script";
}

(phong) #2

E đã test rất nhiều trường hợp vầ chỉnh lại varnish vcl và file cấu hình nginx của api.hostingvippro.xyz thì chạy đc:
Cụ thể như sau:

2 site api.hostingvippro.xyz và hostingvippro.xyz đều listen 127.0.0.1:8080 chứ ko phải mỗi site 1 port như trước. Trước em để api.hostingvippro.xyz listen 127.0.0.1:8081.

Cấu hình lại default.vcl --> cả 2 site trên đều để chung 1 backend vì em đã config lại 2 site cùng listen 127.0.0.1:8080 như trên. File default.vcl chỉnh lại như sau:

vcl 4.0;
import std;

backend pop {
    .host = "127.0.0.1";
    .port = "8080";
    .first_byte_timeout = 300s;
}
acl purge {
    "127.0.0.1";
}
sub vcl_recv {
#log varnish
std.syslog(180, "RECV: " + req.http.host + req.url);
std.log("varnish log info:" +  req.http.host + req.url);
#============================

set req.backend_hint = pop;
        if ((req.http.host ~ "^(?i)www.api.hostingvippro.xyz" ||req.http.host ~ "^(?i)api.hostingvippro.xyz") && req.http.X-Forwarded-Proto !~ "(?i)https") {
                 set req.http.location = "https://api.hostingvippro.xyz" + req.url;
                 return (synth(750, ""));
        }
        if ( (req.http.host ~ "^(?i)www.hostingvippro.xyz" || req.http.host ~ "^(?i)hostingvippro.xyz") && req.http.X-Forwarded-Proto !~ "(?i)https") {
                 set req.http.location = "https://hostingvippro.xyz" + req.url;
                 return (synth(750, ""));
        }

}

sub vcl_synth {
        # Listen to 750 status from vcl_recv.
        if (resp.status == 750) {
                set resp.status = 301;
                set resp.http.Location = req.http.location;
                return(deliver);
        }
}

Như trên thì e đã config đc varnish cho cả 2 site. Nhưng do làm mò thôi chứ ko hiểu tại sao là thành công. Mấy nữa lại lăn ra lỗi thì xui. Ai biết cái này giải thích giúp em nhé