Homepage cache and cookies

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
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
other users. Is there a way to still have the cookies set from the
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_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 ~*
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:

The proxy_cache_key defines which key differentiate any cache object. By
using $cookie_name in the string, the value of the cookie ‘name’ will
taken as part of the key.
If multiple clients have the same combination of variables value
(specifically, for cookie, presence, no presence or same value-s), they
will grab the same object from the cache. If different content might get
produced/grabbed with the same value in variables, then you have a

As a sidenote, you are using proxy_pass_header Set-Cookie with
proxy_hide_header Set-Cookie, which conflict one with each other. Remove
them altogether.

B. R.

On Thu, Mar 24, 2016 at 2:14 PM, john_smith77
[email protected]

Thanks for the info. I have removed the redundant config. I suppose what
am really getting at is that I would like Set-Cookie to never be cached
a cache MISS so that the cached cookie values are then not there for
subsequent HITS.

Posted at Nginx Forum:

Indeed, but that prevents all cookies from being set. What I am looking
is to have a cache hit, but have the set-cookie served from the origin
server. In Apache httpd cache, this can be accomplished with
CacheIgnoreHeaders Set-Cookie.

The back end server here is Apache and I have tried setting it to send
this header:


There just does not seem to be a combination of options for nginx that
allows this to work.

I’ve tried this in nginx both with scenarios:

nginx -> Apache httpd -> wordpress
nginx-> Apache httpd -> tomcat

In both cases, proxy_hide_header will prevent the cookies from ever

Posted at Nginx Forum:

So I found a solution and it seems like there is unexpected behavior
nginx. proxy_hide_header Set-Cookie does not seem to work when the
block is set to /


location / {
#Lots of other proxy stuff here…
proxy_hide_header “Set-Cookie”; }

does not allow a cookie to ever be set, but:

location ~home.html {
#Lots of other proxy stuff here…
proxy_hide_header “Set-Cookie”; }

will allow cache HITS but won’t cache Set-Cookie headers. Clients are
able to get the cookies from the back end and will never get another
session cookies.

Posted at Nginx Forum:

That seems to match what the docs for proxy_hide_header
states, just do not use proxy_pass_header at the same time… which does
the exact opposite.

B. R.

On Thu, Mar 24, 2016 at 9:31 PM, john_smith77
[email protected]

Hello John!

I think you can do this better with sessions in NginX.

I’ve made a script that checks if a client is a bot or not,
by testing if the Google Analytics cookie is here, or not.

If it is, the client can post a comment or login. If not, the
server is giving a fake page.

In my PHP script :

$host = $_SERVER['REMOTE_ADDR'];
$session_id = session_id();
$session_name = session_name();
    if ( '' == $session_name ) {
      $session_name = '0';
      echo '<html><head></head><body><h1>server busy, come back 


All pages are cached :wink:


Ph. Gras
Le 25 mars 2016 22:33, john_smith77 [email protected] a
crit :

Specifically I was writing that clients could get their cookies, but not
served form the nginx cache. This has always been about whether or not
cached page was serving the cookies or not.

With the exact same configuration for the cache, there is a difference

location /


location /~home.html

in regards to whether or not the cached version of the page has the
Set-Cookie headers in it.

I will try and research some more to see if that would be expected or

Posted at Nginx Forum:

Make sure you test this unexpected behavior with a minimal, reproducible
If you succeed, please share it or (even better!) fill a bug report
https://trac.nginx.org/. Otherwise, that meant you probably did

​You were previously saying proxy_hise_headers did not pass cookies to
client. Now yo uare saying clients are able to grab their cookie.
I am a bit confused, here…​

B. R.

On Fri, Mar 25, 2016 at 10:33 PM, john_smith77
[email protected]