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?

【IBM Cloud】CISとALBを使ってソースIPアドレスで割り振り先を制御してみた①

0
Last updated at Posted at 2025-12-24

はじめに

IBM CloudのCloud Internet Service(CIS)でX-Forwarded-For(XFF)にClient IPアドレスが格納され、Application Load Balancer(ALB)でXFFのヘッダー情報を元にL7のポリシーで割り振り先のPoolを制御する検証を行いました。

今回の記事は2つに分けており、こちらの記事は前半の記事となります。

  • 後半の記事:追って追記します。

前提条件

今回の検証には以下が前提条件となります。

  • IBM Cloudアカウント(IBMid)
  • Virtual Server for VPC(VSI)インスタンス 2台
  • Application Load Balancer for VPC(ALB)インスタンス
  • Cloud Internet Service(CIS)インスタンス
    • ドメインの取得

検証の流れ

VSIにnginxのインストール

前提条件の通り、環境として利用するVSIを用意してnginxをインストールします。なお、今回のVSIのOSはCentOSを利用しています。

VSIにログインし、以下のコマンドでnginxをインストールします。

sudo yum install -y nginx
結果
[root@moon-test-pool1 ~]# sudo yum install -y nginx
Last metadata expiration check: 0:16:18 ago on Thu 11 Dec 2025 05:58:43 AM UTC.
Dependencies resolved.
====================================================================================================================================================================================================================
 Package                                                  Architecture                                 Version                                                Repository                                       Size
====================================================================================================================================================================================================================
Installing:
 nginx                                                    x86_64                                       2:1.20.1-24.el9                                        appstream                                        36 k
Installing dependencies:
 centos-logos-httpd                                       noarch                                       90.8-3.el9                                             appstream                                       1.5 M
 nginx-core                                               x86_64                                       2:1.20.1-24.el9                                        appstream                                       570 k
 nginx-filesystem                                         noarch                                       2:1.20.1-24.el9                                        appstream                                       9.3 k

Transaction Summary
====================================================================================================================================================================================================================
Install  4 Packages

Total download size: 2.1 M
Installed size: 4.3 M
Downloading Packages:
(1/4): nginx-1.20.1-24.el9.x86_64.rpm                                                                                                                                               610 kB/s |  36 kB     00:00    
(2/4): nginx-filesystem-1.20.1-24.el9.noarch.rpm                                                                                                                                    322 kB/s | 9.3 kB     00:00    
(3/4): nginx-core-1.20.1-24.el9.x86_64.rpm                                                                                                                                          5.7 MB/s | 570 kB     00:00    
(4/4): centos-logos-httpd-90.8-3.el9.noarch.rpm                                                                                                                                      13 MB/s | 1.5 MB     00:00    
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                                18 MB/s | 2.1 MB     00:00     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                                                            1/1 
  Running scriptlet: nginx-filesystem-2:1.20.1-24.el9.noarch                                                                                                                                                    1/4 
  Installing       : nginx-filesystem-2:1.20.1-24.el9.noarch                                                                                                                                                    1/4 
  Installing       : nginx-core-2:1.20.1-24.el9.x86_64                                                                                                                                                          2/4 
  Installing       : centos-logos-httpd-90.8-3.el9.noarch                                                                                                                                                       3/4 
  Installing       : nginx-2:1.20.1-24.el9.x86_64                                                                                                                                                               4/4 
  Running scriptlet: nginx-2:1.20.1-24.el9.x86_64                                                                                                                                                               4/4 
  Verifying        : centos-logos-httpd-90.8-3.el9.noarch                                                                                                                                                       1/4 
  Verifying        : nginx-2:1.20.1-24.el9.x86_64                                                                                                                                                               2/4 
  Verifying        : nginx-core-2:1.20.1-24.el9.x86_64                                                                                                                                                          3/4 
  Verifying        : nginx-filesystem-2:1.20.1-24.el9.noarch                                                                                                                                                    4/4 

Installed:
  centos-logos-httpd-90.8-3.el9.noarch                  nginx-2:1.20.1-24.el9.x86_64                  nginx-core-2:1.20.1-24.el9.x86_64                  nginx-filesystem-2:1.20.1-24.el9.noarch                 

Complete!

また、statusも確認します。

