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?

2
  • I try on Apache and I have the same problem. Try with wget -S
    – Dom
    Sep 17, 2014 at 6:56
  • @TRiG: The problem is obvious. Just replace <? with ?>
    – Sven
    Sep 17, 2014 at 16:54

2 Answers 2

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.

2
  • 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, 2014 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. Sep 18, 2014 at 6: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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .