This article is short, but it is my biggest gain this week, also hope to help the students who read this article, less step on a pit.



NodeJS is commonly deployed through NGINX as a proxy for load balancing.


The front end defines a request header user_id to identify each user’s request. During debugging, it is ok to access everything through IP +port. When nginx is configured as a proxy for forwarding, everything starts to become bad. The unique user id user_id is no longer available at the Node layer.


I initially thought it was a Node function call that didn’t get the request header correctly,

It is suspected again that Nginx did not pass the request header proxy to the Node service.


So, where did my custom request header go?

If it is OK not to go to the nginx agent, then obviously Node is not to blame.


Who was responsible for the disappearance?

Once again I was lost in thought…

.

.

.

.

.

.

.

.

.

.

.

.

.

.

Suddenly, the God of wisdom came and the Google prophet told me that NGINX had hidden the request headers himself.


By default, the nginx proxy removes the “_” underscore from the header argument, so the backend Node service does not get the parameter name with the “_” line.


What do you think, Yuanfang?

The truth is that the user_id parameter of the client request contains “_”, which the nginx agent ignores by default.

Really is coincidentally into the book, one foot into this pit.

If you change the custom request header user_id to userId, you won’t fall into this hole.


What if you had to use the custom request header user_id?


Set underscores_in_headers from off to ON.

underscores_in_headers on; 
# This property defaults to off, indicating that if the header name contains an underscore, it is ignored.Copy the code


Syntax: underscores_in_headers on | off;

Default:
underscores_in_headers off;Copy the code
Context: http.server

Enables or disables the use of underscores in client request header fields. When the use of underscores is disabled, request header fields whose names contain underscores are marked as invalid and become subject to the ignore_invalid_headers directive.


NGINX documents refer to: http://nginx.org/en/docs/http/ngx_http_core_module.html#underscores_in_headers


Author: Black Horse big front end Cuitianze