Apache設定パターン
#Apache
本番設定及び、セキュリティ設定
バージョン非表示
https://httpd.apache.org/docs/2.4/ja/mod/core.html#servertokens
code:conf
ServerTokens Prod
ServerSignature off
参考
cmanのhtaccess生成ツール
https://htaccess.cman.jp/explain/redirect.html
メンテナンスページの表示
.htaccess
code:.htaccess
ErrorDocument 503 /maintenance.html
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !=/maintenance.css
RewriteCond %{REQUEST_URI} !=/icon.png
RewriteCond %{REQUEST_URI} !=/maintenance.html
RewriteRule ^.*$ - R=503,L
</IfModule>
※、メンテナンスページに、maintenance.cssとicon.pngを使う場合の設定。
ErrorDocument
core - Apache HTTP サーバ バージョン 2.4
ErrorDocument 503 /maintenance.html
503が発生した際maintenance.html をレスポンス。
RewriteBase
ベースパスの設定
mod_rewrite - Apache HTTP Server Version 2.4
RewriteRule
mod_rewrite - Apache HTTP Server Version 2.4
RewriteRuleは、-でURLの置換をしない
フラグについて
RewriteRule Flags - Apache HTTP Server Version 2.4
LはLastのLで、マッチした後、以降のRewrite処理を行わない設定。
R はリダイレクト
(300〜399)の外にある場合は置換されない。
F
forbidden
暗黙的にLが指定される
正規表現参考
Apache | RewriteCondディレクティブ:URLのリライト/リダイレクトを行う条件を定義する
!=で文字列完全不一致チェック
このように設定するとmentainance.htmlが503ステータスでレスポンスされることになる。
全て拒否
.htaccess
code:deny
Order Allow,Deny
デフォルト拒否
後続に許可する設定を入れることができる
Deny from all
全て拒否
txtなどのファイルにアクセス制限を追加する
code:referer
<Files ~ "\.(txt)$">
Deny from all
</Files>
Apache | Apache2.4でOrder/Allow/Denyディレクティブを利用する
PDFなどのファイルに独自URLを設定する
pdfを表示するが、URLをsomeurlにしておく。
.htaccess
code:pdf用リダイレクトURL
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^someurl/$ wp-content/uploads/some.pdf L
</IfModule>
Alias
https://httpd.apache.org/docs/2.4/ja/mod/mod_alias.html
ドキュメントルート外のディレクトリにマッピング
code:conf
# /download パスをドキュメントルート外のディレクトリにマッピング
Alias /user/download /var/www/download
# ダウンロード用のディレクトリ設定
<Directory /var/www/download>
# ダウンロード用にContent-Dispositionヘッダーを設定
<FilesMatch ".*">
ForceType application/octet-stream
Header set Content-Disposition "attachment"
</FilesMatch>
</Directory>
サイト移行のリダイレクト
301なので永久。
sample.comへのアクセスのみをリダイレクトしたいとき
HTTP_HOSTはアクセスされたドメイン
code:.htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sample\.com$
RewriteRule ^(.*)$ https://www.sample.com%{REQUEST_URI} R=301,L
全てトップへリダイレクト
code:htaccess
RewriteEngine On
RewriteRule ^(.*)$ https://www.new.com R=301,L
httpとhttpsの両対応するリダイレクト
code:.htaccess
######
# Redirect
RewriteEngine On
# base path
RewriteBase /
RewriteCond %{SERVER_PORT} 80
RewriteRule ^sample/(.*)$ http://www.cman.jp/$1 R=301,L
RewriteCond %{SERVER_PORT} 443
RewriteRule ^sample/(.*)$ https://www.cman.jp/$1 R=301,L
httpsへのリダイレクト
転送Forwarded-Protoがhttpsでないとき、httpsへリダイレクト
code:htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP:X-Forwarded-Proto} !=https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} R=301,L
</IfModule>
CORS
https://gist.github.com/nixta/0b98d7975562bc31c4c9
debian
sudo a2enmod headers
モジュール有効化が必要
CORSの設定
apacheのconfファイルで設定
mod_headers - Apache HTTP サーバ バージョン 2.4
code:conf
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Methods "GET, POST, OPTIONS"
Header set Access-Control-Allow-Headers "Content-Type"
ベーシック認証
apacheのconfファイルで設定
sudo htpasswd -c /var/www/.htpasswd user
.htpasswdファイル作成
code:conf
<Directory "/var/www/sample.com">
AuthType Basic
AuthName "Restricted Content"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
</Directory>
.htaccessで設定
https://htaccess.cman.jp/explain/basic.html
code:.htaccess
# authenticate(Basic)
AuthUserFile /var/www/htpasswd/.htpasswd
AuthGroupFile /dev/null
AuthName "Please enter your ID and password"
AuthType Basic
require valid-user
Health Check
health.phpのみ許可する
code:conf
<VirtualHost *:80>
ServerName any
DocumentRoot /var/www/default
<Location />
SetEnvIf Request_URI "/health.php" healthcheck
require env healthcheck
</Location>
</VirtualHost>
参考
basic認証
https://httpd.apache.org/docs/2.4/ja/howto/auth.html
digitalocean blog
https://www.digitalocean.com/community/tutorials/how-to-set-up-password-authentication-with-apache-on-ubuntu-14-04
basic認証ファイルを作成するコマンド
apache2はUbuntuなどの場合。
code:basic.sh
sudo htpasswd -c /etc/apache2/.htpasswd username
キャッシュさせないようにする
参考
https://teach.web-represent.link/how-to-disable-caching-in-apache/
code:conf
<ifmodule mod_headers.c>
Header set Cache-Control "no-store, no-cache, must-revalidate, max-age=0"
Header set Pragma "no-cache"
Header set Expires "0"
</ifmodule>
no-store
https://developer.mozilla.org/ja/docs/Web/HTTP/Headers/Cache-Control
レスポンスディレクティブの no-store は、あらゆる種類のキャッシュが(プライベートであろうと共有であろうと)このレスポンスを保存しないようにすることを指示します。
通常、must-revalidate は max-age と共に使用されます。
https://developer.mozilla.org/ja/docs/Web/HTTP/Reference/Headers/Cache-Control
private
ユーザー個人を特定するコンテンツ、特にログイン後に受け取るレスポンスや、Cookie で管理されるセッションについては、private ディレクティブを追加する必要があります。
no-storeが優先される
理論的には、ディレクティブが衝突した場合、最も制限の厳しいディレクティブが尊重されます。つまり、以下の例は基本的に無意味です。private, no-cache, max-age=0, must-revalidate は no-store と競合しているからです。
Expires
https://developer.mozilla.org/ja/docs/Web/HTTP/Headers/Expires
レスポンスに max-age または s-maxage ディレクティブを持つ Cache-Control ヘッダーがある場合、Expires ヘッダーは無視されます。
Pragma
https://developer.mozilla.org/ja/docs/Web/HTTP/Headers/Pragma
Pragma は HTTP/1.0 クライアントとの下位互換性のためにのみ使用してください。