2

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.

2
  • Might the issue be that we’re not entering in the “if statement”? Meaning that $http_x_my_custom_variable is not actually set to “1”? Commented Jun 25, 2018 at 20:41
  • I don't think so. I confirmed that the header is set to "1" on the backend. Commented Jun 26, 2018 at 8:41

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.