Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom headers not received in nodejs while using nginx reverse proxy

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.

like image 690
valar morghulis Avatar asked Jan 04 '23 10:01

valar morghulis


1 Answers

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.

like image 112
Richard Smith Avatar answered Apr 06 '23 09:04

Richard Smith