Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx - read custom header from upstream server

I am using nginx as a reverse proxy and trying to read a custom header from the response of an upstream server (Apache) without success. The Apache response is the following:

HTTP/1.0 200 OK Date: Fri, 14 Sep 2012 20:18:29 GMT  Server: Apache/2.2.17 (Ubuntu) X-Powered-By: PHP/5.3.5-1ubuntu7.10 Connection: close Content-Type: application/json; charset=UTF-8 My-custom-header: 1 

I want to read the value from My-custom-header and use it in a if clause:

location / {     // ...     // get My-custom-header value here     // ... } 

Is this possible? Thanks in advance.

like image 728
luis Avatar asked Sep 14 '12 20:09

luis


People also ask

How do you fix upstream sent too big header while reading response header from upstream?

To resolve this issue, we need to increase the proxy buffers that Nginx uses. Before Nginx sends a response back to your visitor, it will buffer the request it had to make from its upstream. However, there are limited buffers available to buffer such a response.

Does nginx pass headers?

Passing Request HeadersBy default, NGINX redefines two header fields in proxied requests, “Host” and “Connection”, and eliminates the header fields whose values are empty strings. “Host” is set to the $proxy_host variable, and “Connection” is set to close .

How does Nginx resolve upstream?

Upstream Domain Resolve¶ Its buffer has the latest IPs of the backend domain name and it integrates with the configured load balancing algorithm (least_conn, hash, etc) or the built in round robin if none is explicitly defined. At every interval (one second by default), it resolves the domain name.


2 Answers

It's not only possible, it's easy:

in nginx the response header values are available through a variable (one per header). See http://wiki.nginx.org/HttpCoreModule#.24sent_http_HEADER for the details on those variables.

In your examle the variable would be $sent_http_My_custom_header.

like image 130
cobaco Avatar answered Sep 20 '22 03:09

cobaco


I was facing the same issue. I tried both $http_my_custom_header and $sent_http_my_custom_header but it did not work for me.

Although solved this issue by using $upstream_http_my_custom_header.

like image 24
Dev Gosain Avatar answered Sep 21 '22 03:09

Dev Gosain