erika_dike
erika_dike

Reputation: 351

nginx - read custom header set on client

I am using nginx as a reverse proxy. I am trying to read a custom header set on the client, so I can update a variable with the value of that header.

I set the header in an XHR request similar to xhr.setRequestHeader(‘X-My-Custom-Variable', "1"); xhr.setRequestHeader(‘X-My-Second-Custom-Variable’, 'some-value');

From the docs, I see that I can achieve this on nginx with:

if ($http_x_my_custom_variable = ‘1’) {
    set $variable $http_x_my_second_custom_variable;
}

However, this doesn’t work when I run it, $variable is not set.

I have also tried using nginx map keyword like so:

map $http_x_my_custom_variable $variable {
   default “”;
   “1” $http_x_my_second_custom_variable;
}

and still nothing happens.

I have also confirmed that both headers are set to what I expect on the backend Django server.

Note: My actual header names looks more like: X-ABM-ZHR-XAVIER. I only state this just incase it might have something to do with the structure of the header name. Although I have also tried different permutations of header names just in case.

Upvotes: 2

Views: 7796

Answers (1)

Nitb
Nitb

Reputation: 384

Try not using any dash in the variable name.

From client side pass XMyCustomVariable and on nginx read it with $http_XMyCustomVariable (or lowercase).

I know it is not elegant but as a first step towards debugging, I would confirm that part.

Also, try using one of the known variable names (again for debugging) e.g. send the variable 1 value in user agent header and check on nginx using $http_user_agent

Upvotes: 1

Related Questions