How to remove certain cookies from nginx response

13,699

Although you already mentioned that you switched to Varnish to accomplish what you asked for, the correct answer would have been to use the headers-more-nginx-module which basically allows you the same as the Varnish function does (and much more).

Share:
13,699
Martin Taleski
Author by

Martin Taleski

IT enthusiast

Updated on September 16, 2022

Comments

  • Martin Taleski
    Martin Taleski over 1 year

    I have nginx set up as a reverse proxy server and I want to remove certain cookies set on the backed server (apache)

    My website uses a lot of cookies which I can not control (Expression Engine CMS, don't ask me why). I want to delete some of those cookies (lets say cookies A B and C) and keep some other (cookies D and E).

    After that I will set up nginx to respond with cached content only if the request has no cookies.

    Do you have any idea how to do this? Thanks

    So far I have in my config:

    proxy_cache_path /opt/nginx/cache levels=1:2 keys_zone=mycache:20m max_size=1G;
    proxy_temp_path /opt/nginx/tmp_cache/;
    proxy_ignore_headers Expires Cache-Control Set-Cookie;
    proxy_cache_use_stale error timeout invalid_header http_502;
    proxy_cache_bypass $cookie_nocache;
    proxy_no_cache $cookie_nocache;
    

    ...

    location / {
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_cache mycache;
        proxy_cache_valid  200 302  6h;
        proxy_cache_valid  404      1m;
        proxy_pass http://x.x.x.x:8080;
    }
    
  • Alice Purcell
    Alice Purcell over 3 years
    Please include an example of how you might use this module to achieve the OP's goals. (I can't see any way with this module to remove only one cookie and it would be really helpful — and this question, despite being closed, is still what Google puts at the top of its search results.)