# Nginx caching no header content-length

**URL:** <https://www.ruby-forum.com/t/nginx-caching-no-header-content-length/244182>\
**Category:** NGINX\
**Created:** [March 2, 2016, 5:28pm UTC](https://www.ruby-forum.com/t/nginx-caching-no-header-content-length/244182 "2016-03-02T17:28:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafa\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@Rafa\_F](https://www.ruby-forum.com/u/Rafa_F)\
**Post date:** [March 2, 2016, 5:28pm UTC](https://www.ruby-forum.com/t/nginx-caching-no-header-content-length/244182/1 "2016-03-02T17:28:40Z")

</div>

Hi,

I am using nginx(caching server) in front my tomcat. My tomcat is  
serving  
header(content-length). But my nginx is not serving header  
content-length.

However, i can put that header in my config with some static value then  
it  
will serve. But, in my case, the content length is coming from tomcat  
server.

Could anybody please help me here.

my config file

location /comet {

```
    chunked_transfer_encoding off;
    proxy_http_version 1.1;
    proxy_buffering on;

    proxy_pass http://tomcat:8080/xxxx

        proxy_cache my_cache;

        # Adding a header to see the cache status in the browser
        add_header X-Cache-Status $upstream_cache_status;

        proxy_cache_key "$scheme$request_uri $http_x_seneca_uid";
        proxy_cache_min_uses 1;
        proxy_cache_valid 200 302 120m;
        proxy_cache_valid 404 1m;
        proxy_cache_use_stale error timeout invalid_header http_500

```

http\_502 http\_503 http\_504;

```
      }

```

==============\>

My problem:- i have to pass content-header to my client. Could any body  
please help me here

---

<div class="post-metadata">

**Author:** ![Vikas\_Parashar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f08c70/32.png) [@Vikas\_Parashar](https://www.ruby-forum.com/u/Vikas_Parashar)\
**Post date:** [March 2, 2016, 5:49pm UTC](https://www.ruby-forum.com/t/nginx-caching-no-header-content-length/244182/2 "2016-03-02T17:49:07Z")

</div>

Hello!

On Wed, Mar 02, 2016 at 09:58:26PM +0530, Vikas Parashar wrote:

> I am using nginx(caching server) in front my tomcat. My tomcat is serving  
> header(content-length). But my nginx is not serving header content-length.
> 
> However, i can put that header in my config with some static value then it  
> will serve. But, in my case, the content length is coming from tomcat  
> server.

The “Content-Length” header is expected to be automatically  
removed by nginx if it does some processing on the response body  
and the length can be changed during processing. In particular,  
this includes the following cases:

- the response is gzipped (“gzip on”);
- SSI is enabled;
- sub\_filter is enabled.

Various 3rd party modules can also remove Content-Length.

If you want to preserve Content-Length, you should identify which  
modules remove Content-Length in your particular configuration and  
switch them off.

[…]

> ```
> chunked_transfer_encoding off;
> 
> ```

Note: this directive in your config prevents nginx from using  
“Transfer-Encoding: chunked” to indicate response length. As a  
result nginx won’t be able to maintain persistent connections with  
clients when Content-Length isn’t known in advance.

Unless you’ve added this directive to resolve specific problems  
with broken clients and you know for sure you need it, you may  
want to remove this directive from your configuration.

–  
Maxim D.

> **[nginx](http://nginx.org/)**

---

<div class="post-metadata">

**Author:** ![Vikas\_Parashar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f08c70/32.png) [@Vikas\_Parashar](https://www.ruby-forum.com/u/Vikas_Parashar)\
**Post date:** [March 2, 2016, 6:04pm UTC](https://www.ruby-forum.com/t/nginx-caching-no-header-content-length/244182/3 "2016-03-02T18:04:18Z")

</div>

Thanks for your promot response.

location /xxxxx {

error\_log /var/log/nginx/contenttroubleshooterror.log debug;  
#chunked\_transfer\_encoding off;  
#proxy\_http\_version 1.1;  
#proxy\_buffering on;  
#proxy\_pass\_header Content-Length;

proxy\_pass [http://tomcat:8080/xxxxx](http://tomcat:8080/xxxxx);

proxy\_cache my\_cache;

# Adding a header to see the cache status in the browser

add\_header X-Cache-Status $upstream\_cache\_status;

# sub\_filter\_last\_modified on;

#add\_header ‘Content-Length’ $upstream\_http\_content\_length;  
#add\_header ‘Content-Length’ 12312;

proxy\_cache\_key “$scheme$request\_uri $http\_x\_seneca\_uid”;  
proxy\_cache\_min\_uses 1;  
proxy\_cache\_valid 200 302 120m;  
proxy\_cache\_valid 404 1m;  
proxy\_cache\_use\_stale error timeout invalid\_header http\_500 http\_502  
http\_503 http\_504;  
}

don’t know how to do it. Could you please let me know, how to add  
header  
forcefully.  
like add\_header ‘Content-Length’ $upstream\_http\_content\_length;

somehow,

add\_header ‘Content-Length’ 12312312

Static value is working fine. But at the moment, i replaced it from  
upstream\_http\_content\_length

it did not work. Any Luck

---

<div class="post-metadata">

**Author:** ![Vikas\_Parashar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f08c70/32.png) [@Vikas\_Parashar](https://www.ruby-forum.com/u/Vikas_Parashar)\
**Post date:** [March 2, 2016, 6:53pm UTC](https://www.ruby-forum.com/t/nginx-caching-no-header-content-length/244182/4 "2016-03-02T18:53:57Z")

</div>

Hello!

On Wed, Mar 02, 2016 at 10:33:34PM +0530, Vikas Parashar wrote:

[…]

> don’t know how to do it.

Check your configuration for any modules which are expected to  
change content. Disable them (comment out relevant directives out  
or switch off in a particular location).

In particular, check for the following directives:

- ssi;
- sub\_filter;
- gzip, gunzip;
- add\_before\_body, add\_after\_body.

More details can be found here:

> **[Module ngx\_http\_ssi\_module](http://nginx.org/en/docs/http/ngx_http_ssi_module.html)**

> **[Module ngx\_http\_sub\_module](http://nginx.org/en/docs/http/ngx_http_sub_module.html)**

> **[Module ngx\_http\_gzip\_module](http://nginx.org/en/docs/http/ngx_http_gzip_module.html)**

[http://nginx.org/en/docs/http/ngx\_http\_guzip\_module.html](http://nginx.org/en/docs/http/ngx_http_guzip_module.html)

> **[Module ngx\_http\_addition\_module](http://nginx.org/en/docs/http/ngx_http_addition_module.html)**

> Could you please let me know, how to add header  
> forcefully.  
> like add\_header ‘Content-Length’ $upstream\_http\_content\_length;
> 
> somehow,
> 
> add\_header ‘Content-Length’ 12312312
> 
> Static value is working fine. But at the moment, i replaced it from  
> upstream\_http\_content\_length

You shouldn’t try to add Content-Length manually. This is very  
likely to break things completely.

–  
Maxim D.

> **[nginx](http://nginx.org/)**
