Mod-Rewrite

Apache 使用 Rails Asset Pipeline 生成的資產提供空 gzip

  • July 5, 2012

我按照博文 The Asset Pipeline 中描述的步驟,從開發到生產,並將它們調整到我的環境。兩個重要的文件是:

/etc/apache/site-available/example.com

<VirtualHost *:80>
   ServerName example.com
   ServerAlias www.example.com
   DocumentRoot "/var/www/sites/example.com/current/public"
   ErrorLog "/var/log/apache2/example.com-error_log"
   CustomLog "/var/log/apache2/example.com-access_log" common
   <Directory "/var/www/sites/example.com/current/public">
       Options All
       AllowOverride All
       Order allow,deny
       Allow from all
   </Directory>
   <Directory "/var/www/sites/example.com/current/public/assets">
       AllowOverride All
   </Directory>
   <LocationMatch "^/assets/.*$">
       Header unset Last-Modified
       Header unset ETag
       FileETag none
       ExpiresActive On
       ExpiresDefault "access plus 1 year"
   </LocationMatch>
   RewriteEngine On
   # Remove the www
   RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
   RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
</VirtualHost>

/var/www/sites/example.com/shared/assets/.htaccess

RewriteEngine on
RewriteCond %{HTTP:Accept-Encoding} \b(x-)?gzip\b
RewriteCond %{REQUEST_FILENAME}.gz -s
RewriteRule ^(.+) $1.gz [L]

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

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

但是 apache 似乎發送了空的 gzip 文件,因為測試站點失去了所有樣式,並且 firebug 沒有找到 css 文件的任何內容。如果我直接呼叫資產路徑,我會得到一些看起來像二進制數據的亂碼。如果我移動 htaccess 文件,一切都會恢復正常。我怎麼能找出哪裡/哪裡出了問題,或者你有什麼建議我犯了什麼錯誤?

> apache2 -v
System: Server version: Apache/2.2.14 (Ubuntu)
Server built:   Mar  5 2012 16:42:17
> uname -a
Linux node0 2.6.18-028stab094.3 #1 SMP Thu Sep 22 12:47:37 MSD 2011 x86_64 GNU/Linux

更改編碼:

<IfModule mod_mime.c>
AddEncoding gzip .gz
</IfModule>

請檢查此部落格條目以獲取更詳細的說明。

引用自:https://serverfault.com/questions/403913