Conditionally updating cookie expiry time in nginx to find the idle time

Viewed 149

I'm trying to implement a solution to identify if there was an idle time of X minutes between two requests.

For this, I'm planning to use the following approach.

  1. Initially, set a cookie from the application side. The expiry time of this cookie is set to X minutes.
  2. Whenever there is a request, in nginx, I will check if the cookie expiry time is passed or the cookie is available.
  3. If the cookie is available, and not expired, I will update the expiry time again with X minutes. Otherwise, delete the cookie.
  4. On the server-side, I can check the availability of the cookie and if available, I can conclude the previous request was within X minutes.

To implement this solution, I will need some code snippets as below.

if cookie-exists and cookie.expirytime > currentTime
  Set-Cookie: MY_COOKIE=SOMEVAL; Max-Age=X*60; Secure; HttpOnly
else
  Drop-Cookie MY_COOKIE
endif

How can I implement this condition check and cookie modification in nginx? Is it possible to do without lua?

I don't want to implement this logic on the application side because - there are multiple applications served through nginx and I want to keep the logic common to all. If the user is hitting any of the applications, the cookie should be updated.

1 Answers

Browsers do not send expired cookies, so Nginx will never receive it. You could potentially set 2 cookies: one that expires, and another that identifies repeat visitors.

Related