Does Apache 2 gzip json output by default?

前端 未结 2 1971
日久生厌
日久生厌 2021-02-19 11:04

I\'m using this PHP code for json output. Does apache gzip it by default? Or, how can I check to make sure?

header(\'Content-type: application/json\');
header(\'         


        
相关标签:
2条回答
  • 2021-02-19 11:25

    This is what did the trick for me:

    (assuming you have access to apache configuration)

    AddOutputFilterByType DEFLATE application/json
    

    I added this line directly in /etc/apache2/mods-available/deflate.conf (so it will work for every json file on the webserver) maybe someplace else is better suited in your use case (e.g. if you only want to enable json compression for a single web application and not by default).

    edit: In google chromes developer tools you can easily check if your content is served compressed or uncompressed: https://webmasters.stackexchange.com/a/4613

    0 讨论(0)
  • 2021-02-19 11:27

    No gzip is normally not used you have to enforce that yourself.

    In the simplest case you just need to add this php line:

    ob_start("ob_gzhandler");
    

    See also the official php documentation.

    0 讨论(0)
提交回复
热议问题