O
O
Otrivin2020-10-13 15:34:47
Nginx
Otrivin, 2020-10-13 15:34:47

How to perform an action depending on the presence of a cookie?

Hello!

You need to add a header and set a cookie if it doesn't exist. Tried like this:

map $http_cookie $res_pushed {
  default "false";
  "~*res_pushed" "true"; # Если есть кука res_pushed с любым значением, создаём $res_pushed = true. Иначе false.
}
server{
  http2_push_preload on;
  location /{
    if ($res_pushed = "false") {
      add_header Set-Cookie "res_pushed=true; Max-Age=20; Path=/"; # Если $res_pushed = false, создаём куку
      add_header Link "</style.css>;rel=preload;as=style"; # и добавляем нужный заголовок
    }
  }
}


But the header is not added, the cookie is not created. Where did I go wrong?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question