Nice programing

Apache gzip 압축을 사용하려면 어떻게해야합니까?

nicepro 2021. 1. 9. 11:37
반응형

Apache gzip 압축을 사용하려면 어떻게해야합니까?


내 사이트에서 gzip 압축을 사용할 수 없습니다.

최근 css-tricks.com에서 Chris Coyier 의이 비디오시청했습니다 . 비디오에서 그는 웹 사이트를 더 빠르게 실행하기 위해 gzip 압축을 활성화하는 방법에 대해 설명합니다.

그의 지시에 따라 html5boilerplate.com을 통해 github에 연결하고 .htaccess 파일에서 gzip 압축 코드를 복사하여 내 자신의 파일에 붙여넣고 내 사이트에 업로드했습니다.

gzipwtf.com을 통해 테스트했는데 작동하지 않는 것 같습니다. 누구든지 이것으로 나를 도울 수 있습니까?

내 .htaccess 파일은 다음과 같습니다.

# ----------------------------------------------------------------------
# Trim www
# ----------------------------------------------------------------------

RewriteEngine On
RewriteCond %{HTTP_HOST} !^orbitprint.com$ [NC]
RewriteRule ^(.*)$ http://orbitprint.com/$1 [L,R=301]

# ----------------------------------------------------------------------
# Gzip compression
# ----------------------------------------------------------------------

<IfModule mod_deflate.c>

  # Force deflate for mangled headers 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
  <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/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>

이 시도 :

####################
# GZIP COMPRESSION #
####################
SetOutputFilter DEFLATE
AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml application/x-javascript application/x-httpd-php
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip

아래 스 니펫과 같이 구현하는 것이 좋습니다.

아래 콘텐츠를 .htaccess파일 에 붙여 넣은 다음 Google PageSpeed , Pingdom 도구GTmetrics를 사용하여 성능을 확인하십시오 .

# Enable GZIP
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</ifmodule>

# Expires Headers - 2678400s = 31 days
<ifmodule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 1 seconds"
  ExpiresByType text/html "access plus 7200 seconds"
  ExpiresByType image/gif "access plus 2678400 seconds"
  ExpiresByType image/jpeg "access plus 2678400 seconds"
  ExpiresByType image/png "access plus 2678400 seconds"
  ExpiresByType text/css "access plus 518400 seconds"
  ExpiresByType text/javascript "access plus 2678400 seconds"
  ExpiresByType application/x-javascript "access plus 2678400 seconds"
</ifmodule>

# Cache Headers
<ifmodule mod_headers.c>
  # Cache specified files for 31 days
  <filesmatch "\.(ico|flv|jpg|jpeg|png|gif|css|swf)$">
  Header set Cache-Control "max-age=2678400, public"
  </filesmatch>
  # Cache HTML files for a couple hours
  <filesmatch "\.(html|htm)$">
  Header set Cache-Control "max-age=7200, private, must-revalidate"
  </filesmatch>
  # Cache PDFs for a day
  <filesmatch "\.(pdf)$">
  Header set Cache-Control "max-age=86400, public"
  </filesmatch>
  # Cache Javascripts for 31 days
  <filesmatch "\.(js)$">
  Header set Cache-Control "max-age=2678400, private"
  </filesmatch>
</ifmodule>

.htaccess가 제대로 실행되어야합니다. 4 개의 다른 Apache 모듈 (각 <IfModule>지시문 당 하나씩 ) 에 따라 다릅니다 . 다음 중 하나를 추측합니다.

  • Apache 서버에 mod_filter, mod_deflate, mod_headers 및 / 또는 mod_setenvif 모듈이 설치 및 실행되고 있지 않습니다. 서버 구성에 액세스 할 수 있으면 확인하십시오 /etc/apache2/httpd.conf(및 관련 Apache 구성 파일). 그렇지 않으면 phpinfo()apache2handler 섹션에서 를 통해로드 된 모듈을 확인할 수 있습니다 (첨부 된 이미지 참조). ( 편집 ) 또는 터미널 창을 열고 sudo apachectl -M로드 된 모듈을 나열하는 명령 실행할 수 있습니다 .

  • http 500 내부 서버 오류가 발생하면 서버에서 .htaccess 파일을 사용하지 못할 수 있습니다.

  • 자체 헤더를 전송하는 (Apache의 헤더를 덮어 쓰는) PHP 파일을로드하려고하므로 브라우저를 "혼란"하게됩니다.

