0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Apache 備忘録

Posted at

設定ファイル

httpd.conf

Alias

URLをファイルシステムの位置にマップ
Alias URLパス ディレクトリ|ファイルパス
※URLパスの末尾には/をつけない(/があるものと認識される)
※ディレクトリを指定する場合は最後に/をつける(意図しない挙動を生む場合があるため)

Alias /apache /Applications/MAMP/htdocs/

Apache リファレンス

Directory

指定したディレクトリとサブディレクトリのみで適応される設定

<Directory ディレクトリ>
# 処理
</Directory>


<Directory "/Applications/MAMP/htdocs/">
    DirectoryIndex xxx.xx
</Directory>

DirectoryIndex

デフォルトで表示されるファイルを指定できる

DirectoryIndex file1.html

Options

ディレクトリに対して使用可能な機能を設定する

<Directory /web/docs>
Options Indexes FollowSymLinks
</Directory>

<Directory /web/docs/spec>
Options Includes
</Directory>

# /web/docs/spec というディレクトリには、 Includes だけが適用
<Directory /web/docs>
Options Indexes FollowSymLinks
</Directory>

<Directory /web/docs/spec>
Options +Includes -Indexes
</Directory>
# /web/docs/spec というディレクトリには、 FollowSymLinks と Includes が適用

htaccess

httpd.conf以外で設定を追加する方法
Directoryでの設定の代わりにフォルダ内に直接配置するイメージ

  • AllowOverride Allを設定する
  • httpd.confに設定を追加する
  • パフォーマンスは落ちる(htaccessがあるかどうかの確認が入るため)
  • 配置したディレクトリ、サブディレクトリで有効
  • 下位階層で上書き可能

Redirect

異なるURLに転送する
statusのデフォルトは302
(301:永続的なリダイレクト 302:一時的なリダイレクト)
※URL-pathは前方一致のため無限ループに注意

Redirect [status] URL-path 飛ばしたいURL

Redirect /apache/redirect-test /apache

301,302の違い

301:永続的なリダイレクト
⇒キャッシュがブラウザに残るため、あとから設定を変更してもキャッシュを参照するため、反映されない

302:一時的なリダイレクト
⇒キャッシュが残らないため、設定を変更したら問題なく変更が反映される

LogLevel

どのレベルまでエラーログを出力するかの設定
emerg alert crit error warn(デフォルト) notice info debug がある

ErrorLog

エラーログの保存場所を指定

CustomLog

ログファイルの吐き出し場所を指定

LogFormat

CustomLogの出力フォーマットを決定

RewriteRule

URLの書き換え

使用条件

  • RewriteEngine Onを前に呼び出す
  • Options FollowSymLinks がディレクトリで記述
RewriteRule Pattern Substitution [flags]
Pattern:Pathにマッチする条件を正規表現で記載(相対パス)
Substitution:書き換え後のパスを指定(フラグのみの場合は"-")
flags:[R]→リダイレクト [L]→処理終了 [F]→403エラー 

RewriteEngine On
RewriteRule rewrite-test/index.html /apache/rewrite-test/tmp.html 

RewriteBase /apache/rewrite-test/
RewriteRule rewrite-test/index.html tmp.html 
# 共通部をRewriteBaseで記載することでURLの省略可能

rewriteログの出力

# ver2.4以上
LogLevel warn rewrite:trace8

後方参照
( )でグループ化した部分が後方で$N(1-9)で取得が可能

RewriteRule rewrite-test/imgs/(\d{3}).jpg imgs/$1.png

RewriteCond

直下のリライトルールの実行条件を記載する

RewriteCond TestString CondPatter
TestString:テスト文字列
CondPatter:正規表現で表す。()を使うと、後方参照として%1~%9を使用可能

RewriteCond %{HTTP_HOST} ^localhost
RewriteRule .? - [F] #適応
RewriteRule rewrite-test/imgs/(\d{3}).jpg imgs/$1.png #直下じゃないため適応されない

# ...url...?p=file1.htmlなどを取得する方法
RewriteCond %{QUERY_STRING} p=(.+)
RewriteRule rewrite-test/sub1/ sub1/%1? 

# sub2にファイルやディレクトリがない場合はsub1から取得する際の書き方
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule rewrite-test/sub2/(.+) sub1/$1 

Webp画像への変更

img,pngで依頼が来た際に、webpが見れるブラウザであれば、webpで返す

AddType image/webp .webp
RewriteCond %{HTTP_ACCEPT} image/webp
RewriteCond /Applications/MAMP/htdocs/fullstack-webdev/070_Apacheの基礎/rewrite-test/imgs/$1.webp -f
RewriteRule /?imgs/(.*)\.(jpe?g|png) imgs/$1.webp

サブドメインの追加

/private/etc/hostsに下記が追加されている前提

127.0.0.1 dev.local
127.0.0.1 www.dev.local
127.0.0.1 vhost.dev.local
RewriteCond %{HTTP_HOST} ^www\.dev\.local
# RewriteRule (.+) http://dev.local:8888/$1 こっちでも可
RewriteRule .? http://dev.local:8888%{REQUEST_URI} [R=301]

RewriteCond %{HTTP_HOST} ^vhost\.dev\.local
RewriteCond %{REQUEST_URI} !^/apache/rewrite-test/vhost/ # 無限ループ対策
RewriteRule (.*) vhost/$1

gzipの使用

リクエスト側のheaderでAccept-Encoding: gzip, deflate, brとgzipの受け入れが可能な場合、
以下で設定が可能
(mod_deflate, filter_moduleのモジュールは有効でないといけない)

<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/css text/javascript
</IfModule>

DeflateCompressionLevel 9
# 1 (低圧縮) から 9 (高圧縮)

キャッシュ

commang + shiht + rでキャッシュを使わず再リクエストが可能

<IfModule mod_expires.c>
  ExpiresActive On
  <FilesMatch "\.(png|jpe?g|gif|css|js)$">
    ExpiresDefault "access plus 6 months"
  </FilesMatch>
</IfModule>

KeepAlive

HTTPの持続的な接続を有効にする

KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 1
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?