LoginSignup
102
120

More than 5 years have passed since last update.

Apache:アクセス制御をする

Last updated at Posted at 2015-03-18

アクセス制限

httpd.conf
<Directory "/usr/local/apache2/htdocs">
#Order:Allow,Denyの処理順序を決める
#Allow:許可するアクセス元を指定
#Deny :禁止するアクセス元を指定
    Order allow,deny
    Allow from all            #すべて
    Deny  from 192.168.1.28   #IPアドレス指定
    Deny  from 192.168.2      #IPアドレス前半
    Deny  from 192.168.3.0/255.255.255.0  #ネットマスク
    Deny  from 192.168.3.0/24 #サブネットマスク
    Deny  from dummy.com      #ホスト名、ドメイン名の一部     
</Directory>

再起動してアクセス制御を確認する。
上記はDirectory単位のアクセス制御になる。
<File>内に記載するとFile単位のアクセス制御になる。

HTTPリクエストヘッダー情報で制限する

HTTPのリクエストヘッダーのWEBブラウザの種類の情報、リンク元の情報によるアクセス制限をする

HTTPリクエストヘッダーの確認方法

Chromeブラウザでchrome://net-internals/#eventsにアクセスして、「type:URL_REQUEST」で検索する。

User-Agent(WEBブラウザの種類の情報)による制限

HTTPのリクエストヘッダーにWEBブラウザの種類の情報(User-Agent)が含まれている。

User-Agentヘッダーに"Mozilla"が含まれるWEBブラウザのみ許可する

BrowserMatch:User-Agentヘッダーのみ対象

httpd.conf
<Directory "/usr/local/apache2/htdocs">

# BrowserMatch: User-Agentのヘッダーに正規表現がマッチすれば環境変数を設定する
# "Mozilla"がマッチすれば、環境変数mozillaを設定する
    BrowserMatch    "Mozilla" mozilla 
    Order deny,allow
    Deny  from all            
    #環境変数mozillaが設定されていればアクセスを許可する
    Allow from env= mozilla        
</Directory>

Referer(リンク元のURL情報)による制限

リンク元によるアクセス制御はページ内の画像にディープリンク(直リンク)されたくない場合やアクセス解析を混乱させるリファラーSPAM対策時に有効。

SetEnvIf:任意のHTTPリクエストヘッダーが対象

httpd.conf
# アイコンのディレクトリコンテナ
<Directory "/usr/local/apache2/icons">

# SetEnvIf: 指定した対象に正規表現がマッチしたら環境変数を設定する
#(任意のHTTPのリクエストヘッダーを対象にできる)
# Refererヘッダーに"www.example.jp"が含まれていれば、環境変数exampleを設定する
    SetEnvIf Referer "www\.example\.jp" example
    Order deny,allow
    Deny  from all            
    #環境変数exampleが設定されていればアクセスを許可する
    Allow from env= example
</Directory>

エラーメッセージを変更する

httpd.conf
ErrorDocument 500 "The server made a boo boo."
ErrorDocument 404 /missing.html
ErrorDocument 404 "/cgi-bin/missing_handler.pl"
ErrorDocument 402 http://www.example.com/subscription_info.html

パスワードを設定する

htpasswdでユーザーを登録し、設定ファイルで認証を設定することでパスワードによるアクセス制限をかける。

htpasswd:ユーザー登録をする

htpasswd -c [passwd file名] [ユーザー名]
実行後、パスワードファイルが作成される。

#設定ファイルのディレクトリに移動してから実行する
$ cd /etc/httpd/conf/                          
$ htpasswd -c passwdfile vagrant 
New password: 
Re-type new password: 
Adding password for user vagrant

httpd.conf:Basic認証の設定をする

httpd.conf
<Directory "/usr/local/apache2/htdocs">
# ~
# ~ 
# AuthType     :ユーザー認証の種類を指定
# AuthName     :認証の範囲をユーザーに知らせるメッセージを指定
# AuthUserFile :ユーザー名とパスワードが登録されたファイルを指定
# Require      :認証してアクセスできるユーザーを指定。
#              (valid-userで認証したすべてのユーザー)
    AuthType Basic # 基本認証を指定
    AuthName "Password for example.com" # 認証ダイアログに表示されるメッセージ
    AuthUserFile /etc/httpd/conf/passwdfile #ユーザー登録作ったファイル          
    Require valid-user #認証に成功したユーザーはすべてアクセスできる        
</Directory>

Apacheを再起動し、Basic認証設定を確認する。

$ /etc/rc.d/init.d/httpd restart

お気に入り.png

認証してグループ単位で許可する

グループを登録する(グループファイルを作成する)

$ vi groupauth

[グループ名]:[ユーザー名] [ユーザー名]・・・(ユーザー名は半角で区切る)

