7
8

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 5 years have passed since last update.

今更ながらX-Forwarded-For ヘッダーを設定してしてみた

Last updated at Posted at 2019-07-22

アプリケーションの前にELB(ALB CLB)がいる場合、何も設定しないと、接続元のIPアドレスはELBのIPアドレスになってしまい、実際誰がアクセスしてきたか、クライアントのアドレスがわかりません。

そのために、X-Forwarded-For ヘッダーを記述する必要がある。

ELB アクセスログにクライアント IP アドレスをキャプチャする方法を教えてください。

今更ながら、試して見た。

  • 環境
  • Amazon Linux2
  • Apache
  • ALB
  • クライアント iphone→MACへデザリング

まずは、S3に転送している通常にログを確認してみる。

109.111.159.*:43514 10.0.2.218:80

/etc/httpd/conf/httpd.confを開き、LogFormat セクションで、次のように %{X-Forwarded-For}i を追加します。

httpd.conf
    ...
    LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
    ...

リロードします。

systemctl reload httpd

クライアントからアクセス。
念のため、デザリングしているMACとiPhone両方からアクセスしてみる。
今回はデザリングしているので、ソースアドレスはMACとiPhone同じになるはずだ。

ALBはS3へ5分ごとに発行されるので待って確認してみる。

MAC

126.182.77.*:20817 10.0.2.218:80 

iPhone

126.182.77.*:20817 10.0.2.218:80 
7
8
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
7
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?