Apacheメモ

提供: 初心者エンジニアの簡易メモ
ナビゲーションに移動 検索に移動

apacheメモ

Apatch2.4の時に404エラーとなった場合

AH01630: client denied by server configuration:
<Directory "/var/www/hoge">
     AllowOverride All
     Require all granted
</Directory>

VirtualHostの設定方法

httpd.confを以下のように修正
*:80の部分は変更可能です。
Listen 80
# 追加
NameVirtualHost *:80
<VirtualHost *:80>
  ServerName sample1.example.com
  DocumentRoot "/var/www/html"
  <Directory "/var/www/html">
    AllowOverride All # .htaccessを許可
    Allow from All
  </Directory>
</VirtualHost>
<VirtualHost *:80>
  ServerName sample2.example.com
  DocumentRoot "/var/www/html/2"
  HostNameLookups off
  UseCanonicalName on
  <Directory "/var/www/html/2">
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All # .htaccessを許可
    Allow from All
  </Directory>
</VirtualHost>
hostsに以下を追加し確認
127.0.0.1       sample2.example.com
127.0.0.1       sample2.example.com

htaccessを許可/拒否

AllowOverride All # すべて許可
AllowOverride None # すべて拒否

詳細に設定したい場合は以下の通り

AllowOverride AuthConfig FileInfo Indexes Limit Options=Indexes,SymLinksIfOwnerMatch,MultiViews,FollowSymLinks,ExecCGI,Includes,IncludesNOEXEC

mod_rewriteの使い方

apacheのhttpd.confの以下コメントを外す。

LoadModule rewrite_module modules/mod_rewrite.so

例)URLの./abc/を./new/に変更する場合 .htaccess

RewriteEngine on
RewriteRule ^abc/(.*)$ new/$1 [L]

htmlを省略してアクセスできるように

例えば、ドメイン全体で、 /speed.html を /speed だけで、アクセスできる。

confに設定する。

  <IfModule mod_rewrite.c>
      RewriteEngine on
      RewriteCond %{HTTP_HOST} ^kajinet.localhost$
      #RewriteRule ^/koukoku$ /koukoku.html [L]
      RewriteRule ^/([\w]+)$ /$1.html [L]
  </IfModule>

指定dirの下だけhtmlを省略してアクセスできるように

例えば、 /example/speed.html を /example/speed だけで、アクセスできる。

指定dirの下だけ(例:example)

confに設定する。

  <IfModule mod_rewrite.c>
    RewriteEngine on

    # 1. すでに .html がついている直接アクセスや、実在するディレクトリはそのまま通す(ループ防止)
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]

    # 2. 指定dir配下のアクセスで、後ろに .html を補完してファイルが存在する場合のみ書き換える
    RewriteCond %{DOCUMENT_ROOT}/example/$1.html -f
    RewriteRule ^/example/([^/]+)$ /example/$1.html [L]
  </IfModule>

localhostでサブドメインを使用

ttp://www.localhost を d:/home/に割り当ててみる

C:\Program Files\Apache Group\Apache\conf\httpd.conf
一番下の
#</VirtualHost>
の下に追加記述
<VirtualHost *>
DocumentRoot d:/home
ServerName www.localhost
</VirtualHost>

C:\WINDOWS\system32\drivers\etc\
hostsに以下を追加
127.0.0.1       localhost
127.0.0.1       www.localhost
アパッチとブラウザを再起動

Index Of非表示方法

httpd.conf
Options Indexes FollowSymlinks MultiViews
↓
Options FollowSymlinks MultiViews

バーチャルホストconfを別ファイルで設定

  • httpd.confの最下に設定
Include conf/extra/httpd-vhosts.conf
  • extra/httpd-vhosts.confにVirtualHost設定記述

mod_proxy

<Location />
  ProxyPass http://localhost:8080/
  ProxyPassReverse  http://localhost:8080/
</Location>

test.example.comを内部的に別サーバ(test.cloudcore)で動作させる場合

 略
 </Directory>
 ProxyPass / http://test.cloudcore/
 ProxyPassReverse / http://test.cloudcore/
 ProxyPassReverseCookieDomain test.cloudcore test.example.com
 ProxyPassReverseCookiePath / /
</VirtualHost>

accesslogからipの集計

awk '{print $1;}' /etc/httpd/logs/access_log|sort|uniq -c
#アクセスの多いip上位100件
awk '{print $1;}' /etc/httpd/logs/access_log|sort|uniq -c|sort -r|head -n 100
#特定パスのみからip集計
grep /test1/ /etc/httpd/logs/access_log | awk '{print $1;}' |sort|uniq -c|sort -r|head -n 100

proxyを挟んでもip適切に出力する方法

/etc/httpd/conf.d/combined_x.conf

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"%{X-Forwarded-For}i\"" combined_x

/etc/httpd/conf.d/hogehoge.com.conf

CustomLog "/var/log/httpd/hogehoge.com.access_log" combined_x

参考:https://kakakikikeke.blogspot.jp/2013/07/apachex-forwarded-for.html

参考:http://rriifftt.hatenablog.com/entry/2016/02/10/155744

参考:http://dotnsf.blog.jp/archives/1018169332.html

VirtualHostにエラーログ追記

<VirtualHost *:80>
  ServerName hogehoge.com
  DocumentRoot "/var/www/zend/hogehoge/public"
  HostNameLookups off
  UseCanonicalName on
  ErrorLog /var/log/httpd/error_hogehoge_log
  <Directory "/var/www/zend/hogehoge/public">
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Allow from All
  </Directory>
</VirtualHost>

アプリ側にログがあれば、そちらが優先される。

例:zend

application/configs/application.ini
phpSettings.error_log = APPLICATION_PATH "/../data/logs/php_error.log"

拒否ipを外だしファイルで設定

<VirtualHost *:80>
  ServerName hoge.example.com
  DocumentRoot "/var/www/zend/hoge/public"
  HostNameLookups off
  UseCanonicalName on
  CustomLog /var/log/httpd/access_hoge_log combined env=!aptimenolog
  ErrorLog /var/log/httpd/error_hoge_log
  <Directory "/var/www/zend/hoge/public">
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Allow from All
    Include /etc/httpd/conf/extra/hoge_ip.conf
  </Directory>
</VirtualHost>

/etc/httpd/conf/extra/hoge_ip.conf

Order Deny,Allow
Deny from 192.168.1.100
Deny from 10.0.0.5
# 他の拒否IPを追加

拒否ua記述

<VirtualHost *:80>
  ServerName hoge.example.com
  DocumentRoot "/var/www/zend/hoge/public"
  HostNameLookups off
  UseCanonicalName on
  ErrorLog /var/log/httpd/error_hoge_log
  <Directory "/var/www/zend/hoge/public">
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Order Allow,Deny
    Allow from All

    # UAブロック
    BrowserMatchNoCase "(AhrefsBot|Applebot|Baiduspider)" bad_bot
    Deny from env=bad_bot

  </Directory>
</VirtualHost>