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?

nginx AI回答付き

0
Last updated at Posted at 2026-06-20

nginx IP or リファラー

user  nginx;
worker_processes  auto;
error_log  /dev/stderr notice;
events { worker_connections  1024; }

http {
    log_format  custom_header_log  'STATUS: "$status" | ALLOW_IP: "$remote_addr" | XFF: "$http_x_forwarded_for" | REQ: "$request" | HOST: "$http_host" | REF: "$http_referer"';
    access_log  /var/log/nginx/access.log  custom_header_log;

    # 多段プロキシの信頼設定
    set_real_ip_from 10.0.0.0/16;
    set_real_ip_from 192.168.2.0/24;
    real_ip_header X-Forwarded-For;
    real_ip_recursive on;

    # IPアドレスのホワイトリスト判定
    map $remote_addr $ip_allowed {
        default           "0";
        "192.168.2.107"   "1";
        "203.0.113.50"    "1";
    }

    server {
        listen       80;

        # --- ここから追記 ---
        # favicon.icoへのアクセスはアクセスログもエラーログも出さずに204(空コンテンツ)を返す
        location = /favicon.ico {
            log_not_found off;      # ファイルがなくてもエラーログに記録しない
            access_log off;         # アクセスログに出力しない
            return 204;             # 204 No Content(成功だけど中身なし)を返す
        }

        location / {
            #valid_referers ~http(s)?://192\.168\.2\.190:1900;

            set $access_status "0";

            if ($ip_allowed = "1") {
                set $access_status "1";
            }

             # 2. 【ここを修正】リファラー文字列を直接正規表現でチェック
            # http://192.168.2.190:1900 から始まっていれば「アクセス許可(1)」にする
            if ($http_referer ~* "^https?://192\.168\.2\.190:1900") {
                set $access_status "1";
            }



            if ($access_status = "0") {
                return 403;
            }

            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
    }
}
  • set_real_ip_from (信頼するプロキシの指定)
     nginxset_real_ip_from 10.0.0.0/16;
     set_real_ip_from 192.168.2.0/24;
    意味: 接続元として信頼するプロキシサーバーのIPアドレス(またはネットワーク帯)を指定
    解説: 「ここに書かれたIPからアクセスがあった場合のみ、後ろに続く設定(IPの置き換え)を実行する」という条件になります。社内ネットワークやVPC内のロードバランサーのIP帯を指定するのが一般的

  • real_ip_header (IPが隠されている場所の指定)
    意味: 本当のクライアントIPが、どのHTTPヘッダーに格納されているかを指定します
    解説: 多くのプロキシサーバーは、経由したIPアドレスを X-Forwarded-For というヘッダーに「クライアントIP, プロキシ1, プロキシ2...」のようにカンマ区切りで追記していきます。Nginxに対して「このヘッダーの中身を見て本当のIPを探しなさい」と指示しています。

  • real_ip_recursive on; (再帰的なIP探索の有効化)
    意味: X-Forwarded-For ヘッダーを右側(最新の経由地)から順番に遡り、「信頼できないIP(=一般のクライアント)」が初めて現れた時点で探索をストップし、そのIPを正式なクライアントIPとして採用する設定です。

off(デフォルト)の場合、
ヘッダーの「一番右側」にあるIP(直前のプロキシ)しか見ません。

on にすることで、
複数のプロキシ(例: Cloudflare ➔ 社内ロードバランサー ➔ 本番Nginx)を通過する多段プロキシ環境であっても、社内ネットワーク(10.0.0.0/16 など)をすべてスキップして、インターネット上の本当の訪問者のIPを正しく $remote_addr に代入できます。

 kubectl create configmap nginx-config --from-file=nginx.conf=nginx.conf --dry-run=client -o yaml | kubectl apply -f -
 
 kubectl rollout restart deployment nginx-deployment
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?