# How to unset header in nginx module

**URL:** <https://www.ruby-forum.com/t/how-to-unset-header-in-nginx-module/217937>\
**Category:** NGINX\
**Created:** [March 16, 2012, 5:58pm UTC](https://www.ruby-forum.com/t/how-to-unset-header-in-nginx-module/217937 "2012-03-16T17:58:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [March 16, 2012, 5:58pm UTC](https://www.ruby-forum.com/t/how-to-unset-header-in-nginx-module/217937/1 "2012-03-16T17:58:54Z")

</div>

Hi,

I’d like to delete particular headers\_in like a apr\_table\_unset() in  
request handler of my module.

I wrote examples from the article:  
[http://wiki.nginx.org/HeadersManagement](http://wiki.nginx.org/HeadersManagement)

static void unset\_header(ngx\_http\_request\_t \*r, u\_char \*key){  
ngx\_list\_part\_t \*part;  
ngx\_table\_elt\_t \*h;  
ngx\_uint\_t i;  
size\_t len = strlen((char \*)key);

```
part = &r->headers_in.headers.part;
h = part->elts;
for (i = 0; ; i++) {
    if (i >= part->nelts) {
        if (part->next == NULL) {
            break;
        }
        part = part->next;
        h = part->elts;
        i = 0;
    }
    if (len != h[i].key.len || ngx_strcasecmp(key, h[i].key.data) != 

```

1. {  
h[i].hash = 0;  
h[i].key.len = 0;  
h[i].key.data = NULL;  
h[i].value.len = 0;  
h[i].value.data = NULL;  
h[i].lowcase\_key = NULL;  
}  
}  
}

But this code send empty header(only colon) line to backend.  
like this:

:

Should I reconstruct list of headers\_in.headers?  
Or, should I modify http main module that to avoid sending null header?  
Or, Is there any better(faster) way?

Thank you.

---

<div class="post-metadata">

**Author:** ![Tsukasa\_Hamano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6a8cbe/32.png) [@Tsukasa\_Hamano](https://www.ruby-forum.com/u/Tsukasa_Hamano)\
**Post date:** [March 16, 2012, 6:33pm UTC](https://www.ruby-forum.com/t/how-to-unset-header-in-nginx-module/217937/2 "2012-03-16T18:33:04Z")

</div>

Hello!

On Sat, Mar 17, 2012 at 01:58:14AM +0900, Tsukasa Hamano wrote:

> ```
> ngx_table_elt_t *h;
> part = part->next;
> }
> 
> ```
> 
> Or, Is there any better(faster) way?  
> You shouldn’t touch r-\>headers\_in at all. It represents headers  
> got from client, and it’s incorrect to change them.

Instead, to restrict headers passed to backends you should use  
functionality provided by relevant backend modules, e.g. with  
proxy\_pass use

```
proxy_set_header X-Some-Header "";

```

Maxim D.

---

<div class="post-metadata">

**Author:** ![Tsukasa\_Hamano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6a8cbe/32.png) [@Tsukasa\_Hamano](https://www.ruby-forum.com/u/Tsukasa_Hamano)\
**Post date:** [March 17, 2012, 9:34am UTC](https://www.ruby-forum.com/t/how-to-unset-header-in-nginx-module/217937/3 "2012-03-17T09:34:52Z")

</div>

Hi Maxim,

Thank you for your advice.

At Fri, 16 Mar 2012 21:32:23 +0400,  
Maxim D. wrote:

> proxy\_pass use
> 
> ```
> proxy_set_header X-Some-Header "";
> 
> ```

I want to modify and unset header based on client request. Apache can  
do it. Is there a plan to make something like apr\_table\_unset()?

I found the following code:

> <https://github.com/openresty/headers-more-nginx-module/blob/master/src/ngx_http_headers_more_util.c>

ngx\_http\_headers\_more\_rm\_header()

I think I have no choise other than this code. but It looks  
inefficient and slow.  
Perhaps we can give meaning to key.len = 0 is unset flag, because  
empty header name is not permitted by RFC.

Thanks for your comment.
