Apache: 2006年5月アーカイブ

RedHat系のRPMでインストールしたApacheなら
# /usr/sbin/httpd -S
とします。 DebianのパッケージのApacheだったら
# /usr/sbin/apache -S
です。 ※比較的最近のApacheじゃないとありません。
# /usr/sbin/httpd -S
VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
_default_:443          www.example.local (/etc/httpd/conf.d/ssl.conf:88)
*:80                   is a NameVirtualHost
         default server www.example.local (/etc/httpd/conf/httpd.conf:1032)
         port 80 namevhost www.hoge.local (/etc/httpd/conf/httpd.conf:1032)
         port 80 namevhost www.domain.local (/etc/httpd/conf/httpd.conf:1039)
Syntax OK

名前ベースのバーチャルホスト

|
1.NameVirtualHostや<VirtualHost >の引数の部分のIPアドレスとポート
2.ServerName "バーチャルホスト名"
の組み合わせで応答が決定します。

ブラウザが、「1」で設定されているIPアドレス、「2」で設定されているバーチャルホスト名でアクセスしたときに、Apache側は該当するServerNameが書かれている<VirtualHost >ディレクティブの設定で動作します。

 NameVirtualHostや<VirtualHost >ディレクティブの引数の部分の「*:80」は、「サーバーの持っているすべてのIPアドレス:ポート番号」を意味します。
 
 ここの引数の部分ですが、「*」のほかにもサーバーが持っている具体的なIPアドレスを指定してもいいし、本当のホスト名やバーチャルホスト名を指定しても大丈夫ですが、「*」か、IPアドレスをお勧めします

 また、ポート番号の部分「:80」も、httpd.confの上の方でListen 80を指定してあるので省略可能な場合がありますが、やはり明示的にポート番号を指定したほうがよいでしょう。※理由は後述

■プライマリ仮想サーバー
定義しているバーチャルホスト以外へのリクエストは、設定ファイル内で最初に定義してあるバーチャルホストに送られます。

 なので、一番最初の<VirtualHost *:80>は、http://"IPアドレス"や、http://"本当のサーバー名"などでアクセスされた場合を想定して記述しておくことになります。

※アドレスベースのバーチャルホストで、<VirtualHost _default_:80>を設定してしまうのも有効です。

NameVirtualHost *:80

<VirtualHost *:80> ServerAdmin webmaster@example.local ServerName www.example.local DocumentRoot /var/www/html DirectoryIndex index.html </VirtualHost>

<VirtualHost *:80> ServerAdmin webmaster@hoge.local ServerName www.hoge.local DocumentRoot /home/www.hoge.local/htdocs DirectoryIndex index.html ErrorLog /home/www.hoge.local/logs/error_log CustomLog /home/www.hoge.local/logs/access_log common </VirtualHost>

<VirtualHost *:80> ServerAdmin webmaster@domain.local ServerName www.domain.local DocumentRoot /home/www.domain.local/htdocs DirectoryIndex index.html ErrorLog /home/www.domain.local/logs/error_log CustomLog /home/www.domain.local/logs/access_log common </VirtualHost>

■NameVirtualHostや<VirtualHost >ディレクティブでサーバー名を指定しないほうがよい理由

下記のような書き方もできます。
わかりやすいといえばわかりやすいです。

しかし、サーバー名に依存した記述をすると、DNSに依存することになります。
サーバーの起動時にバーチャルホスト名の名前引きができなかった場合、その名前ベースバーチャルホスト設定は効き目がなくなって、一番上のバーチャルホストの設定で応答することになります。

NameVirtualHost www.example.local:80

<VirtualHost www.example.local:80> ServerAdmin webmaster@example.local ServerName www.example.local DocumentRoot /var/www/html DirectoryIndex index.html </VirtualHost>

<VirtualHost www.hoge.local:80> ServerAdmin webmaster@hoge.local ServerName www.hoge.local DocumentRoot /home/www.hoge.local/htdocs DirectoryIndex index.html ErrorLog /home/www.hoge.local/logs/error_log CustomLog /home/www.hoge.local/logs/access_log common </VirtualHost>

