7

I have nodejs and nginx running i am sending an additional header in the API 'api_key' and its not received in req.headers and req.get('api_key') in nodejs i have bellow configuration file for nginx

server {
    listen 80;
    listen [::]:80 default_server ipv6only=on;
    server_name mysite.com;
    return 301 https://$host$request_uri;
 location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-NginX-Proxy true;
    proxy_pass http://localhost:9102/;
    proxy_set_header Host $http_host;
    proxy_set_header api_key $http_api_key; 
    #proxy_set_header api_key 'Some Value'; works 
    proxy_redirect off;
  }
}

If set the value of proxy_set_header api_key 'some value' it works and headers are printed on console but the api_key is subjected to change that's why i am using $http_api_key so that whatever comes in api_key custom header is received as it is sent from rest client. I have tried couple of solutions like proxy_set_header api_key $upstream_http_api_key; but no help. I want to receive any custom header sent from rest client in nodejs.

1
  • 1
    Add proxy_pass_header api_key; and not set it yourself. See if that helps Commented Sep 25, 2017 at 7:17

1 Answer 1

16

By default, nginx does not pass headers containing underscores.

Try:

underscores_in_headers on;

See this document for details.

Alternatively, use API-KEY or X-API-KEY instead of API_KEY.

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

2 Comments

Is there some similar config for node.js server running via forever? This is how we run our application - /bin/forever start -o logs/out.log -e logs/err.log app.js.
im having similar issues, i added underscores_in_headers on; but still undefined... stackoverflow.com/questions/51265803/…

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.