事前確認事項

インストール

aptitude install apache2-mpm-xxxx

動作を軽くする為に不要なモジュールは無効にする

以下は有効にしているもの
alias
authz_host
deflate
dir
env
filter
headers
mime
negotiation
php5
rewrite
setenvif
ssl

設定箇所

Load Balanerとして動かす

まずproxy, proxy_http, proxy_balanerをロードする

a2enmod proxy proxy_http proxy_balaner

次に以下のファイルを編集
/etc/apache2/mods-available/proxy.conf

       <Proxy *>
               AddDefaultCharset off
               Order deny,allow
               Deny from all
               Allow from all #<--検証期間中などはIPなどでもOk
               #Allow from .example.com
       </Proxy>
/etc/init.d/apache2 restart

vhost設定を作成
/etc/apache2/sites-available/funny-lb

<VirtualHost *:80>
       ServerName funny.example.jp:80
       ProxyRequests off

       ProxyPass / balancer://funny/
       ProxyPassReverse / balancer://funny/

       <Proxy balancer://funny/>
              #web server 01
              BalancerMember http://web01.private/ loadfactor=10
              #web server 02
              BalancerMember http://web02.private/ loadfactor=10
              ProxySet lbmethod=byrequests
              Order Allow,Deny
              Allow from all
       </Proxy>

       ErrorLog /var/log/apache2/error-funny.log

       # Possible values include: debug, info, notice, warn, error, crit,
       # alert, emerg.
       LogLevel warn

       CustomLog /var/log/apache2/access-funny.log combined
 </VirtualHost>
a2ensite funny-lb
/etc/init.d/apache2 reload

参考URL

UserAgent?を見てページを出し分ける

Dir構造
/var/www/hoge/maintenance_mobile.html(携帯用のメンテナンスページ)
/var/www/hoge/maintenance.html(PC用のメンテナンスページ)
/var/www/hoge/.htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} ^(DoCoMo|KDDI|DDIPOKET|UP\.Browser|J-PHONE|Vodafone|SoftBank)
RewriteRule ^$ /maintenance_mobile.html [R]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^$ /maintenance.html [R]
</IfModule>

LB配下のサーバでIP制限をかける

LBをmod_proxyで実装していると、IPがLBのIPになってしまう為、IP別の振り分けを行う場合はこんな.htaccessを置くと振り分けが出来ます。

LB
 -Web1:/var/content/.htaccess <--ここに書く
 -Web2

特にproxy経由でアクセスしたLB越しのWebのリクエストのX-Forwared-forは
"オリジナルのIP,ProxyのIP"となっているので、正規表現をちょっと気をつける。

↓XXX.XXX.XXX.XXX.XXXとYYY.YYY.YYY.YYY以外だったらmaint.htmlを見せる
.htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond  %{HTTP:X-Forwarded-For} !^.*XXX\.XXX\.XXX\.XXX$
RewriteCond  %{HTTP:X-Forwarded-For} !^.*YYY\.YYY\.YYY\.YYY$
RewriteRule .* maint.html [R=302,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* ./index.php

特定のディレクトリに来たら別の場所へリダイレクト

引越しをした時など
http://example.jp/wiki --> http://example2.jp/wiki-new

/etc/apache2/site-enabled/example.jp.conf
<VirtualHost *:80>
       ServerName example.jp
       DocumentRoot /var/www/example_jp/
       <Directory />
               Options FollowSymLinks
               AllowOverride None
       </Directory>
       <Directory /var/www/example_jp/>
               Options FollowSymLinks MultiViews
               AllowOverride All
               Order allow,deny
               allow from all
       </Directory>
       AccessFileName .htaccess
       Redirect /wiki http://example2.jp/wiki-new
       ErrorLog /var/log/apache2/error.example.log
       LogLevel warn
       ServerSignature Off
       CustomLog /var/log/apache2/access.example.log combined
</VirtualHost>

IP別にページを出し分ける

/var/www/.htaccessとかで

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REMOTE_HOST} !^123\.45\.67\.89
RewriteRule \.html$ /other-page.html [R=302,L] 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* ./index.php

特定のファイルタイプを圧縮する

mod_deflateとmod_filterを有効にして

a2enmod deflate filter

vhostで設定している設定ファイルの<Directory></Directory>
の中に

<IfModule mod_filter.c>
   #Netscape 4.x :(
   BrowserMatch ^Mozilla/4 gzip-only-text/html
   #Netscape 4.06-4.08 :(
   BrowserMatch ^Mozilla/4\.0[678] no-gzip
   #MSIE/ 2.0.48以上のバージョンだと以下の方法じゃないと期待した動きをしない
   BrowserMatch \bMSIE[E] !no-gzip !gzip-only-text/html
   
   FilterDeclare Comp CONTENT_SET
   FilterProvider Comp DEFLATE resp=Content-Type $text/plain
   FilterProvider Comp DEFLATE resp=Content-Type $text/html
   FilterProvider Comp DEFLATE resp=Content-Type $application/javascript
   FilterProvider Comp DEFLATE resp=Conten-Type $text/css
   FilterChain Comp
   
   #don't compress images
   SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|zip|lzh|exe)$ dont-vary
   
   #proxies don't deliver the wrong content
   Header append Vary User-Agent env=!dont-vary
</IfModule>

とすると上記text/plain, text/html, application/javascript, text/cssは圧縮される。
ログを見ると
前 : XXX.XXX.XXX.XXX - - [14/Apr/2010:04:50:09 +0900] "GET /login HTTP/1.1" 200 1443
後 : XXX.XXX.XXX.XXX - - [14/Apr/2010:05:04:00 +0900] "GET /login HTTP/1.1" 200 771

1443バイトから771バイトになってるヾ(≧∀≦)ノ

画像へのリクエストをアクセスログに記録しない

SetEnvIf Request_URI \.gif image-request
SetEnvIf Request_URI \.jpg image-request
SetEnvIf Request_URI \.png image-request
CustomLog logs/access_log common env=!image-request

確認方法

設定ファイルを確認する

apache2ctl configtest

現在接続されているユーザーが終了後、restartする

apache2ctl graceful

強制再起動

apache2ctl restart
/etc/init.d/apache2 restart

エラー時には

request failed: error reading the headers

[Wed Feb 02 18:23:09 2011] [error] [client XXX.XXX.XXX.XXX] Invalid URI in request HTTP/1.1 200 OK
[Wed Feb 02 18:23:09 2011] [error] [client XXX.XXX.XXX.XXX] request failed: error reading the headers

調べてみるとリクエスト方法がおかしいようです。
多分botとかクローラーとかの実装方法がおかしいのかと。。

internal dummy connectionとは?

ログを見てみたら、

::1 - - [24/Jul/2009:05:29:32 +0900] "GET / HTTP/1.0" 200 11362 "-"
"Apache/2.2.3 (Debian) PHP/5.2.9-0.dotdeb.1 with Suhosin-Patch mod_ssl/2.2.3 
OpenSSL/0.9.8c (internal dummy connection)"

ってでてる。internal dummy connectionって何よ!と思って調べてみたら、こういう事らしい。

参考URL


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2012-03-12 (月) 16:54:44 (197d)