<VirtualHost www.domain.local:80> ServerAdmin webmaster@domain.local ServerName www.domain.local DocumentRoot /home/www.domain.local/htdocs DirectoryIndex index.html ErrorLog /home/www.domain.local/logs/error_log CustomLog /home/www.domain.local/logs/access_log common </VirtualHost>


■NameVirtualHostや<VirtualHost >ディレクティブで、ポート番号を明示的に指定したほうがよい理由

 RedHat系のLinuxなどでmod_sslをインストールしている場合、/etc/httpd/conf.d/ssl.confにdefaultのIPアドレスベースのバーチャルホストで、ポート443で待ち受ける設定があるので、通常のhttpアクセスのバーチャルホスト設定のほうでも80番を明示的に指定しないとapacheの起動時に警告を受けます。

○/etc/httpd/conf.d/ssl.confのdefault設定

LoadModule ssl_module modules/mod_ssl.so
Listen 443
AddType application/x-x509-ca-cert .crt
AddType application/x-pkcs7-crl    .crl
SSLPassPhraseDialog  builtin
SSLSessionCache         shmcb:/var/cache/mod_ssl/scache(512000)
SSLSessionCacheTimeout  300
SSLMutex default
SSLRandomSeed startup file:/dev/urandom  256
SSLRandomSeed connect builtin
SSLCryptoDevice builtin
##
## SSL Virtual Host Context
##
<VirtualHost _default_:443>
  ErrorLog logs/ssl_error_log
  TransferLog logs/ssl_access_log
  LogLevel warn

SSLEngine on SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP SSLCertificateFile /etc/httpd/conf/ssl.crt/server.crt SSLCertificateKeyFile /etc/httpd/conf/ssl.key/server.key

<Files ~ "\.(cgi|shtml|phtml|php3?)$"> SSLOptions +StdEnvVars </Files> <Directory "/var/www/cgi-bin"> SSLOptions +StdEnvVars </Directory>

SetEnvIf User-Agent ".*MSIE.*" \ nokeepalive ssl-unclean-shutdown \ downgrade-1.0 force-response-1.0

CustomLog logs/ssl_request_log \ "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b" </VirtualHost>

いつも忘れるので書いておこう。
ある場所からは認証なしでページ閲覧できて、
他の場所からは認証が必要になるというパターンの.htaccessの書き方。
もちろん、httpd.confなどに直接書いてもOKです。

【条件】
mod_access モジュールが有効になっていること
mod_auth モジュールが有効になっていること

○httpd.conf
AllowOverride Limit AuthConfig 

○.htaccess

# "Limit"ルール
#
# この書き方に違和感を覚える人もいるかもしれませんが、
# (特にルーターの設定をする人)
# ルーターのACLは上から評価していって、
# 適合するものがあったらその時点で評価は終了しますが、
# Apacheのこれは、Orderの順番で必ず両方を評価していき、
# 下位のACLが上位のACLを上書きしていきます。
# つまり、172.16.0.1の場合、"Deny from all"で拒絶対象となりますが
# 次の"Allow from 172.16."で救済されるわけです。
# 
Order Deny,Allow
Deny from all
Allow from 10.0.0.32/27 172.16. 192.168.0.1 .example.local

# # "AuthConfig"ルール # AuthUserFile /var/www/htpasswd AuthName "Secret Area" AuthType Basic require valid-user

# # Sastisfy any だと"Limit"と"AuthConfig"どちらかにパスすればOKです。 # これを Satisfy all とすると、 # "Limit"と"AuthConfig"両方の条件を満たさないとOKとなりません。 Satisfy any

このアーカイブについて

このページには、2006年5月以降に書かれたブログ記事のうちApacheカテゴリに属しているものが含まれています。

前のアーカイブはApache: 2006年4月です。

次のアーカイブはApache: 2008年4月です。

最近のコンテンツはインデックスページで見られます。過去に書かれたものはアーカイブのページで見られます。

Apache: 2006年5月: 月別アーカイブ

Powered by Movable Type 4.1