Homepage cache and cookies

john_smith77 nginx-forum at forum.nginx.org
Thu Mar 24 13:14:06 UTC 2016


I am trying to cache the home page to a site. I am only caching the home
page. If I put in a condition to check for cookies existing before caching,
everything works as expected, but there is a high BYPASS rate due to the
client not having the cookies the first time they visit the site. Once I
took out the check for cookies, clients started getting cached cookies from
other users. Is there a way to still have the cookies set from the origin
server, and have a cache hit? 

server {
    listen       80;

    if ($request_uri !~* "home.html") {
                set $skip_cache 1; }

    location / {
        proxy_pass http://backend;
        proxy_set_header Host      $host;
        proxy_cache_bypass $skip_cache;
        proxy_no_cache $skip_cache;
        proxy_pass_header Set-Cookie;
        proxy_cache one;
        proxy_cache_key
$scheme$proxy_host$uri$is_args$args$cookie_myCookie$cookie_myOtherCookie;
        proxy_cache_valid  200 302  10m;
        proxy_cache_valid  404      1m;
        proxy_ignore_headers "Set-Cookie" "Cache-Control" "Expires";
        proxy_hide_header "Set-Cookie";
        proxy_cache_lock on;
        if ($http_user_agent ~*
'(iPhone|Android|Phone|PalmSource|BOLT|Symbian|Fennec|GoBrowser|Maemo|MIB|Minimo|NetFront|Presto|SEMC|Skyfire|TeaShark|Teleca|uZard|palm|psp|openweb|mmp|novarra|nintendo
ds|hiptop|ipod|blackberry|up.browser|up.link|wap|windows
ce|blackberry|iemobile|bb10)' ) {
                proxy_pass http://backend_mobile; }
        if ($http_user_agent ~* '(iPad|playbook|hp-tablet|kindle|Silk)' ) {
                proxy_pass http://backend_tablet; }

        root   /opt/rh/rh-nginx18/root/usr/share/nginx/html;
        index  index.html index.htm;
    }

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,265629,265629#msg-265629



More information about the nginx mailing list