← Back to Apache Course | Chapter 5: Performance | Lesson 4 of 4

Compression और mod_deflate

mod_deflate responses (HTML, CSS, JavaScript, JSON) को network पर भेजने से पहले compress करता है, ताकि browser एक छोटी file download करे और इसे locally decompress करे -- आमतौर पर text-based content के लिए एक बड़ा win।

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 होना चाहिए।

Warning: पहले से compressed formats (images, video, archives) को compress करना हर request पर essentially बिना किसी फायदे के CPU जलाता है -- DEFLATE को text-based content types तक restrict करें।

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

apacheconf
<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). #}

⚠️ This example can't run in the browser editor. Try it in your own local environment instead.

{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. images और archives समेत हर content type के लिए DEFLATE enable करना, size को meaningfully कम किए बिना CPU waste करते हुए।
  2. AddOutputFilterByType rules add करने से पहले mod_deflate को खुद enable करना भूल जाना, जिन पर वे depend करते हैं।
  3. यह test न करना कि compression actually active है (curl -H "Accept-Encoding: gzip" -I और Content-Encoding: gzip check करना), यह मान लेना कि config काम करता है सिर्फ इसलिए कि इसे add किया गया।
चैप्टर सारांश
  • KeepAlive और MPM (prefork/worker/event) का चुनाव दोनों control करते हैं कि Apache एक साथ कई connections कितनी efficiently handle करता है; event modern 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.