Compression और mod_deflate
mod_deflate responses (HTML, CSS, JavaScript, JSON) को network पर भेजने से पहले compress करता है, ताकि browser एक छोटी file download करे और इसे locally decompress करे -- आमतौर पर text-based content के लिए एक बड़ा win।In this page:
Compression Bandwidth कैसे बचाता है
Text-based formats -- HTML, CSS, JavaScript, JSON, plain text -- बहुत अच्छे से compress होते हैं, अक्सर अपने original size के एक हिस्से तक सिकुड़ते हुए, क्योंकि इनमें repeated characters और patterns भरे होते हैं। browser यह कहने के लिए Accept-Encoding: gzip भेजता है कि यह compressed responses handle कर सकता है, और Apache compressed body के साथ एक Content-Encoding: gzip header से reply करता है।
mod_deflate Enable करना
Ubuntu/Debian पर: sudo a2enmod deflate फिर reload। एक बार enable होने के बाद, AddOutputFilterByType DEFLATE text/html text/css application/javascript Apache को बताता है कि कौन से content types compress करने हैं -- यह MIME type से apply होता है, file extension से नहीं।
पहले से Compressed Files को Compress न करें
JPEG, PNG, MP4, और ZIP files internally पहले से compressed हैं, इसलिए इन्हें दोबारा mod_deflate से गुज़ारना बहुत कम या बिना size reduction के CPU waste करता है -- कभी-कभी "compressed" output original से थोड़ा बड़ा भी हो जाता है। Compression सिर्फ text-based MIME types तक scoped होना चाहिए।
mod_deflate बनाम Brotli
Gzip (mod_deflate के जरिए) universally supported है और configure करना simple है। Brotli, अलग mod_brotli module से supported, same speed पर आमतौर पर gzip से थोड़ा बेहतर compress करता है, और हर modern browser इसे support करता है -- basic gzip compression पहले से हो जाने पर एक आगे के, incremental improvement के लिए consider करने लायक।
उदाहरण: Enabling gzip compression for text content
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/css
AddOutputFilterByType DEFLATE application/javascript application/json
</IfModule>
{# Flagged by hand after confirming a runner can't handle this example
(a shell command / go.mod file stored as a TopicExample, a language
feature the configured runner version doesn't support, or output
that blows a runner's sandbox limit) -- see TopicExample.norun.
Never render the run button for these, regardless of language,
since it would just fail at execute_code (or worse, hang the
Judge0 queue on a submission that can never finish cleanly). #}
- images और archives समेत हर content type के लिए DEFLATE enable करना, size को meaningfully कम किए बिना CPU waste करते हुए।
- AddOutputFilterByType rules add करने से पहले mod_deflate को खुद enable करना भूल जाना, जिन पर वे depend करते हैं।
- यह test न करना कि compression actually active है (
curl -H "Accept-Encoding: gzip" -IऔरContent-Encoding: gzipcheck करना), यह मान लेना कि config काम करता है सिर्फ इसलिए कि इसे add किया गया।
- KeepAlive और MPM (prefork/worker/event) का चुनाव दोनों control करते हैं कि Apache एक साथ कई connections कितनी efficiently handle करता है;
eventmodern default है जब तक कोई non-thread-safe module prefork पर force न करे। - Caching (Cache-Control/Expires, ETag validators) और compression (text content के लिए mod_deflate) दोनों network पर move होने वाला data कम करते हैं, और दोनों को carefully scope करने की ज़रूरत है -- caching सिर्फ वही जो reuse करना safe है, compression सिर्फ text-based formats।
Chapter Quiz — Complete all 4 topics to unlock
0/4 topics done
Complete these topics first: