2013-10-08 9 views
10

Sto usando questo codice PHP per l'output di json. Apache lo gzip di default? Oppure, come posso verificarlo?Apache 2 gzip json è in uscita di default?

header('Content-type: application/json'); 
header('Cache-Control: max-age=0,no-cache,no-store,post-check=0,pre-check=0'); 
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); 
echo json_encode($response); 
+2

Credo che questo è quello che stai cercando. http://www.simonwhatley.co.uk/how-to-configure-apache-to-gzip-your-components –

risposta

2

No gzip non è normalmente utilizzato è necessario imporlo da soli.

Nel caso più semplice è sufficiente aggiungere questa riga php:

ob_start("ob_gzhandler"); 

Vedere anche il funzionario php documentation.

20

Questo è ciò che ha fatto il trucco per me:

(ammesso che abbiate accesso alla configurazione di Apache)

AddOutputFilterByType DEFLATE application/json 

Ho aggiunto questa linea direttamente a /etc/apache2/mods-available/deflate.conf (in modo da funzionare per ogni file JSON su il webserver) forse un altro posto è più adatto nel tuo caso d'uso (es. se vuoi abilitare solo la compressione json per una singola applicazione web e non per impostazione predefinita).

edit: In Google Chromes strumenti di sviluppo si può facilmente verificare se il contenuto è servita compresso o non compresso: https://webmasters.stackexchange.com/a/4613