1
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.

nginxでアクセス可能なremote ipを制限する

Posted at

概要

  • nginxでIP制限をする場合の設定方法をまとめます
  • IPはサンプルで適当なものを設定しておりますmm

基本の設定

IP1.2.3.4のみリクエストを通す例です。

server {
    location / {
        root /usr/share/nginx/html;
        # 通過させて良いIPを指定
        allow 1.2.3.4;
        deny all;
    }
}

記述順にルールが適用されるようです。
denyとallowの順番が逆だとすべてのリクエストがdenyされます😇

proxyを挟む場合

nginxのallow, denyのIPは直前のサーバのIPが使用されます。
そのため

client -> proxy -> nginx(server)

という構成の場合、clientのIPではなく、proxyのIPが使用されます。

(proxyでIP制限すれば良いとかは置いておいて)
IP制限はclientのIPでかけたいのでなんとかしたい。

そんなときは、real_ip_headerset_real_ip_fromを設定すればOK!

server {
    # X-Forwarded-Forヘッダの末尾のIPを使用する
    real_ip_header X-Forwarded-For;
    # ここで設定したIPから来たリクエストだけreal_ip_headerを適用する
    set_real_ip_from 5.6.7.8;

    location / {
        root /usr/share/nginx/html;
        allow 1.2.3.4;
        deny all;
    }
}
1
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
1
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?