Apache output compression doesn't work when Content-Length is set










0















I have this minimal code that outputs some text:



<?php

$output = "";

for ($i = 0; $i < 7000; $i++)
$output .= ($i % 2) ? "Foo " : "Bar ";


header("Content-Length: ".strlen($output));
echo $output;

exit;


Using Apache 2.2.34 on my web server, and I'm unable to compress the output via .htaccess (see below).



But if I simply remove this header("Content-Length… line, suddenly the output is compressed as expected and the appropriate headers are sent (Content-Encoding: gzip, Vary: Accept-Encoding, Transfer-Encoding: chunked).



My .htaccess uses the AddOutputFilterByType and Filter* directives, I’ve tried both independently as well:



AddOutputFilterByType DEFLATE "text/html"

<IfModule filter_module>
FilterDeclare COMPRESS
FilterProvider COMPRESS DEFLATE Content-Type $text/html
FilterProvider COMPRESS DEFLATE resp=Content-Type /text/(css|javascript|plain|xml|x-component)/
FilterProvider COMPRESS DEFLATE resp=Content-Type /application/(javascript|json|xml|x-javascript)/
FilterChain COMPRESS
FilterProtocol COMPRESS change=yes;byteranges=no
</IfModule>


I'm not experiencing this issue in my local working environment, where I'm using Apache 2.4 (sadly I can't upgrade the server).



EDIT: In my case, the problem is solved, see my comments below.










share|improve this question
























  • [Self-answer as a comment, because it's not really an answer] I suspect this behaviour is deliberate, considering the Content-length header is supposed to indicate the compressed size, not the uncompressed size as is the case here.

    – Niavlys
    Dec 2 '18 at 15:14











  • The reason I started experiencing this issue is because I switched to Slim using PHP-DI's Slim-Bridge (github.com/PHP-DI/Slim-Bridge), and the default configuration sets the addContentLengthHeader to true, forcing Slim to add the Content-Length header to the uncompressed content before handing it to Apache. The fix was as simple as adding "settings.addContentLengthHeader" => false in my definitions array.

    – Niavlys
    Dec 2 '18 at 15:15















0















I have this minimal code that outputs some text:



<?php

$output = "";

for ($i = 0; $i < 7000; $i++)
$output .= ($i % 2) ? "Foo " : "Bar ";


header("Content-Length: ".strlen($output));
echo $output;

exit;


Using Apache 2.2.34 on my web server, and I'm unable to compress the output via .htaccess (see below).



But if I simply remove this header("Content-Length… line, suddenly the output is compressed as expected and the appropriate headers are sent (Content-Encoding: gzip, Vary: Accept-Encoding, Transfer-Encoding: chunked).



My .htaccess uses the AddOutputFilterByType and Filter* directives, I’ve tried both independently as well:



AddOutputFilterByType DEFLATE "text/html"

<IfModule filter_module>
FilterDeclare COMPRESS
FilterProvider COMPRESS DEFLATE Content-Type $text/html
FilterProvider COMPRESS DEFLATE resp=Content-Type /text/(css|javascript|plain|xml|x-component)/
FilterProvider COMPRESS DEFLATE resp=Content-Type /application/(javascript|json|xml|x-javascript)/
FilterChain COMPRESS
FilterProtocol COMPRESS change=yes;byteranges=no
</IfModule>


I'm not experiencing this issue in my local working environment, where I'm using Apache 2.4 (sadly I can't upgrade the server).



EDIT: In my case, the problem is solved, see my comments below.










share|improve this question
























  • [Self-answer as a comment, because it's not really an answer] I suspect this behaviour is deliberate, considering the Content-length header is supposed to indicate the compressed size, not the uncompressed size as is the case here.

    – Niavlys
    Dec 2 '18 at 15:14











  • The reason I started experiencing this issue is because I switched to Slim using PHP-DI's Slim-Bridge (github.com/PHP-DI/Slim-Bridge), and the default configuration sets the addContentLengthHeader to true, forcing Slim to add the Content-Length header to the uncompressed content before handing it to Apache. The fix was as simple as adding "settings.addContentLengthHeader" => false in my definitions array.

    – Niavlys
    Dec 2 '18 at 15:15













0












0








0








I have this minimal code that outputs some text:



<?php

$output = "";

for ($i = 0; $i < 7000; $i++)
$output .= ($i % 2) ? "Foo " : "Bar ";


header("Content-Length: ".strlen($output));
echo $output;

exit;


Using Apache 2.2.34 on my web server, and I'm unable to compress the output via .htaccess (see below).



But if I simply remove this header("Content-Length… line, suddenly the output is compressed as expected and the appropriate headers are sent (Content-Encoding: gzip, Vary: Accept-Encoding, Transfer-Encoding: chunked).



My .htaccess uses the AddOutputFilterByType and Filter* directives, I’ve tried both independently as well:



AddOutputFilterByType DEFLATE "text/html"

<IfModule filter_module>
FilterDeclare COMPRESS
FilterProvider COMPRESS DEFLATE Content-Type $text/html
FilterProvider COMPRESS DEFLATE resp=Content-Type /text/(css|javascript|plain|xml|x-component)/
FilterProvider COMPRESS DEFLATE resp=Content-Type /application/(javascript|json|xml|x-javascript)/
FilterChain COMPRESS
FilterProtocol COMPRESS change=yes;byteranges=no
</IfModule>


I'm not experiencing this issue in my local working environment, where I'm using Apache 2.4 (sadly I can't upgrade the server).



EDIT: In my case, the problem is solved, see my comments below.










share|improve this question
















I have this minimal code that outputs some text:



<?php

$output = "";

for ($i = 0; $i < 7000; $i++)
$output .= ($i % 2) ? "Foo " : "Bar ";


header("Content-Length: ".strlen($output));
echo $output;

exit;


Using Apache 2.2.34 on my web server, and I'm unable to compress the output via .htaccess (see below).



But if I simply remove this header("Content-Length… line, suddenly the output is compressed as expected and the appropriate headers are sent (Content-Encoding: gzip, Vary: Accept-Encoding, Transfer-Encoding: chunked).



My .htaccess uses the AddOutputFilterByType and Filter* directives, I’ve tried both independently as well:



AddOutputFilterByType DEFLATE "text/html"

<IfModule filter_module>
FilterDeclare COMPRESS
FilterProvider COMPRESS DEFLATE Content-Type $text/html
FilterProvider COMPRESS DEFLATE resp=Content-Type /text/(css|javascript|plain|xml|x-component)/
FilterProvider COMPRESS DEFLATE resp=Content-Type /application/(javascript|json|xml|x-javascript)/
FilterChain COMPRESS
FilterProtocol COMPRESS change=yes;byteranges=no
</IfModule>


I'm not experiencing this issue in my local working environment, where I'm using Apache 2.4 (sadly I can't upgrade the server).



EDIT: In my case, the problem is solved, see my comments below.







php apache .htaccess http-headers http-compression






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 2 '18 at 15:18







Niavlys

















asked Nov 15 '18 at 7:35









NiavlysNiavlys

9251716




9251716












  • [Self-answer as a comment, because it's not really an answer] I suspect this behaviour is deliberate, considering the Content-length header is supposed to indicate the compressed size, not the uncompressed size as is the case here.

    – Niavlys
    Dec 2 '18 at 15:14











  • The reason I started experiencing this issue is because I switched to Slim using PHP-DI's Slim-Bridge (github.com/PHP-DI/Slim-Bridge), and the default configuration sets the addContentLengthHeader to true, forcing Slim to add the Content-Length header to the uncompressed content before handing it to Apache. The fix was as simple as adding "settings.addContentLengthHeader" => false in my definitions array.

    – Niavlys
    Dec 2 '18 at 15:15

















  • [Self-answer as a comment, because it's not really an answer] I suspect this behaviour is deliberate, considering the Content-length header is supposed to indicate the compressed size, not the uncompressed size as is the case here.

    – Niavlys
    Dec 2 '18 at 15:14











  • The reason I started experiencing this issue is because I switched to Slim using PHP-DI's Slim-Bridge (github.com/PHP-DI/Slim-Bridge), and the default configuration sets the addContentLengthHeader to true, forcing Slim to add the Content-Length header to the uncompressed content before handing it to Apache. The fix was as simple as adding "settings.addContentLengthHeader" => false in my definitions array.

    – Niavlys
    Dec 2 '18 at 15:15
















[Self-answer as a comment, because it's not really an answer] I suspect this behaviour is deliberate, considering the Content-length header is supposed to indicate the compressed size, not the uncompressed size as is the case here.

– Niavlys
Dec 2 '18 at 15:14





[Self-answer as a comment, because it's not really an answer] I suspect this behaviour is deliberate, considering the Content-length header is supposed to indicate the compressed size, not the uncompressed size as is the case here.

– Niavlys
Dec 2 '18 at 15:14













The reason I started experiencing this issue is because I switched to Slim using PHP-DI's Slim-Bridge (github.com/PHP-DI/Slim-Bridge), and the default configuration sets the addContentLengthHeader to true, forcing Slim to add the Content-Length header to the uncompressed content before handing it to Apache. The fix was as simple as adding "settings.addContentLengthHeader" => false in my definitions array.

– Niavlys
Dec 2 '18 at 15:15





The reason I started experiencing this issue is because I switched to Slim using PHP-DI's Slim-Bridge (github.com/PHP-DI/Slim-Bridge), and the default configuration sets the addContentLengthHeader to true, forcing Slim to add the Content-Length header to the uncompressed content before handing it to Apache. The fix was as simple as adding "settings.addContentLengthHeader" => false in my definitions array.

– Niavlys
Dec 2 '18 at 15:15












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53314451%2fapache-output-compression-doesnt-work-when-content-length-is-set%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53314451%2fapache-output-compression-doesnt-work-when-content-length-is-set%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto