The Content-Length header does not exist
Solution 1
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.
Solution 2
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.
Related videos on Youtube
user3393523
Updated on September 18, 2022Comments
-
user3393523 over 1 year
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?
-
Dom over 9 yearsI try on Apache and I have the same problem. Try with
wget -S
-
FooBee over 9 years@TRiG: The problem is obvious. Just replace
<?
with?>
-
-
Alvin Wong over 9 yearsiirc if you know the output length, e.g. a file download, you can set the content-length header with PHP
header
function. -
user3393523 over 9 years@SvW, Thank you for your answer. But, the same thing is working on Apache server.I mean Apache server is sending the
Content-Length
.