Is there a way to check if a specific cookie exist in nginx?
For now I have a section like below to set header from cookie:
proxy_set_header x-client-id $cookie_header_x_client_id;
I want to check if that cookie exist then set the header, otherwise do not override header.
I've tried:
if ($cookie_header_x_client_id) {
proxy_set_header x-client-id $cookie_header_x_client_id;
}
But it does not work and gives the error below:
"proxy_set_header" directive is not allowed here in /etc/nginx/sites-enabled/website:45
Any solution?
There are only a limited number of directives that are allowed within an
ifcontext in nginx. This is related to the fact thatifis part of therewritemodule; as such, within its context, you can only use the directives that are specifically outlined within the documentation of the module.The common way around this "limitation" is to build up state using intermediate variables, and then use directives like
proxy_set_headerusing such intermediate variables: