Хобрук: Ваш путь к мастерству в программировании

Очистить кеш лака с несколькими бэкендами

У меня есть лаковый кеш за узлом завершения nginx ssl, который маршрутизируется к двум разным бэкендам (оба работают на WordPress) на основе URL-адреса. «/» направляется на сервер A, а «/about/» — на сервер B.

Если я сначала завожу локально curl http://127.0.0.1/, для установки кеша, а потом curl -X PURGE http://127.0.0.1/, он очищает кеш.

Если я нажму на него с нашего сервера терминации nginx ssl, а затем попытаюсь очистить кеш с помощью той же команды curl, он не очистит кешированный элемент, созданный при прохождении через сервер nginx.

vcl-файл:

# Based off of https://gist.github.com/matthewjackowski/062be03b41a68edbadfc

# Marker to tell the VCL compiler that this VCL has been adapted to the
# new 4.0 format.
vcl 4.0;
import directors;

# Default backend definition. Set this to point to your content server.
backend default {
    .host = "serverA";
    .port = "80";
    .connect_timeout = 600s;
    .first_byte_timeout = 600s;
    .between_bytes_timeout = 600s;
    .max_connections = 800;
}

backend bedrock {
    .host = "serverB";
    .port = "80";
    .connect_timeout = 600s;
    .first_byte_timeout = 600s;
    .between_bytes_timeout = 600s;
    .max_connections = 800;
}

# Only allow purging from specific IPs
acl purge {
    "localhost";
    "127.0.0.1";
}

# This function is used when a request is send by a HTTP client (Browser) 
sub vcl_recv {
    # Normalize the header, remove the port (in case you're testing this on various TCP ports)
    set req.http.X-Forwarded-For = client.ip;
    set req.backend_hint = default;

    # Unset headers that might cause us to cache duplicate infos
    unset req.http.Accept-Language;
    unset req.http.User-Agent;
    set req.http.X-Forwarded-Proto = "https";

    if (req.url == "/") {
        set req.backend_hint = bedrock;
    }

    # Allow purging from ACL
    if (req.method == "PURGE") {
        # If not allowed then a error 405 is returned
        if (!client.ip ~ purge) {
            return(synth(405, "This IP is not allowed to send PURGE requests."));
        }    
        # If allowed, do a cache_lookup -> vlc_hit() or vlc_miss()
        return (purge);
    }

    # drop cookies and params from static assets
    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, "\?.*$", "");
    }

    # drop tracking params, only needed on the frontend.
    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, "\?.*$", "");
    }

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

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

# Drop any cookies Wordpress tries to send back to the client.
sub vcl_backend_response {
    # Remove some headers we never want to see
    unset beresp.http.Server;
    unset beresp.http.X-Powered-By;
    # This function is used when a request is sent by our backend (Nginx server)
    if (bereq.http.Cookie ~ "(UserID|_session)") {
        set beresp.http.X-Cacheable = "NO:Got Session";
        set beresp.uncacheable = true;
        return (deliver);

    } elsif (beresp.ttl <= 0s) {
        # Varnish determined the object was not cacheable
        set beresp.http.X-Cacheable = "NO:Not Cacheable";

    } elsif (beresp.http.set-cookie) {
        # You don't wish to cache content for logged in users
        set beresp.http.X-Cacheable = "NO:Set-Cookie";
        set beresp.uncacheable = true;
        return (deliver);

    } elsif (beresp.http.Cache-Control ~ "private") {
        # You are respecting the Cache-Control=private header from the backend
        set beresp.http.X-Cacheable = "NO:Cache-Control=private";
        set beresp.uncacheable = true;
        return (deliver);

    } else {
        # Varnish determined the object was cacheable
        set beresp.http.X-Cacheable = "YES";
        # Remove Expires from backend, it's not long enough
        unset beresp.http.expires;
        # Set the clients TTL on this object
        set beresp.http.cache-control = "max-age=900";
        # Set how long Varnish will keep it
        set beresp.ttl = 1d;
    }

    if ( (!(bereq.url ~ "((wp/)?wp-(login|admin)|login)")) || (bereq.method == "GET") ) {
        set beresp.http.X-UnsetCookies = "TRUE";
        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 = 1d;
    }

}

# The routine when we deliver the HTTP request to the user
# Last chance to modify headers that are sent to the client
sub vcl_deliver {
    if (obj.hits > 0) { 
        set resp.http.X-Cache = "cached";
    } else {
        set resp.http.x-Cache = "uncached";
    }

    # Remove some headers: PHP version
    unset resp.http.X-Powered-By;
    # Remove some headers: Apache version & OS
    unset resp.http.Server;
    # Remove some headers: Varnish
    unset resp.http.X-Varnish;
}

