8

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?

Falcon Momot
  • 25,244
  • 15
  • 63
  • 92
user3393523
  • 91
  • 1
  • 1
  • 4

2 Answers2

20

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.

Sven
  • 98,649
  • 14
  • 180
  • 226
  • 1
    iirc if you know the output length, e.g. a file download, you can set the content-length header with PHP `header` function. – Alvin Wong Sep 17 '14 at 16:49
  • @SvW, Thank you for your answer. But, the same thing is working on Apache server.I mean Apache server is sending the `Content-Length`. – user3393523 Sep 18 '14 at 06:11
1

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.

user242786
  • 11
  • 1