어쨌든 서버 구성 오류 로그를 다시 확인하여 무엇이 잘못 되었는지 확인해야합니다 . 확실히하기 위해 여기 Apache 문서에서 제안 된 가장 빠른 방법을 사용하십시오 .

AddOutputFilterByType DEFLATE text/html text/plain text/xml

그런 다음 큰 텍스트 파일을로드하십시오 (가급적이면 먼저 캐시를 정리하십시오).

( 편집 ) 필요한 모듈이 (Apache 모듈 디렉토리에) 있지만로드되지 않은 경우 /etc/apache2/httpd.conf를 편집하고 모듈에 대해 LoadModule 지시문을 추가하십시오 .

필요한 모듈이없는 경우 (로드되지 않았거나 Apache 모듈 디렉토리에없는 경우) 유일한 옵션은 Apache (전체 버전)를 다시 설치하는 것입니다.

phpinfo () apache2handler 섹션


우선 apache / bin / conf / httpd.conf로 이동하여 mod_deflate.so가 활성화되어 있는지 확인하십시오.

Then go to the .htaccess file and add this line:

SetOutputFilter DEFLATE

This should output all the content served as gzipped, i have tried it and it works.


Ran into this problem using the same .htaccess configuration. I realized that my server was serving javascript files as text/javascript instead of application/javascript. Once I added text/javascript to the AddOutputFilterByType declaration, gzip started working.

As to why javascript was being served as text/javascript: there was an AddType 'text/javascript' js declaration at the top of my root .htaccess file. After removing it (it had been added in error), javascript starting serving as application/javascript.


In my case i have used following code for enabling gzip compression in apache web server.

  # Compress HTML File, CSS File, JavaScript File, Text File, XML File and Fonts
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE application/json
    AddOutputFilterByType DEFLATE application/x-httpd-php
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/x-javascript
    AddOutputFilterByType DEFLATE font/otf
    AddOutputFilterByType DEFLATE font/ttf

I have taken reference from http://www.tutsway.com/enable-gzip-compression-using-htacess.php.


If your Web Host is through C Panel Enable G ZIP Compression on Apache C Panel

Go to CPanel and check for software tab.

Previously Optimize website used to work but now a new option is available i.e "MultiPHP INI Editor".

Select the domain name you want to compress.

Scroll down to bottom until you find zip output compression and enable it.

Now check again for the G ZIP Compression.

You can follow the video tutorial also. https://www.youtube.com/watch?v=o0UDmcpGlZI


In my case append only this line worked

SetOutputFilter DEFLATE


Enable compression via .htaccess

이것을 읽는 대부분의 사람들은 웹 호스트 / 서버의 .htaccess라는 파일에 일부 코드를 추가하여 압축을 사용할 수 있습니다. 즉, 웹 호스트의 파일 관리자 (또는 파일을 추가하거나 업로드 할 위치)로 이동합니다.

.htaccess 파일은 사이트에 대한 많은 중요한 사항을 제어합니다.

.htaccess 파일에 아래 코드를 추가해야합니다.

<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

.htaccess 파일을 저장 한 다음 웹 페이지를 새로 고칩니다.

Gzip 압축 도구를 사용하여 압축이 작동하는지 확인합니다 .


<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

<IfModule mod_deflate.c>
# Insert filters
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/x-httpd-php
AddOutputFilterByType DEFLATE application/x-httpd-fastphp
AddOutputFilterByType DEFLATE image/svg+xml

# Drop problematic browsers
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

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

참조 URL : https://stackoverflow.com/questions/12367858/how-can-i-get-apache-gzip-compression-to-work

반응형