вывод varnishlog при кэшировании страницы

*   << BeReq    >> 3         
-   Begin          bereq 2 fetch
-   Timestamp      Start: 1488390891.742004 0.000000 0.000000
-   BereqMethod    GET
-   BereqURL       /
-   BereqProtocol  HTTP/1.0
-   BereqHeader    X-Prerender-Token: qUcOM8XD5dRKUvlnCaMx
-   BereqHeader    X-Real-IP: 10.224.20.1
-   BereqHeader    Host: ssl-sermination-domain.com
-   BereqHeader    Upgrade-Insecure-Requests: 1
-   BereqHeader    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
-   BereqHeader    DNT: 1
-   BereqHeader    X-Forwarded-For: 10.224.20.146
-   BereqHeader    X-Forwarded-Proto: https
-   BereqHeader    Accept-Encoding: gzip
-   BereqProtocol  HTTP/1.1
-   BereqHeader    X-Varnish: 3
-   VCL_call       BACKEND_FETCH
-   VCL_return     fetch
-   BackendOpen    20 boot.bedrock 10.240.0.4 80 10.224.20.148 39350
-   BackendStart   10.240.0.4 80
-   Timestamp      Bereq: 1488390891.743281 0.001276 0.001276
-   Timestamp      Beresp: 1488390891.863164 0.121160 0.119883
-   BerespProtocol HTTP/1.1
-   BerespStatus   200
-   BerespReason   OK
-   BerespHeader   Date: Wed, 01 Mar 2017 17:54:51 GMT
-   BerespHeader   Server: Apache/2.4.7 (Ubuntu)
-   BerespHeader   X-Powered-By: PHP/5.5.9-1ubuntu4.21
-   BerespHeader   Link: <https://ssl-sermination-domain.com/wp-json/>; rel="https://api.w.org/"
-   BerespHeader   Link: <https://ssl-sermination-domain.com/>; rel=shortlink
-   BerespHeader   Vary: Accept-Encoding
-   BerespHeader   Content-Encoding: gzip
-   BerespHeader   Access-Control-Allow-Origin: *
-   BerespHeader   Cache-Control: Public
-   BerespHeader   Max-Age: 600
-   BerespHeader   Content-Length: 6396
-   BerespHeader   Content-Type: text/html; charset=UTF-8
-   TTL            RFC 120 10 -1 1488390892 1488390892 1488390891 0 0
-   VCL_call       BACKEND_RESPONSE
-   BerespUnset    Server: Apache/2.4.7 (Ubuntu)
-   BerespUnset    X-Powered-By: PHP/5.5.9-1ubuntu4.21
-   BerespHeader   X-Cacheable: YES
-   BerespUnset    Cache-Control: Public
-   BerespHeader   cache-control: max-age=900
-   TTL            VCL 86400 10 0 1488390892
-   BerespHeader   X-UnsetCookies: TRUE
-   TTL            VCL 3600 10 0 1488390892
-   VCL_return     deliver
-   Storage        malloc s0
-   ObjProtocol    HTTP/1.1
-   ObjStatus      200
-   ObjReason      OK
-   ObjHeader      Date: Wed, 01 Mar 2017 17:54:51 GMT
-   ObjHeader      Link: <https://ssl-sermination-domain.com/wp-json/>; rel="https://api.w.org/"
-   ObjHeader      Link: <https://ssl-sermination-domain.com/>; rel=shortlink
-   ObjHeader      Vary: Accept-Encoding
-   ObjHeader      Content-Encoding: gzip
-   ObjHeader      Access-Control-Allow-Origin: *
-   ObjHeader      Max-Age: 600
-   ObjHeader      Content-Length: 6396
-   ObjHeader      Content-Type: text/html; charset=UTF-8
-   ObjHeader      X-Cacheable: YES
-   ObjHeader      cache-control: max-age=900
-   ObjHeader      X-UnsetCookies: TRUE
-   Fetch_Body     3 length stream
-   Gzip           u F - 6396 27271 80 80 51104
-   BackendReuse   20 boot.bedrock
-   Timestamp      BerespBody: 1488390891.863519 0.121514 0.000355
-   Length         6396
-   BereqAcct      331 0 331 435 6396 6831
-   End            