systemctl status nginx
結果
[root@moon-test-pool1 ~]# sudo systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: disabled)
     Active: active (running) since Thu 2025-12-11 06:15:11 UTC; 23min ago
    Process: 63424 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
    Process: 63425 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
    Process: 63426 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
   Main PID: 63427 (nginx)
      Tasks: 3 (limit: 23129)
     Memory: 3.1M (peak: 3.2M)
        CPU: 25ms
     CGroup: /system.slice/nginx.service
             ├─63427 "nginx: master process /usr/sbin/nginx"
             ├─63428 "nginx: worker process"
             └─63429 "nginx: worker process"

Dec 11 06:15:11 moon-test-pool1 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Dec 11 06:15:11 moon-test-pool1 nginx[63425]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Dec 11 06:15:11 moon-test-pool1 nginx[63425]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Dec 11 06:15:11 moon-test-pool1 systemd[1]: Started The nginx HTTP and reverse proxy server.

index.htmlファイルの編集

Webコンテンツの置き場に移動してファイルを確認します。
index.htmlはALBやCISからアクセスされる実体ファイルですので、こちらが表示されていたら問題ないです。

cd /usr/share/nginx/html
ls -l
結果
[root@moon-test-pool1 ~]# cd /usr/share/nginx/html
[root@moon-test-pool1 html]# ls -l
total 12
-rw-r--r--. 1 root root 3971 Jun 19  2025 404.html
-rw-r--r--. 1 root root 4020 Jun 19  2025 50x.html
drwxr-xr-x. 2 root root   27 Dec 11 06:15 icons
lrwxrwxrwx. 1 root root   25 Jun 19  2025 index.html -> ../../testpage/index.html
-rw-r--r--. 1 root root  368 Jun 19  2025 nginx-logo.png
lrwxrwxrwx. 1 root root   14 Jun 19  2025 poweredby.png -> nginx-logo.png
lrwxrwxrwx. 1 root root   37 Jun 19  2025 system_noindex_logo.png -> ../../pixmaps/system-noindex-logo.png

こちらの動作を確認する際にわかりやすくするため、中身を置き換えておきます。

sudo vi index.html
moon-test-pool1 moon-test-pool1

Hello from VSI-1

log_formatの追加およびログの確認

index.htmlファイルの中身を変更した後、以下のコマンドにてローカルでアクセスできるか確認します。
先ほど設定した中身が表示され、nginxは正常に稼働していることがわかります。

curl http://localhost
結果
[root@moon-test-pool1 html]# curl http://localhost
moon-test-pool1 moon-test-pool1

Hello from VSI-1

次はnginxの設定ファイルを変更してXFFをログに出す設定を行います。
メイン設定ファイル(nginx.conf)へ移動します。

cd /etc/nginx
ls -l

ファイルを開きます。

sudo vi /etc/nginx/nginx.conf

httpブロックを探して、log_formatを追加します。

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    access_log  /var/log/nginx/access.log;
    ...
}
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
.
.
.

設定が問題ないか確認します。

確認
sudo nginx -t
結果
[root@moon-test-pool1 ~]# sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

その後、設定を反映します。

sudo systemctl reload nginx

設定が完了したら、ログのディレクトリに移動し、リアルタイムで表示させてみます。

ディレクトリ移動
cd /var/log/nginx
リアルタイムで表示
sudo tail -f access.log
結果
[root@moon-test-pool1 nginx]# sudo tail -f access.log
xxx.xxx.x.xxx - - [24/Dec/2025:06:41:04 +0000] "GET / HTTP/1.0" 200 50 "-" "-" "-"
xxx.xxx.x.xxx - - [24/Dec/2025:06:41:04 +0000] "GET / HTTP/1.0" 200 50 "-" "-" "-"
xxx.xxx.x.xxx - - [24/Dec/2025:06:41:09 +0000] "GET / HTTP/1.0" 200 50 "-" "-" "-"
xxx.xxx.x.xxx - - [24/Dec/2025:06:41:09 +0000] "GET / HTTP/1.0" 200 50 "-" "-" "-"
xxx.xxx.x.xxx - - [24/Dec/2025:06:41:14 +0000] "GET / HTTP/1.0" 200 50 "-" "-" "-"
xxx.xxx.x.xxx - - [24/Dec/2025:06:41:14 +0000] "GET / HTTP/1.0" 200 50 "-" "-" "-"
xxx.xxx.x.xxx - - [24/Dec/2025:06:41:19 +0000] "GET / HTTP/1.0" 200 50 "-" "-" "-"
xxx.xxx.x.xxx - - [24/Dec/2025:06:41:19 +0000] "GET / HTTP/1.0" 200 50 "-" "-" "-"
xxx.xxx.x.xxx - - [24/Dec/2025:06:41:24 +0000] "GET / HTTP/1.0" 200 50 "-" "-" "-"
xxx.xxx.x.xxx - - [24/Dec/2025:06:41:24 +0000] "GET / HTTP/1.0" 200 50 "-" "-" "-"

