4
3

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.

Azure Application GatewayでX-Forwarded-For ヘッダーを書き換える

Posted at

Application GatewayではHTTPヘッダーを書き換えることができます。
デフォルトではX-Forwared-Forヘッダーは クライアントIPアドレス:ポート番号 となっていますが、ポート番号を表示させずにクライアントIPアドレスのみに書き換えてみました。
手順は既にApplication Gateway構築済みでバックエンドにWebサーバが稼働している状態を前提としています。

環境

  • Azure Application Gateway Standard v2
  • CentOS v8.2
  • Apache httpd v2.4.37

Webサーバ準備

  • httpdをインストールして起動します
$ sudo yum install -y httpd
$ sudo systemctl start httpd
  • X-Forwared-Forログ出力設定をします
$ sudo vi /etc/httpd/conf/httpd.conf
[Before]
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
[After]
LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
$ sudo systemctl reload httpd
  • この状態でApplication Gateway経由でアクセスし、アクセスログを確認してみます
$ sudo tail -f /var/log/httpd/access_log
aaa.bbb.ccc.ddd:27107 10.0.0.38 - - [08/Mar/2021:10:42:17 +0000] "GET / HTTP/1.1" ~~省略~~
  • こんな感じで クライアントIPアドレス:ポート番号 が表示されます。ここからポート番号を削除したいと思います。

Application Gateway HTTPヘッダー書き換え

  • Azure PortalのApplication Gateway左バーより、[書き換え]を選択します。
    image.png

  • [書き換えセット]を選択します。
    image.png

  • [書き換えセット名]を入力します。
    image.png

  • 関連付けるルールを選択し[次へ]を選択します。
    image.png

  • [書き換え規則の追加]を選択します。
    image.png

  • [書き換えルール名]を入力します。
    image.png

  • [実行]を選択します。
    image.png

  • 以下のように入力し、書き換えセットを作成します。
    image.png

  • 再度アクセスしてポート情報が消えていればOKです。

- aaa.bbb.ccc.ddd - - [08/Mar/2021:10:49:12 +0000] "GET / HTTP/1.1" ~~省略~~
4
3
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
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?