The Content-Length header does not exist

I have nginx server installed on Linux. When I send a request with curl, the Content-Length header is missing from the response.

The 1.php file is:

<?php
   echo "hello";
?>

The example request is:

curl api.mysite.com/taxi/1.php -i

HTTP/1.1 200 OK
Server: nginx/1.2.1
Date: Wed, 17 Sep 2014 06:16:00 GMT
Content-Type: text/html; charset=utf8
Vary: Accept-Encoding
X-Powered-By: PHP/5.4.4-14+deb7u14
Age: 0
X-Cache: MISS from cache.turonnet.uz
Transfer-Encoding: chunked
Connection: keep-alive

How can I fix that?


content-length can't be set if the Transfer-Encoding is set to be chunked. At the time of sending the headers, the server is unaware of how much data it will finally send. Each chunk has it's own length header field (see the RFC).

If you think about it, unlike with a static HTML file, the web server has no way of knowing how much data will be generated by a PHP script. It could either cache the generated file and send it after the script is finished or sent it out in chunks while it is generated. The latter is preferred especially for scripts with large output and a long run time.


Nginx does not know the length because php is generating dynamic content. You could first write to the php output buffer and then set the header field manually before flushing the buffer.