ALBの設定およびVSIと接続

次はVSIとALBを連携するため、ALBの設定としてバッグエンド・プールフロントエンド・リスナーを作成します。

まずはすでに作成済みのALBインスタンスにて、バックエンド・プール作成をクリックします。
スクリーンショット 2025-12-24 16.05.06.png
作成画面にて名前やヘルスポート番号を適宜入力し、作成をクリックします。なお、今回私はポート番号80にしています。
スクリーンショット 2025-12-24 16.13.52.png

バックエンド・プールが作成されたら、次はフロントエンド・リスナータブにてリスナーの作成をクリックします。
スクリーンショット 2025-12-24 16.15.09.png
デフォルトバックエンド・プールに先ほど作成したものを選択し、リスナーポート番号も同様に80を入力して作成します。そのほかはデフォルト設定にしています。
スクリーンショット 2025-12-24 16.16.40.png

最後に実際紐付けるVSIをメンバーとして追加します。バックエンド・プールタブにてメンバーの接続をクリックします。
スクリーンショット 2025-12-24 16.19.38.png
メンバーの接続をクリックします。
スクリーンショット 2025-12-24 16.19.49.png
コンピュート・デバイスタブにてサブネットを選択し、そのサブネットに紐づいているVSIを選択します。サーバーポートは同様に80を選択します。
設定が終わったら保存をクリックします。
スクリーンショット 2025-12-24 16.20.18.png

メンバー接続まで完了したらALBが再度更新されはじめます。
しばらく経つと正常性(Status)が緑色に変わり、ALBとVSIが接続されます。
スクリーンショット 2025-12-24 16.25.36.png

ALBからVSI(nginx)への通信確認

接続が完了したら、最後にALBからVSI(nginx)への通信を確認してみます。
まずはALBのホスト名を確認します。
スクリーンショット 2025-12-24 16.30.36.png
その後、以下のコマンドにてALB経由でnginxに到達できるかを確認してみると、問題なくファイルの中身が表示され、接続されていることがわかります。

curl http://<ALBのホスト名>
結果
[root@moon-test-pool1 ~]# curl http://xxxxxxx-jp-tok.lb.appdomain.cloud
moon-test-pool1 moon-test-pool1

Hello from VSI-1

また、上で設定したnginxのaccess.logでも通信元を確認してみます。
以下のコマンドを入力します。

sudo tail -f /var/log/nginx/access.log
結果
[root@moon-test-pool1 ~]# sudo tail -f /var/log/nginx/access.log
xxx.xxx.x.xxx - - [24/Dec/2025:07:36:18 +0000] "GET / HTTP/1.0" 200 50 "-" "-" "-"
xxx.xxx.x.xxx - - [24/Dec/2025:07:36:22 +0000] "GET / HTTP/1.0" 200 50 "-" "-" "-"
xxx.xxx.x.xxx - - [24/Dec/2025:07:36:23 +0000] "GET / HTTP/1.0" 200 50 "-" "-" "-"
xxx.xxx.x.xxx - - [24/Dec/2025:07:36:27 +0000] "GET / HTTP/1.0" 200 50 "-" "-" "-"
xxx.xxx.x.xxx - - [24/Dec/2025:07:36:28 +0000] "GET / HTTP/1.0" 200 50 "-" "-" "-"
xxx.xxx.x.xxx - - [24/Dec/2025:07:36:32 +0000] "GET / HTTP/1.0" 200 50 "-" "-" "-"
xxx.xxx.x.xxx - - [24/Dec/2025:07:36:33 +0000] "GET / HTTP/1.0" 200 50 "-" "-" "-"
xxx.xxx.x.xxx - - [24/Dec/2025:07:36:37 +0000] "GET / HTTP/1.0" 200 50 "-" "-" "-"
xxx.xxx.x.xxx - - [24/Dec/2025:07:36:38 +0000] "GET / HTTP/1.0" 200 50 "-" "-" "-"
xxx.xxx.x.xxx - - [24/Dec/2025:07:36:42 +0000] "GET / HTTP/1.0" 200 50 "-" "-" "-"

なお、ログの末端に記載の"-" "-" "-"がFXXの部分になります。まだFXXは設定していないため、こちらのログは正常です。

前半の検証はこれで完了です。

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?