LoginSignup
4
4

More than 5 years have passed since last update.

mod_extfilterとmod_deflateを併用してハマった話

Last updated at Posted at 2014-05-14

一言でいうなら、SetOutputFilterを書く順番には気を付けましょう。で終わってしまう話。
ちょっとハマったので、調べたついでにメモしておく。

やりたかったこと

mod_extfilterでレスポンスを書き換えて、mod_deflateで圧縮して返したい。

やったこと

confファイルに以下のように書いた。fooをvarに置換して返すフィルタを定義。

ExtFilterDefine testfilter mode=output intype=text/html cmd="/bin/sed s/foo/var/"

<IfModule mod_deflate.c>
  DeflateCompressionLevel 9
  <Location />
    SetOutputFilter DEFLATE
    SetEnvIfNoCase Request_URI \
              \.(?:gif|jpe?g|png)$ no-gzip dont-vary
    Header append Vary User-Agent env=!dont-vary
  </Location>
</IfModule>

Listen 4040
<VirtualHost *:4040>
  DocumentRoot /var/www/html

  <Directory "/var/www/html">
    SetOutputFilter testfilter
    ....
  </Directory>
</virtualHost>

以下のテキトーなファイルを置いて試してみる

cat /var/www/html/hoge.html

<html>
<head>
<title>てすと</title>
</head>
<body>
foo
</body>
</html>

curlを投げて確認したところ・・・あれ?置換されてないぞ

curl http://localhost:4040/hoge.html -H "Accept-Encoding: gzip" | gunzip

<html>
<head>
<title>てすと</title>
</head>
<body>
foo
</body>
</html>

解決方法

SetOutputFilterを書く場所が悪く、先にDEFLATEで圧縮されたため、extfilterで置換がかからなかった。
このように書けば期待通りになった。

ExtFilterDefine testfilter mode=output intype=text/html cmd="/bin/sed s/foo/var/"

<IfModule mod_deflate.c>
  DeflateCompressionLevel 9
  <Location />
#    SetOutputFilter DEFLATE   <=ここの行が不要で(丸コピで気にもしてなかった...)
    SetEnvIfNoCase Request_URI \
              \.(?:gif|jpe?g|png)$ no-gzip dont-vary
    Header append Vary User-Agent env=!dont-vary
  </Location>
</IfModule>

Listen 4040
<VirtualHost *:4040>
  DocumentRoot /var/www/html

  <Directory "/var/www/html">
    SetOutputFilter testfilter;DEFLATE   <= ここにDEFLATEを追記する。フィルタの順番に注意
    ....
  </Directory>
</virtualHost>

確認してみると・・・期待通り!

curl http://localhost:4040/hoge.html -H "Accept-Encoding: gzip" | gunzip

<html>
<head>
<title>てすと</title>
</head>
<body>
var
</body>
</html>
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4