example :vagrant example_user
dummy   :vagrant dummy_user
httpd.conf
<Directory "/usr/local/apache2/htdocs">
# ~
# ~
# AuthUserFile :グループが登録されたファイルを指定
# Require group:認証してアクセスできるグループを指定
    AuthType Basic
    AuthName "Password for example.com"
    AuthUserFile /etc/httpd/conf/passwdfile
    AuthUserFile /etc/httpd/conf/groupauth # グループ登録で作ったファイル     
    Require group example #exampleグループを認証する
</Directory>

アクセス制限と認証を組み合わせる

httpd.conf
<Directory "/usr/local/apache2/htdocs">
    # アクセス制限
    Order allow,deny
    Allow from all
    Deny  from 192.168.1.28
    # 認証
    AuthType Basic
    AuthName "Password for example.com"
    AuthUserFile /etc/httpd/conf/passwdfile 
    Require valid-user

# Satisfy:アクセス制限と認証の関係
#      all - 両方の条件にあった場合、許可する
#      any - いずれかの条件にあった場合、許可する
    Satisfy all #記載しなくてもデフォルトはSatisfy all
</Directory>

リダイレクト

URLが変更になった時、変更先のURLを返す

一時的にリダイレクト

ファイルの末尾に下記の内容を追加する

httpd.conf
# Redirect [アクセスされるPath] [リダイレクト先]
Redirect /redirect_test/ http://example.com/test/  

http://example.com/redirect_test/ にアクセスすると、
http://example.com/test/ にリダイレクトされる。

Pathは正規表現での指定も可能

httpd.conf
Redirect ^/redirect_test/(.*)$ http://example.com/test/ 

引越しのためのリダイレクト

ファイルの末尾に下記の内容を追加する

httpd.conf
# Redirect [ステータス] [アクセスされるPath] [リダイレクト先]
# parmanent:ステータスコード301(Moved Parmanetly)を返す。省略すると302(tmp)    
Redirect parmanent /redirect_test/ http://example.com/test/ 

コンテントネゴシエーション

WEBブラウザはHTTPリクエストヘッダーは言語、文字タイプ、データタイプなどの環境情報について通知している。それぞれ複数の形式で優先度が付いており、サーバー側が最適なものを選択できる。
このように、処理できるデータ形式をクライアントとサーバー間で自動的に問い合わせることをコンテントネゴシエーションという。

# 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

httpd.conf:Options-MultiViews

httpd.conf
<Directory "/usr/local/apache2/htdocs">
#~
#~
# MultiViews :コンテントネゴシエーションを有効にするOption指定
    Options FollowSymLinks MultiViews
#~
#~
# AddLanguage :拡張子を言語指定に関連づける
AddLanguage en .en
AddLanguage ja .ja
# AddCharset:拡張子を文字コード指定に関連づける
AddCharset shift_jis .sjis
#~
#~
</Directory>

DocumentRootに日本語のページ(lang.html.ja)と英語のページ(lang.html.en)を置いてアクセスする
http://example.com/lang.html
http://example.com/lang.html.ja
http://example.com/lang.html.en

日本語のページが表示される

以下、lang.htmlサンプル
linuxの日本語対応についてはこちら

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>
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>

HTTPリクエストヘッダーで優先順位が決まらない、もしくは指定がない場合はLanguagePriority,AddDefaultCharsetの設定を参照する

httpd.conf
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

サーバーの種類を隠す

HTTPレスポンスヘッダー、エラードキュメントのサーバー種類、バージョン情報が含まれているが、サーバーバージョン固有の脆弱性が発見された時攻撃対象になるのを防ぐためこれらの情報を隠す設定ができる。

ServerTokens: HTTPレスポンスヘッダーのServerヘッダー情報の表示設定

ServerSignature: エラードキュメントの末尾情報の表示、非表示設定(On/Off)

httpd.conf
ServerTokens Prod
ServerSignature Off
ServerTokensの指定
Prod Apache
Majar Apache/2
Minor Apache/2.2
Minimal Apache/2.2.15
OS Apache/2.2.15(CentOS)
Full(デフォルト) Apache/2.2.15(CentOS)DAV/2

データタイプを追加する

MIMEタイプはサーバー側が決めてHTTPレスポンスヘッダーContentTypeに記述する

MIMEタイプ

テキスト:text/plain
HTML :text/html
jpeg画像:image/jpeg

AddType

ファイル名の拡張子をMIMEタイプに関連づける
httpd.conf
AddType application/epub+zip .epub

ロボットからのアクセスを制限する

WEB検索エンジンはロボットというプログラムでWEBページを収集しており、無駄なサーバーアクセスを減らし負荷を軽減するためにロボットのアクセス制限する

収集されたくないディレクトリをrobots.txtで指定する

robots.txtはDocumentRootディレクトリに作成すること。

robots.txt
User-agent: *
Disallow : /test/
Disallow : /data/
102
120
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
102
120