LoginSignup
8
9

More than 5 years have passed since last update.

GitLab CE OmnibusバンドルのnginxにIPアクセス制限機能を付ける

Last updated at Posted at 2015-01-31

GitLab CE Omnibus(以下gitlab)にバンドルされているnginxは/etc/gitlab/gitlab.rbファイルで適度にカスタマイズができるようになっていますが、痒いところに手が届かない場合もあります。

例えばIPによるアクセス制限がかけられなかったり。
ということで、成功法ではないと思いますが、IPアクセス制限を仕掛けられるようにちょこっといじってみました。試した環境はCentOS6.6 + GitLab7.7.2です。

gitlab-ctl reconfigureで作成されるnginxの設定ファイル、

  • /var/opt/gitlab/nginx/conf/gitlab-http.conf

のテンプレートは、

  • /opt/gitlab/embedded/cookbooks/gitlab/templates/default/nginx-gitlab-http.conf.erb

これです。このファイルをエディタで開いて、

  location / {
    ## Serve static files from defined root folder.
    ## @gitlab is a named location for the upstream fallback, see below.
    try_files $uri $uri/index.html $uri.html @gitlab;
  }

このlocationブロックを、

  location / {
    <% if @ip_access_control %>
      <% @allow_ips.each do |allow_ip| %>
    allow <%= allow_ip %>;
      <% end %>
      <% @deny_ips.each do |deny_ip| %>
    deny <%= deny_ip %>;
      <% end %>
    <% end %>
    ## Serve static files from defined root folder.
    ## @gitlab is a named location for the upstream fallback, see below.
    try_files $uri $uri/index.html $uri.html @gitlab;
  }

こんなふうに修正して保存します。
次に/etc/gitlab/gitlab.rbをエディタで開いて、

nginx['ip_access_control'] = true
nginx['allow_ips'] = ["アクセス許可したいIPその1","アクセス許可したいIPその2" ....]
nginx['deny_ips'] = ["アクセス拒否したいIPその1","アクセス拒否したいIPその2" ....]

ip_access_controlをtrueにして、allow_ips、deny_ipsにアクセス許可/拒否したいIPを列挙して保存します。
例えば、

nginx['ip_access_control'] = true
nginx['allow_ips'] = ["172.16.1.0/24"]
nginx['deny_ips'] = ["all"]

こんな設定をしてgitlab-ctl reconfigure && gitlab-ctl restartをすれば172.16.1.0/24からのアクセスを許可し、それ以外は拒否するようになります。

この修正方法でBasic認証も付けられそうな気がします。

GitLab CIを使う場合はそっちのテンプレートもそれ相応に修正すればいいのかもしれません(使ってないのでよくわからないです)。

ところでエンタメ系Webサービス開発に興味がある方はぜひ連絡をおねがいs(略)

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