*   << Request  >> 2         
-   Begin          req 1 rxreq
-   Timestamp      Start: 1488390891.741888 0.000000 0.000000
-   Timestamp      Req: 1488390891.741888 0.000000 0.000000
-   ReqStart       10.224.20.146 33370
-   ReqMethod      GET
-   ReqURL         /
-   ReqProtocol    HTTP/1.0
-   ReqHeader      X-Prerender-Token: qUcOM8XD5dRKUvlnCaMx
-   ReqHeader      X-Real-IP: 10.224.20.1
-   ReqHeader      X-Forwarded-For: 10.224.20.1
-   ReqHeader      Host: ssl-sermination-domain.com
-   ReqHeader      X-Forwarded-Proto: https
-   ReqHeader      Connection: close
-   ReqHeader      Cache-Control: max-age=0
-   ReqHeader      Upgrade-Insecure-Requests: 1
-   ReqHeader      User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko)     Chrome/56.0.2924.87 Safari/537.36
-   ReqHeader      Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
-   ReqHeader      DNT: 1
-   ReqHeader      Accept-Encoding: gzip, deflate, sdch, br
-   ReqHeader      Accept-Language: en-US,en;q=0.8
-   ReqHeader      Cookie: __qca=P0-1762908153-1488362085426; _mkto_trk=id:503-BAR-730&token:_mch-ssl-termination.com-1488362085634-71558; calltrk_referrer=direct;     calltrk_landing=https%3A//www.ssl-termination.com/about/; optimizelyEndUserId=oeu1488387324154r0.4330785754032478; op
-   ReqUnset       X-Forwarded-For: 10.224.20.1
-   ReqHeader      X-Forwarded-For: 10.224.20.1, 10.224.20.146
-   VCL_call       RECV
-   ReqUnset       X-Forwarded-For: 10.224.20.1, 10.224.20.146
-   ReqHeader      X-Forwarded-For: 10.224.20.146
-   ReqUnset       Accept-Language: en-US,en;q=0.8
-   ReqUnset       User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko)     Chrome/56.0.2924.87 Safari/537.36
-   ReqUnset       X-Forwarded-Proto: https
-   ReqHeader      X-Forwarded-Proto: https
-   ReqUnset       Cookie: __qca=P0-1762908153-1488362085426; _mkto_trk=id:503-BAR-730&token:_mch-ssl-termination.com-1488362085634-71558; calltrk_referrer=direct;     calltrk_landing=https%3A//www.ssl-termination.com/about/; optimizelyEndUserId=oeu1488387324154r0.4330785754032478; op
-   VCL_return     hash
-   ReqUnset       Accept-Encoding: gzip, deflate, sdch, br
-   ReqHeader      Accept-Encoding: gzip
-   VCL_call       HASH
-   VCL_return     lookup
-   VCL_call       MISS
-   VCL_return     fetch
-   Link           bereq 3 fetch
-   Timestamp      Fetch: 1488390891.863387 0.121500 0.121500
-   RespProtocol   HTTP/1.1
-   RespStatus     200
-   RespReason     OK
-   RespHeader     Date: Wed, 01 Mar 2017 17:54:51 GMT
-   RespHeader     Link: <https://ssl-sermination-domain.com/wp-json/>; rel="https://api.w.org/"
-   RespHeader     Link: <https://ssl-sermination-domain.com/>; rel=shortlink
-   RespHeader     Vary: Accept-Encoding
-   RespHeader     Content-Encoding: gzip
-   RespHeader     Access-Control-Allow-Origin: *
-   RespHeader     Max-Age: 600
-   RespHeader     Content-Length: 6396
-   RespHeader     Content-Type: text/html; charset=UTF-8
-   RespHeader     X-Cacheable: YES
-   RespHeader     cache-control: max-age=900
-   RespHeader     X-UnsetCookies: TRUE
-   RespHeader     X-Varnish: 2
-   RespHeader     Age: 0
-   RespHeader     Via: 1.1 varnish-v4
-   VCL_call       DELIVER
-   RespHeader     x-Cache: uncached
-   RespUnset      X-Varnish: 2
-   VCL_return     deliver
-   Timestamp      Process: 1488390891.863412 0.121524 0.000025
-   RespHeader     Accept-Ranges: bytes
-   Debug          "RES_MODE 2"
-   RespHeader     Connection: close
-   Timestamp      Resp: 1488390891.863608 0.121720 0.000196
-   ReqAcct        1089 0 1089 501 6396 6897
-   End            

*   << Session  >> 1         
-   Begin          sess 0 HTTP/1
-   SessOpen       10.224.20.146 33370 0.0.0.0:8081 10.224.20.148 8081 1488390891.741828 14
-   Link           req 2 rxreq
-   SessClose      RESP_CLOSE 0.122
-   End     

varnishlog при очистке:

