Sua Loi Blogger Blade1407

How to enable gzip compression in Xampp Server, Cách bật nén Gzip cho server Apache – tăng tốc website của bạn

Website  chậm do không bật cache và bật nén Gzip trên server, hôm nay mình hướng dẫn bật nén
Gzip cho con VPS chạy hệ điều hành CentOS nên share thông tin lên cho các bạn tham khảo.
Đầu tiên các bạn cần vào file này: /etc/httpd/conf/httpd.conf và kiểm tra dòng:

LoadModule deflate_module modules/mod_deflate.so

Nếu có dấu # ở đầu thì bỏ đi nhé, để kích hoạt module deflate đã có sẵn trong gói Apache rồi !
Tiếp theo, chúng ta tìm tới dòng gần cuối thêm đoạn này vào :

<Directory /var/www/html/>
<IfModule mod_mime.c>
AddType application/x-javascript .js
AddType text/css .css
</IfModule>
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/css application/x-javascript text/x-component text/html text/plain text/xml application/javascript
<IfModule mod_setenvif.c>
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch bMSIE !no-gzip !gzip-only-text/html
</IfModule>
</IfModule>
Header append Vary User-Agent env=!dont-vary
</Directory>

Đây là đoạn code để nén toàn bộ các định dạng css, html … trước khi gửi tới trình duyệt !
Tiếp nữa, trong file HTACCESS ta cho thêm đoạn code này vào cuối:

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

Vậy là xong, ta kiểm tra website đã nén hay chưa qua đường dẫn này: http://www.gidnetwork.com/tools/gzip-test.php

* Lưu ýMod_gzip dành cho các phiên bản cũ của Apache, nếu server của bạn đang dùng các phiên bản cũ thì dùng MOD này nhé !

Trong trường hợp bạn dùng xampp thì đây là giải pháp : 
Bạn hãy tìm thêm trong httpd.conf ( VD: C:\xampp\apache\conf\httpd.conf ) 2 dòng này và bỏ dấu #  nếu có:

LoadModule headers_module modules/mod_deflate.so ##(có thể có hoặc không)
LoadModule filter_module modules/mod_filter.so
Thêm vào cuối file này :
SetOutputFilter DEFLATE 

<Directory "C:/your-server-root/manual">  #any path to which you wish to apply gzip compression to!
    <IfModule mod_deflate.c>
        AddOutputFilterByType DEFLATE text/html  # or any file type you wish
    </IfModule>
</Directory> 
Sau đó thêm đoạn mã vào file HTACCESS  như CentOS
Chúc các bạn thành công!

=============================================
UPDATE cho xampp:

How to enable gzip compression in Xampp Server

How to enable gzip compression in Xampp Server

Tired of seeing a different file size of your JavaScript, HTML and CSS in your local development environment to the production server? You may consider to enable the Gzip compression (that should be available and enabled on every webserver) in Xampp to, so you won't have headaches when you think about the real size of your applications.

In this article we'll show you how to enable the gzip compression in your local Xampp server.

Before continue
To see with your own eyes the change of compression, we recommend you to take a screenshot of the network tab of your browser to see the total size of your assets. In our example, the network screenshot of chrome is the following:

Cach bat nen Gzip cho server Apache – tang toc website cua ban


318 KB were downloaded without Gzip compression. Having said that, let's get started !

1. Modify php.ini

The first you need to do is to enable the compression feature of zlib. The zlib extension offers the option to transparently compress your pages on-the-fly, if the requesting browser supports this.

Open your php.ini file (xampp/php/php.ini) and proceed to change the value of the zlib.output_compression option. This property is probably set to Off, so be sure to set its value to On to make the gzip compression available:

; Transparent output compression using the zlib library
; Valid values for this option are 'off', 'on', or a specific buffer size
; to be used for compression (default is 4KB)
; Note: Resulting chunk size may vary due to nature of compression. PHP
;   outputs chunks that are few hundreds bytes each as a result of
;   compression. If you prefer a larger chunk size for better
;   performance, enable output_buffering in addition.
; Note: You need to use zlib.output_handler instead of the standard
;   output_handler, or otherwise the output will be corrupted.
; http://php.net/zlib.output-compression
zlib.output_compression=On

Save changes and proceed to modify the httpd.conf file.

2. Modify httpd.conf file

Apache HTTP Server is configured by placing directives in plain text configuration files. The main configuration file is usually called httpd.conf. The location of this file is usually at /xampp/apache/conf/httpd.conf, so open it and uncomment the mod_deflate.so and mod_filter.so extensions by removing any # at the beginning of the line:
# Enable deflate and filter with the following pattern
# LoadModule <action> <file>

# Note that according to your OS, the <action> may change
# so be sure that modules/mod_deflate.so and modules/mod_filter.so
# are uncommented !
LoadModule deflate_module modules/mod_deflate.so
LoadModule filter_module modules/mod_filter.so
Once these lines are uncommented, you will now need to add the following block to the end of the httpd.conf file. This block specifies which directories of xampp should be served using the gzip compression. Tipically, you should apply that to all the htdocs folder content, however it's up to you:
SetOutputFilter DEFLATE 

# Set the path to the directory where you wish to apply the gzip compression
# Usually you would like to apply that to the entire htdocs folder, but you can change it
<Directory "C:/xampp/htdocs">  
    <IfModule mod_deflate.c>
        # Add any file type you want
        AddOutputFilterByType DEFLATE text/html
    </IfModule>
</Directory> 
Save changes and restart all the services with the Control Panel of Xampp and access your projects in the browser. Now you should see a decrease in the size of every file (you can compare them with the initial screenshot):

Now the total size decreased from 318KB to only 112 KB.

Happy coding !
Chia sẻ:

Viết 1 bình luận:

0 comments so far,add yours