tail -f /dev/null

If you haven't had any obstacles lately, you're not challenging. be the worst.

独自 Directive を IfDefine で定義して Rewrite を切り替える

通常は Sorry 画面を表示しておき、必要なタイミングで Apache の起動オプションを利用して Maintenance 画面へ切り替えたい。

IfDefine を利用した Directive 適用範囲の切替

IfDefine Directive を利用すれば、httpd で起動する際にオプションの -D <独自 Directive> で指定した IfDefine のセクション範囲を適用出来る。

次の例だと、 <IfDefine !M> が通常の Directive 適用範囲となり、幾つかの URI を除き全リクエストは Document 503 へ Query String 無しでリダイレクトされる。

    <IfDefine !M>
        RewriteEngine On
        ErrorDocument 503 /sorry.html
        RewriteCond %{REQUEST_URI} !=/sorry.html [NC]
        RewriteCond %{REQUEST_URI} !=/maintenance.html [NC]
        RewriteCond %{REQUEST_URI} !^(.*)\.(png|gif|jpg|jpeg|ico|css)$ [NC]
        # Redirect to sorry page and delete all query strings
        RewriteRule ^ - [R=503,L,QSD]
    </IfDefine>

    <IfDefine M>
        RewriteEngine On
        ErrorDocument 503 /maintenance.html
        RewriteCond %{REQUEST_URI} !=/sorry.html [NC]
        RewriteCond %{REQUEST_URI} !=/maintenance.html
        RewriteCond %{REQUEST_URI} !^(.*)\.(png|gif|jpg|jpeg|ico|css)$ [NC]
        # Redirect to maintenance page and delete all query strings
        RewriteRule ^ - [R=503,L,QSD]
    </IfDefine>

これを -D<独自 Directive> start で起動すると <IfDefine M> の範囲が適用される。

httpd -h
Usage: httpd [-D name] [-d directory] [-f file]
             [-C "directive"] [-c "directive"]
             [-k start|restart|graceful|graceful-stop|stop]
             [-v] [-V] [-h] [-l] [-L] [-t] [-T] [-S] [-X]
Options:
  -D name            : define a name for use in <IfDefine name> directives
sudo httpd -k stop
sudo httpd -DM -k start

※ 因みに試した限りでは graceful, restart では切り替わらなかった。

systemd を利用している場合

systemd の場合 /usr/lib/systemd/system/httpd.service の Service ファイルにて ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND が定義されている。

Directive を切り替える時のみ起動時のオプションを利用したい場合は、 httpd の環境変数定義 (/etc/sysconfig/httpd) にある OPTIONS か、systemd の ExecStart= 部分を書き換えるしか無さそう。

Apache の SetEnv Directive やシンプルに export OPTIONS=-D 等で環境変数を定義出来ないかと思ったが、うまくいかなかった。イケてるやり方を見つけたい。

References