2011-09-22 2 views
11

애셋 : 사전 컴파일 레이크 작업을 실행할 때 gzipped 버전의 앱 애셋이 생성됩니다. 자산 파이프 라인에 대한 레일스 가이드에 따르면, 웹 서버 (나의 경우 아파치 2.2)가 웹 서버에 작업을시키는 대신 미리 압축 된 파일을 제공하도록 설정할 수있다.애셋으로 준비된 gzip 된 애셋을 처리하기 위해 mod_deflate를 구성하는 방법 : 사전 컴파일

내가 알아낼 수없는 것은 mod_deflate를 어떻게 구성하여 두 파일을 압축하여 서비스하는 대신에 제공 되는가하는 것입니다.

AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript 
BrowserMatch ^Mozilla/4 gzip-only-text/html 
BrowserMatch ^Mozilla/4\.0[678] no-gzip 
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html 

내가 레일의 코드를 변환 한 공공/자산 htaccess로 들어가 안내 :

나는 mod_deflate가이 값을 httpd.conf를 통해 활성화

# Some browsers still send conditional-GET requests if there's a 
# Last-Modified header or an ETag header even if they haven't 
# reached the expiry date sent in the Expires header. 

Header unset Last-Modified 
Header unset ETag 
FileETag None 

# RFC says only cache for 1 year 

ExpiresActive On 
ExpiresDefault "access plus 1 year" 

# Serve gzipped versions instead of requiring Apache to do the work 

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME}.gz -s 
RewriteRule ^(.+) $1.gz [L] 

# without it, Content-Type will be "application/x-gzip" 

<FilesMatch .*\.css.gz> 
    ForceType text/css 
</FilesMatch> 

<FilesMatch .*\.js.gz> 
    ForceType text/javascript 
</FilesMatch> 

하나를 아이디어를 올바르게 설정하는 방법?

답변

24

먼저 mod_deflate가 여기에서 작동하는 것을 원하지 않습니다. 따라서 자산 .htaccess 파일에 다음을 추가하십시오.

SetEnv no-gzip 

자산에 대해 mod_deflate를 해제해야합니다.

둘째로, 나는 레일 사람들과 의견이 다르긴하지만, 자산에 몇 가지 결함이 있다고 생각합니다 .htaccess 제조법. 상단 부분이다 RewriteEngine과 내가 가진 것 이상하지만 괜찮 :

RewriteEngine on 
# Make sure the browser supports gzip encoding before we send it 
RewriteCond %{HTTP:Accept-Encoding} \b(x-)?gzip\b 
RewriteCond %{REQUEST_URI} .*\.(css|js) 
RewriteCond %{REQUEST_FILENAME}.gz -s 
RewriteRule ^(.+) $1.gz [L] 

# without it, Content-Type will be "application/x-gzip" 
# also add a content-encoding header to tell the browser to decompress 

<FilesMatch \.css\.gz$> 
    ForceType text/css 
    Header set Content-Encoding gzip 
</FilesMatch> 

<FilesMatch \.js\.gz$> 
    ForceType application/javascript 
    Header set Content-Encoding gzip 
</FilesMatch> 
+1

작은 주석 -이 htaccess로에없는 경우, 그렇지 않으면 -s가 작동하지 않습니다, 디렉토리 섹션에 있어야합니다. – lzap

+3

또한 'text/javascript'대신'application/javascript'를 사용하는 것이 좋습니다. * 스크립팅 미디어 유형 *에 대한 [RFC4329] (https://tools.ietf.org/html/rfc4329)를 참조하십시오. – tne

+0

해야 할 일 \t AddEncoding gzip .gz 그렇지 않으면 – Torsten

관련 문제