Làm cách nào để kích hoạt nén Gzip hoặc Deflate qua .htaccess?


22

Làm cách nào để kích hoạt tính năng nén Gzip hoặc Deflate thông qua .htaccess và cái nào là tốt nhất trong những ngày này? Mã ví dụ cần thiết.

Câu trả lời:


15

HTML5 Boilerplate ( http://html5boilerplate.com ) cung cấp những gì có vẻ là cài đặt giải pháp tốt nhất và hiệu quả nhất cùng với nhiều thứ khác như bộ nhớ đệm, các loại mime, v.v ... Rất khuyến khích.

<IfModule mod_deflate.c>

# Force compression for mangled headers.
# http://developer.yahoo.com/blogs/ydn/posts/2010/12/pushing-beyond-gzipping
<IfModule mod_setenvif.c>
<IfModule mod_headers.c>
SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
</IfModule>
</IfModule>


# Compress all output labeled with one of the following MIME-types
# (for Apache versions below 2.3.7, you don't need to enable `mod_filter`
#  and can remove the `<IfModule mod_filter.c>` and `</IfModule>` lines
#  as `AddOutputFilterByType` is still in the core directives).

<IfModule mod_filter.c>
AddOutputFilterByType DEFLATE application/atom+xml \
application/javascript \
application/json \
application/rss+xml \
application/vnd.ms-fontobject \
application/x-font-ttf \
application/x-web-app-manifest+json \
application/xhtml+xml \
application/xml \
font/opentype \
image/svg+xml \
image/x-icon \
text/css \
text/html \
text/plain \
text/x-component \
text/xml
</IfModule>

</IfModule>

EDIT: Vì câu hỏi và câu trả lời này tiếp tục được nâng cấp sau vài năm, tôi đang đặt liên kết cấu hình máy chủ H5BP để tối ưu hóa hoàn chỉnh hơn .

EDIT: liên kết cố định đến https://github.com/h5bp/server-configs-apache


12

Xem tài liệu về mod_deflate của Apache , cụ thể là ví dụ " nén mọi thứ trừ hình ảnh ". Nó đã làm việc tốt với tôi và sẽ được đưa vào một .htaccesstập tin như sau:

<IfModule mod_deflate.c>
        # Insert filter
        SetOutputFilter DEFLATE

        # Netscape 4.x has some problems...
        BrowserMatch ^Mozilla/4 gzip-only-text/html

        # Netscape 4.06-4.08 have some more problems
        BrowserMatch ^Mozilla/4\.0[678] no-gzip

        # MSIE masquerades as Netscape, but it is fine
        # BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

        # NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
        # the above regex won't work. You can use the following
        # workaround to get the desired effect:
        BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

        # Don't compress images
        SetEnvIfNoCase Request_URI \
        \.(?:gif|jpe?g|png)$ no-gzip dont-vary

        # Make sure proxies don't deliver the wrong content
        Header append Vary User-Agent env=!dont-vary
</IfModule>

Và, tất nhiên, đảm bảo rằng bạn có các mục sau trong httpd.conftệp của mình để bật mod_deflate:

LoadModule deflate_module libexec/apache2/mod_deflate.so

9

Tôi đã kích hoạt giảm phát trên các tài sản tĩnh trên trang web của mình (theo loại MIME) bằng cách sử dụng phần sau được thêm vào .htaccesstệp ở thư mục gốc của tôi public_html:

<ifModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/xml text/css text/plain
  AddOutputFilterByType DEFLATE text/javascript application/javascript application/x-javascript application/json
</ifModule>

Bạn cũng có thể kích hoạt nó bằng phần mở rộng tập tin, mặc dù tôi không có cú pháp cho tiện dụng đó.

Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.