# Set arbitrary http header?

**URL:** <https://www.ruby-forum.com/t/set-arbitrary-http-header/229430>\
**Category:** NGINX\
**Created:** [February 1, 2013, 8:21pm UTC](https://www.ruby-forum.com/t/set-arbitrary-http-header/229430 "2013-02-01T20:21:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![zacharyalexstern](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/6a8cbe/32.png) [@zacharyalexstern](https://www.ruby-forum.com/u/zacharyalexstern)\
**Post date:** [February 1, 2013, 8:21pm UTC](https://www.ruby-forum.com/t/set-arbitrary-http-header/229430/1 "2013-02-01T20:21:49Z")

</div>

Currently in our setup, we are using the X-Real-IP header setting in  
nginx,  
to tell in our backend apache logs the IP address of the client.

Is it possible to set up nginx to insert an arbitrary HTTP header with  
text  
of my choosing, or with it’s own IP address?

I’d like to set this up for the purposes of doing some easier log  
analysis.

Thanks!

Posted at Nginx Forum:

> **[set arbitrary http header?](https://forum.nginx.org/read.php?2,235827,235827#msg-235827)**
>
> Currently in our setup, we are using the X-Real-IP header setting in nginx, to tell in our backend apache logs the IP address of the client. Is it possible to set up nginx to insert an arbitrary HTTP header with text of my choosing, or with it's own...

---

<div class="post-metadata">

**Author:** ![zacharyalexstern](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/6a8cbe/32.png) [@zacharyalexstern](https://www.ruby-forum.com/u/zacharyalexstern)\
**Post date:** [February 1, 2013, 8:37pm UTC](https://www.ruby-forum.com/t/set-arbitrary-http-header/229430/2 "2013-02-01T20:37:01Z")

</div>

On Fri, Feb 01, 2013 at 02:21:05PM -0500, zacharyalexstern wrote:

Hi there,

> Currently in our setup, we are using the X-Real-IP header setting in nginx,  
> to tell in our backend apache logs the IP address of the client.

Which line in your nginx configuration is doing that?

> Is it possible to set up nginx to insert an arbitrary HTTP header with text  
> of my choosing, or with it’s own IP address?

Yes.

What happens if you copy the current line, and change some parts of it?

It should be straightforward enough to test.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![zacharyalexstern](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/6a8cbe/32.png) [@zacharyalexstern](https://www.ruby-forum.com/u/zacharyalexstern)\
**Post date:** [February 1, 2013, 8:44pm UTC](https://www.ruby-forum.com/t/set-arbitrary-http-header/229430/3 "2013-02-01T20:44:37Z")

</div>

Ah, so, we have this line:  
proxy\_set\_header X-Real-IP $remote\_addr;

Is there a list of the variables similar to $remote\_addr somewhere?  
Is $proxy\_host possibly what we need?

Posted at Nginx Forum:

> **[Re: set arbitrary http header?](https://forum.nginx.org/read.php?2,235827,235830#msg-235830)**
>
> Currently in our setup, we are using the X-Real-IP header setting in nginx, to tell in our backend apache logs the IP address of the client. Is it possible to set up nginx to insert an arbitrary HTTP header with text of my choosing, or with it's own...

---

<div class="post-metadata">

**Author:** ![zacharyalexstern](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/6a8cbe/32.png) [@zacharyalexstern](https://www.ruby-forum.com/u/zacharyalexstern)\
**Post date:** [February 1, 2013, 8:57pm UTC](https://www.ruby-forum.com/t/set-arbitrary-http-header/229430/4 "2013-02-01T20:57:30Z")

</div>

I’d like nginx to set a header that contains the IP address of the  
server  
nginx is running on.

So something like:  
proxy\_set\_header X-Nginx-IP $proxy\_host;

Assuming $proxy\_host evaluates to the IP of the server nginx is running  
on.

Posted at Nginx Forum:

> **[Re: set arbitrary http header?](https://forum.nginx.org/read.php?2,235827,235833#msg-235833)**
>
> Currently in our setup, we are using the X-Real-IP header setting in nginx, to tell in our backend apache logs the IP address of the client. Is it possible to set up nginx to insert an arbitrary HTTP header with text of my choosing, or with it's own...

---

<div class="post-metadata">

**Author:** ![zacharyalexstern](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/6a8cbe/32.png) [@zacharyalexstern](https://www.ruby-forum.com/u/zacharyalexstern)\
**Post date:** [February 1, 2013, 9:41pm UTC](https://www.ruby-forum.com/t/set-arbitrary-http-header/229430/5 "2013-02-01T21:41:56Z")

</div>

On Fri, Feb 01, 2013 at 02:57:00PM -0500, zacharyalexstern wrote:

> I’d like nginx to set a header that contains the IP address of the server  
> nginx is running on.

There is (almost certainly) not exactly one IP address that fits that  
description.

Maybe $server\_addr is adequate?

Easiest is probably “proxy\_set\_header X-Nginx-IP 10.11.12.13;” on the  
server that you want to identify as 10.11.12.13.

But if you wind back to _why_ you want that – apache already knows what  
ip address the connection to it came from (which should be an address of  
the server that nginx is running on).

It would have logged it, except that you configured apache to discard  
that address and instead use the content of the X-Real-IP header.

Possibly changing the apache configuration to log the content of the  
X-Real-IP header as well as its client ip address is easiest of all.

Whether that is appropriate in your environment depends on what else  
your apache does with the information.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![zacharyalexstern](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/6a8cbe/32.png) [@zacharyalexstern](https://www.ruby-forum.com/u/zacharyalexstern)\
**Post date:** [February 1, 2013, 8:53pm UTC](https://www.ruby-forum.com/t/set-arbitrary-http-header/229430/6 "2013-02-01T20:53:59Z")

</div>

On Fri, Feb 01, 2013 at 02:44:17PM -0500, zacharyalexstern wrote:

> Ah, so, we have this line:  
> proxy\_set\_header X-Real-IP $remote\_addr;

[Module ngx\_http\_proxy\_module](http://nginx.org/r/proxy_set_header) for details.

> Is there a list of the variables similar to $remote\_addr somewhere?

> **[Module ngx\_http\_core\_module](http://nginx.org/en/docs/http/ngx_http_core_module.html#variables)**
>
> This directive appeared in version 1.11.8.

Other modules may provide other variables.

> Is $proxy\_host possibly what we need?

Possibly.

It really depends on what you wish to achieve, which I don’t think is  
clear yet from this thread.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![zacharyalexstern](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/6a8cbe/32.png) [@zacharyalexstern](https://www.ruby-forum.com/u/zacharyalexstern)\
**Post date:** [February 1, 2013, 9:47pm UTC](https://www.ruby-forum.com/t/set-arbitrary-http-header/229430/7 "2013-02-01T21:47:47Z")

</div>

There are several nodes in between nginx and apache. It’s not internet  
 →   
nginx → apache.

It’s internet → nginx → varnish → haproxy - \> apache1  
- \>  
apache2  
- \>  
apacheN

So far, everything is in the apache access logs except the IP of the  
nginx  
server connecting.

But you’re right, I could just set it manually, for each nginx server. I  
might just do that.

And you’re also again right that our servers have multiple IPs.

Posted at Nginx Forum:

> **[Re: set arbitrary http header?](https://forum.nginx.org/read.php?2,235827,235837#msg-235837)**
>
> Currently in our setup, we are using the X-Real-IP header setting in nginx, to tell in our backend apache logs the IP address of the client. Is it possible to set up nginx to insert an arbitrary HTTP header with text of my choosing, or with it's own...