*   << Request  >> 98364     
-   Begin          req 98363 rxreq
-   Timestamp      Start: 1488390373.620924 0.000000 0.000000
-   Timestamp      Req: 1488390373.620924 0.000000 0.000000
-   ReqStart       127.0.0.1 56100
-   ReqMethod      PURGE
-   ReqURL         /
-   ReqProtocol    HTTP/1.1
-   ReqHeader      User-Agent: curl/7.35.0
-   ReqHeader      Host: 127.0.0.1:8081
-   ReqHeader      Accept: */*
-   ReqHeader      X-Forwarded-For: 127.0.0.1
-   VCL_call       RECV
-   ReqUnset       X-Forwarded-For: 127.0.0.1
-   ReqHeader      X-Forwarded-For: 127.0.0.1
-   ReqUnset       User-Agent: curl/7.35.0
-   ReqHeader      X-Forwarded-Proto: https
-   VCL_acl        MATCH purge "localhost"
-   VCL_return     purge
-   VCL_call       HASH
-   VCL_return     lookup
-   VCL_call       PURGE
-   VCL_return     synth
-   Timestamp      Process: 1488390373.620977 0.000052 0.000052
-   RespHeader     Date: Wed, 01 Mar 2017 17:46:13 GMT
-   RespHeader     Server: Varnish
-   RespHeader     X-Varnish: 98364
-   RespProtocol   HTTP/1.1
-   RespStatus     200
-   RespReason     OK
-   RespReason     Purged
-   VCL_call       SYNTH
-   RespHeader     Content-Type: text/html; charset=utf-8
-   RespHeader     Retry-After: 5
-   VCL_return     deliver
-   RespHeader     Content-Length: 240
-   Storage        malloc Transient
-   RespHeader     Accept-Ranges: bytes
-   Debug          "RES_MODE 2"
-   RespHeader     Connection: keep-alive
-   Timestamp      Resp: 1488390373.621082 0.000158 0.000106
-   ReqAcct        80 0 80 218 240 458
-   End            

*   << Session  >> 98363     
-   Begin          sess 0 HTTP/1
-   SessOpen       127.0.0.1 56100 0.0.0.0:8081 127.0.0.1 8081 1488390373.620850 18
-   Link           req 98364 rxreq
-   SessClose      REM_CLOSE 0.000
-   End            

Редактировать:

Я считаю, что мне нужна другая команда очистки, потому что если я зайду по ssh в ящик с лаком и запущу следующее:

  1. свернуть URL-адрес локально (без кэширования)
  2. свернуть URL-адрес локально (кешируется)
  3. локальная очистка curl снова
  4. свернуть URL-адрес локально (без кэширования)
  5. свернуть URL-адрес локально (кешируется)

Он работает по назначению.

01.03.2017

Ответы:


1

Я посмотрел на vcl_hash по умолчанию и понял это. . Мне нужно отправить заголовок Host, и он правильно разорвет кеш.

curl -X PURGE http://127.0.0.1:8081/ -H "Host: ssl-termination-domain.com"

01.03.2017
Новые материалы

Основы принципов S.O.L.I.D, Javascript, Git и NoSQL
каковы принципы S.O.L.I.D? Принципы SOLID призваны помочь разработчикам создавать надежные, удобные в сопровождении приложения. мы видим пять ключевых принципов. Принципы SOLID были разработаны..

Как настроить Selenium в проекте Angular
Угловой | Селен Как настроить Selenium в проекте Angular Держите свое приложение Angular и тесты Selenium в одной рабочей области и запускайте их с помощью Mocha. В этой статье мы..

Аргументы прогрессивного улучшения почти всегда упускают суть
В наши дни в кругах веб-разработчиков много болтают о Progressive Enhancement — PE, но на самом деле почти все аргументы с обеих сторон упускают самую фундаментальную причину, по которой PE..

Введение в Джанго Фреймворк
Схема «работать умно, а не усердно» В этой и последующих статьях я познакомлю вас с тем, что такое фреймворк Django и как создать свое первое приложение с помощью простых и понятных шагов, а..

Настольный ПК как «одно кольцо, чтобы править всеми» домашних компьютеров
Вид после 9 месяцев использования С настольных компьютеров все началось, но в какой-то момент они стали «серверами», и мы все перешли на ноутбуки. В прошлом году я столкнулся с идеей настольных..

Расширенные методы безопасности для VueJS: реализация аутентификации без пароля
Руководство, которое поможет вам создавать безопасные приложения в долгосрочной перспективе Безопасность приложений часто упускается из виду в процессе разработки, потому что основная..

стройный-i18следующий
Представляем стройную оболочку для i18next. Эта библиотека, основанная на i18next, заключает экземпляр i18next в хранилище svelte и отслеживает события i18next, такие как languageChanged,..