кешировать только ответы где есть определённый Set-Cookie

VovansystemS vovansystems на gmail.com
Чт Июл 7 11:59:34 UTC 2022


Добрый день,

нужно избирательно кешировать ответы бэкэнда в nginx. Некоторые ответы
содержат Set-Cookie заголовки.По-умолчанию их кешировать не нужно, но
если встречается определённая куки, то такой ответ нужно кешировать.

пример:

кешируем ответ с заголовком:
Set-Cookie: pll_language=en; expires=Fri, 07-Jul-2023 11:37:39 GMT;
Max-Age=31536000; path=/; secure; SameSite=Lax

не кешируем ответ с сессией пользователя с заголовком:
Set-Cookie: login=i324iuhkj324; expires=Fri, 10-Jul-2023 11:37:39 GMT;
Max-Age=31536000; path=/; secure

пытаюсь делать так:

map $upstream_http_set_cookie $bypass_cache {
   "~*.pll"     0;
    default    1;
}

server {
[..]
location @granted {
[..]
        proxy_ignore_headers Set-cookie;
        proxy_no_cache $bypass_cache;
        proxy_cache_bypass $bypass_cache;
        add_header X-Bypass $bypass_cache;
        add_header X-upstream-set-cookie "aaa $upstream_http_set_cookie";
[..]
}
[..]
}

в ответе получаю:
X-Bypass: 1
X-upstream-set-cookie: aaa pll_language=en; expires=Fri, 07-Jul-2023
11:37:39 GMT; Max-Age=31536000; path=/; secure; SameSite=Lax

такое впечатление, что директива add_header корректно видит содержимое
заголовка ответа апстрима, а вот map (и if тоже пытался) - не видят
содержимого ни $upstream_http_set_cookie ни
$upstream_cookie_pll_language.

Может быть есть какие-то мысли как такое лучше реализовать и возможно
ли это вообще?

nginx -v
nginx version: nginx/1.19.2

nginx -V
nginx version: nginx/1.19.2
built by gcc 8.3.0 (Debian 8.3.0-6)
built with OpenSSL 1.1.1d  10 Sep 2019
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx
--modules-path=/usr/lib/nginx/modules
--conf-path=/etc/nginx/nginx.conf
--error-log-path=/var/log/nginx/error.log
--http-log-path=/var/log/nginx/access.log
--pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock
--http-client-body-temp-path=/var/cache/nginx/client_temp
--http-proxy-temp-path=/var/cache/nginx/proxy_temp
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp
--http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx
--group=nginx --with-compat --with-file-aio --with-threads
--with-http_addition_module --with-http_auth_request_module
--with-http_dav_module --with-http_flv_module
--with-http_gunzip_module --with-http_gzip_static_module
--with-http_mp4_module --with-http_random_index_module
--with-http_realip_module --with-http_secure_link_module
--with-http_slice_module --with-http_ssl_module
--with-http_stub_status_module --with-http_sub_module
--with-http_v2_module --with-mail --with-mail_ssl_module --with-stream
--with-stream_realip_module --with-stream_ssl_module
--with-stream_ssl_preread_module --with-cc-opt='-g -O2
-fdebug-prefix-map=/data/builder/debuild/nginx-1.19.2/debian/debuild-base/nginx-1.19.2=.
-fstack-protector-strong -Wformat -Werror=format-security
-Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now
-Wl,--as-needed -pie'


Подробная информация о списке рассылки nginx-ru