LoginSignup
13
15

More than 1 year has passed since last update.

.git 等隠れファイルやディレクトリへの Web サーバー側からのアクセスを禁止 (Apache & Nginx)

Last updated at Posted at 2017-06-14

Git submodule を使った開発と、自動デプロイをしたいという要件がでてきた。

Submodule を使う場合、 git fetch && GIT_WORK_TREE=[WEB領域] git checkout -f なんて git ディレクトリを外に出すような芸当はできないので、 .git ディレクトリを Web 領域に入れないといけない場合があります。

そんな時、 Apache & Nginx で、.git ディレクトリや、 git 関連のファイルを Web 上からはアクセス出来ないようにしてもらうための設定 config です。

Apache の設定

RedirectMatch で行う方法

.git ディレクトリやファイルを禁止

RedirectMatch 404 /\.git

ディレクトリ内全てを 404 表示

RedirectMatch 404 /

おまけ:「.」で始まる隠れファイル全部を隠す。

RedirectMatch 404 /\..*$

mod_access で行う方法

httpd.conf の 480行目辺りにある <Files ~ "^\.ht"> な設定の後ぐらいに書いておいても良いかもです。

# .git から始まるファイルへのアクセスを禁止
<Files ~ "^\.git">
    Order allow,deny
    Deny from all
</Files>

# .git/ フォルダ以下のファイル・ディレクトリへのアクセスを禁止
<Directorymatch "^/.*/\.git/">
    Order deny,allow
    Deny from all
</Directorymatch>

Nginx の設定

.git ディレクトリやファイルのアクセス禁止。

server {
# 省略
	
	location ~ /\.git {
	    return 404;
	}
}

隠しファイル全体 (.htaccess, .git .svn など) アクセス禁止

server {
# 省略
	
	location ~ /\. {
	    return 404;
	}
}
13
15
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
13
15