Score:0

Is it possible to set proxy header in NGINX using variable for the *header name*?

us flag

According to the NGINX documentation

proxy_set_header field value allows redefining or appending fields to the request header passed to the proxied server. The value can contain text, variables, and their combinations.

So I can do

set $my_variable "some_value";
proxy_set_header x-my-header $my_variable;

Is it anyhow possible to use a variable for the field parameter, i.e. have the header name based on a variable? I want to be able to configure NGINX like

set $my_variable "x-my-header";
proxy_set_header $my_variable "some_value";
Score:1
gr flag

When nginx documentation does not explicitly says that you can use variables for some directive parameter, generally it means you cannot. In particular you can't use variables as the header names in add_header, proxy_set_header and some other related directives. However you can do it using third party modules, e.g. lua-nginx-module via ngx.req.set_header. You can take a look at the OpenResty bundle which includes aforementioned module and is being packaged for a wide range of OS distributions.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.