📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)
ページの作成:「== 概要 == <br><br> == ディレクトリ単位のアクセス制御 == <syntaxhighlight lang="apache"> <Directory "/srv/www/htdocs"> # Order : AllowおよびDenyの順序を決める # Allow : 許可するアクセス元を指定する # Deny : 禁止するアクセス元を指定する Order Allow,Deny # 順序は、Allowが先 Allow from all # 全て許可する Deny from 192.168.1.10 # IPアドレスを…」 |
|||
| (同じ利用者による、間の6版が非表示) | |||
| 98行目: | 98行目: | ||
# 環境変数exampleが設定されている場合はアクセスを許可する | # 環境変数exampleが設定されている場合はアクセスを許可する | ||
Allow from env=example | Allow from env=example | ||
</Directory> | |||
</syntaxhighlight> | |||
<br><br> | |||
== リダイレクト == | |||
==== 一時リダイレクト ==== | |||
<syntaxhighlight lang="apache"> | |||
# httpd.confファイル | |||
# Redirect <アクセスするパス> <リダイレクト先のパスまたはURL> | |||
# これは、ステータスコード 302 (tmp) である | |||
# 以下の例では、http://example.com/redirect_sample/ にアクセスする場合、http://example.com/redirect/ にリダイレクトされる | |||
Redirect /redirect_sample/ http://example.com/redirect/ | |||
# アクセスするパスは正規表現を使用して指定することもできる | |||
# 以下の例は、http://example.com/redirect_sample/内のファイルにアクセスする場合は、全て http://example.com/redirect/ にリダイレクトされる | |||
Redirect ^/redirect_sample/(.*)$ http://example.com/redirect/ | |||
</syntaxhighlight> | |||
<br> | |||
==== 恒久リダイレクト ==== | |||
<syntaxhighlight lang="apache"> | |||
# httpd.confファイル | |||
# Redirect <ステータス> <アクセスするパス> <リダイレクト先のパスまたはURL> | |||
# これは、ステータスparmanent (ステータスコード 301 : Moved Parmanetly) を返す | |||
Redirect parmanent /redirect_sample/ http://example.com/redirect/ | |||
</syntaxhighlight> | |||
<br><br> | |||
== コンテントネゴシエーション == | |||
HTTPリクエストヘッダは、言語、文字タイプ、データタイプ等の環境情報について通知している。<br> | |||
複数の形式で優先度が付いており、サーバ側は最適なものを選択することができる。<br> | |||
<br> | |||
このように、処理できるデータ形式をクライアントとサーバ間で自動的に問い合わせることを、<u>コンテントネゴシエーション</u>と呼ぶ。<br> | |||
<br> | |||
# Accept : クライアントが処理できるデータタイプと優先度 | |||
# Accept-Language : クライアントが処理できる言語と優先度 | |||
# Accept-Charset : クライアントが処理できる文字コードと優先度 | |||
# Accept-Encoding : クライアントが処理できるデータ転送方式と優先度 | |||
# q : 品質値 | |||
GET / HTTP / 1.1 | |||
Accept-Language : ja, en-us; q=0.7, en:q=0.3 | |||
Accept-Charset : Shift_JIS,utf-8; q=0.7,*;q=0.7 | |||
<br> | |||
<syntaxhighlight lang="apache"> | |||
# httpd.confファイル | |||
<Directory "/srv/www/htdocs"> | |||
# MultiViews : コンテントネゴシエーションを有効にする | |||
Options FollowSymLinks MultiViews | |||
# AddLanguage : 拡張子を言語指定に関連付ける | |||
AddLanguage en .en | |||
AddLanguage ja .ja | |||
# AddCharset : 拡張子を文字コード指定に関連付ける | |||
AddCharset shift_jis .sjis | |||
</Directory> | |||
</syntaxhighlight> | |||
<br> | |||
ドキュメントルートに日本語のページ(lang.html.ja)と英語のページ(lang.html.en)を配置してアクセスする。<br> | |||
* http://example.com/lang.html | |||
* http://example.com/lang.html.ja | |||
* http://example.com/lang.html.en | |||
<br> | |||
<syntaxhighlight lang="html"> | |||
<!-- lang.html.ja --> | |||
<html> | |||
<head> | |||
<META http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |||
<title>日本語のページ</title> | |||
</head> | |||
<body> | |||
<p>日本語のコンテンツ</p> | |||
<p>English Content</p> | |||
</body> | |||
</html> | |||
</syntaxhighlight> | |||
<br> | |||
<syntaxhighlight lang="html"> | |||
<!-- lang.html.en --> | |||
<html> | |||
<head> | |||
<META http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |||
<title>English Page</title> | |||
</head> | |||
<body> | |||
<p>日本語のコンテンツ</p> | |||
<p>English Content</p> | |||
</body> | |||
</html> | |||
</syntaxhighlight> | |||
<br> | |||
HTTPリクエストヘッダで優先順位が決まらない、または、指定が無い場合は、<code>LanguagePriority</code>、<code>AddDefaultCharset</code>の設定を参照すること。<br> | |||
<syntaxhighlight lang="apache"> | |||
LanguagePriority en ca cs da de el eo es et fr he hr it ja ko ltz nl nn no pl pt pt-BR ru sv zh-CN zh-TW | |||
AddDefaultCharset off | |||
</syntaxhighlight> | |||
<br><br> | |||
== BASIC認証 == | |||
==== ユーザ登録 ==== | |||
# htpasswd -c <パスワードファイルのパス> <ユーザ名> | |||
htpasswd -c Password.txt test-user | |||
<br> | |||
==== ユーザ単位の設定 ==== | |||
<syntaxhighlight lang="apache"> | |||
# httpd.confファイル | |||
<Directory "/srv/www/htdocs"> | |||
# AuthType : ユーザ認証の種類を指定 | |||
# AuthName : 認証ダイアログに表示されるメッセージを指定 | |||
# AuthUserFile : ユーザ名とパスワードが保存されたファイルを指定 | |||
# Require : 認証してアクセスできるユーザを指定 | |||
# valid-userを指定する場合、BASIC認証した全てのユーザがアクセスが許可される | |||
AuthType Basic | |||
AuthName "Password for example.com" | |||
AuthUserFile /srv/www/conf/password.txt | |||
Require valid-user | |||
</Directory> | |||
</syntaxhighlight> | |||
<br> | |||
Apacheを再起動する。<br> | |||
sudo systemctl restart apache または sudo systemctl restart httpd | |||
<br> | |||
==== グループ単位の設定 ==== | |||
グループ設定ファイルを作成して、グループを登録する。<br> | |||
vi <グループ設定ファイル> | |||
<br> | |||
# <グループ名>:<ユーザ名> <ユーザ名 1> <ユーザ名 2> <ユーザ名 3> ...略 | |||
example1 : test-user1 test-user2 | |||
example2 : test-user1 test-user3 | |||
<br> | |||
<syntaxhighlight lang="apache"> | |||
# httpd.confファイル | |||
<Directory "/srv/www/htdocs"> | |||
# AuthType : ユーザ認証の種類を指定 | |||
# AuthName : 認証ダイアログに表示されるメッセージを指定 | |||
# AuthUserFile : 上記で作成したグループ設定ファイルのパスを指定 | |||
# Require group : アクセス認証可能なグループ名を指定 | |||
AuthType Basic | |||
AuthName "Password for example.com" | |||
AuthUserFile /srv/www/conf/password.txt | |||
AuthUserFile /srv/www/conf/groupauth.txt | |||
Require group example2 # example2グループを認証する | |||
</Directory> | |||
</syntaxhighlight> | |||
<br> | |||
==== アクセス制限と認証の組み合わせ ==== | |||
<syntaxhighlight lang="apache"> | |||
# httpd.confファイル | |||
<Directory "/srv/www/htdocs"> | |||
# アクセス制限 | |||
Order allow,deny | |||
Allow from all | |||
Deny from 192.168.0.10 | |||
# 認証 | |||
# AuthType : ユーザ認証の種類を指定 | |||
# AuthName : 認証ダイアログに表示されるメッセージを指定 | |||
# AuthUserFile : ユーザ設定ファイルまたはグループ設定ファイルのパスを指定 | |||
# Require group : アクセス認証可能なグループ名を指定 | |||
AuthType Basic | |||
AuthName "Password for example.com" | |||
AuthUserFile /srv/www/conf/password.txt | |||
Require valid-user | |||
# Satisfy : アクセス制限と認証の関係 | |||
# all - 両方の条件を満たした場合、許可する | |||
# any - いずれかの条件を満たした場合、許可する | |||
# 未指定の場合は、Satisfy all | |||
Satisfy all | |||
</Directory> | </Directory> | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| 119行目: | 300行目: | ||
ErrorDocument 500 "The server made a boo boo." | ErrorDocument 500 "The server made a boo boo." | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<br><br> | |||
== ロボットからのアクセスを制限 == | |||
ロボットにより収集されたくないディレクトリおよびファイルを、robots.txtファイルで指定する。<br> | |||
<u>また、robots.txtファイルは、DocumentRootディレクトリに作成する必要がある。</u><br> | |||
<br> | |||
# robots.txtファイル | |||
User-agent: * | |||
Disallow : /hoge/ | |||
Disallow : /piyo/fuga/ | |||
<br><br> | |||
== データタイプの追加 == | |||
MIMEタイプは、サーバ側で決めた後、HTTPレスポンスヘッダであるContentTypeに記述する。<br> | |||
<br> | |||
MIMEタイプの例を以下に示す。<br> | |||
* テキスト | |||
*: text/plain | |||
* CSV | |||
*: text/csv | |||
* HTML | |||
*: text/html | |||
* CSS | |||
*: text/css | |||
* PHP | |||
*: application/x-httpd-php | |||
* XML | |||
*: text/xml (ただし、ユーザから読めるものである場合 ([https://tools.ietf.org/html/rfc3023 RFC 3023, section 3])) | |||
* JSON | |||
*: application/json | |||
* 画像 | |||
*: image/gif | |||
*: image/bmp | |||
*: image/jpeg | |||
*: image/png | |||
*: image/tiff | |||
*: image/webp | |||
*: image/svg+xml | |||
<br> | |||
<syntaxhighlight lang="apache"> | |||
# httpd.confファイル | |||
# ファイル名の拡張子をMIMEタイプに関連付ける | |||
AddType application/epub+zip .epub | |||
</syntaxhighlight> | |||
<br><br> | |||
== サーバの種類を隠す == | |||
HTTPレスポンスヘッダ、エラードキュメントのサーバ種類、バージョン情報が含まれている。<br> | |||
サーバのバージョン固有の脆弱性が発見された場合、攻撃対象を回避するためにこれらの情報を隠すことができる。<br> | |||
<br> | |||
<syntaxhighlight lang="apache"> | |||
# httpd.confファイル | |||
# ServerTokens : HTTPレスポンスヘッダのServerヘッダ情報の表示を設定する | |||
ServerTokens Prod | |||
# ServerSignature : エラードキュメントの末尾情報の表示 / 非表示を設定する | |||
# On または Offを指定する | |||
ServerSignature Off | |||
</syntaxhighlight> | |||
<br> | |||
<center> | |||
{| class="wikitable" | style="background-color:#fefefe;" | |||
|+ ServerTokensの設定 | |||
|- | |||
! style="background-color:#66CCFF; width: 50%" | ServerTokensの種類 | |||
! style="background-color:#66CCFF; width: 50%" | 例 | |||
|- | |||
| Prod || Apache | |||
|- | |||
| Majar || Apache/2 | |||
|- | |||
| Minor || Apache/2.2 | |||
|- | |||
| Minimal || Apache/2.2.15 | |||
|- | |||
| OS || Apache/2.2.15(CentOS) | |||
|- | |||
| Full<br>(デフォルト) || Apache/2.2.15(CentOS)DAV/2 | |||
|} | |||
</center> | |||
<br><br> | <br><br> | ||