クライアントとバックエンドサーバー間でPROXYプロトコルv1またはv2をサポートするNGINXサーバーをデプロイすることで、クライアントの実際のIPアドレスを確実に保持できます。 これは、ログの記録と監査、アクセス制御と認証、セキュリティ、パフォーマンスの最適化などの主要なシナリオで重要です。 クライアントIPアドレスを保存すると、システムのセキュリティ、信頼性、ユーザーエクスペリエンスが向上します。
シナリオ
バックエンドサービスが伝送制御プロトコル (TCP) を使用するeコマースWebサイトを実行します。 Webサイトへの訪問者のアクセス制御、正確なロギング、および位置情報を実装するには、オリジンサーバー上のクライアントの実際のIPアドレスを取得する必要があります。 NGINXはPROXYプロトコルをサポートしています。 これを利用するには、NGINXを設定してPROXYプロトコルクライアントの実際のIPアドレスを含むPROXYプロトコルヘッダーを解析するようにアプリケーションを設定します。
使用上の注意
NGINXのインストール
# Install the dependencies that are required to compile NGINX.
yum -y install gcc gcc-c++ autoconf automake
yum -y install zlib zlib-devel openssl openssl-devel pcre-devel
# Download the installation package.
wget http://nginx.org/download/nginx-1.20.0.tar.gz
# Decompress the source package.
tar -zxvf nginx-1.20.0.tar.gz
# Go to the NGINX directory.
cd nginx-1.20.0
# Configure the settings for NGINX compilation and installation. Make sure that you include --with-stream in the command.
./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --with-http_stub_status_module --with-http_gzip_static_module --with-stream
# Compile NGINX.
make
# Install NGINX.
make install
PROXYプロトコルv1/v2
をサポートするようにNGINXを設定する
手順1: 設定ファイルの変更
NGINXがPROXYプロトコル
をサポートするように設定するには、proxy_protocol
パラメーターをserver
ブロックのlisten
ディレクティブに追加するだけです。 詳細については、「PROXYプロトコルの受け入れ」をご参照ください。 例:
http {
log_format combined '$proxy_protocol_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
#...
server {
server_name localhost;
listen 80 proxy_protocol;
listen 443 ssl proxy_protocol;
ssl_certificate /etc/nginx/ssl/public.example.com.pem;
ssl_certificate_key /etc/nginx/ssl/public.example.com.key;
location /app/ {
proxy_pass http://backend1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $proxy_protocol_addr;
proxy_set_header X-Forwarded-For $proxy_protocol_addr;
}
}
}
stream {
log_format basic '$proxy_protocol_addr - $remote_user [$time_local] '
'$protocol $status $bytes_sent $bytes_received '
'$session_time';
#...
server {
listen 8080 ssl proxy_protocol;
ssl_certificate /etc/nginx/ssl/cert.pem;
ssl_certificate_key /etc/nginx/ssl/cert.key;
proxy_pass esa.example.com:8080;
proxy_protocol on;
}
}
手順2: TCP/UDPプロキシアプリケーションの作成
ESAコンソールにアクセスし、Webサイトの詳細ページに移動します。 左側のナビゲーションツリーで、[TCP/UDP プロキシ] > [設定] を選択します。 次に、[アプリケーションを作成] をクリックします。 表示されるページで、[プロトコル] ドロップダウンリストから [TCP] を選択し、[クライアントIP を渡す] ドロップダウンリストから [PROXY Protocol v1]
または [PROXY Protocol v2]
を選択します。 必要に応じて他のパラメーターを設定します。
設定結果の確認
HTTPサーバーを起動してファイルを提供します。
python -m SimpleHTTPServer 8080
curlコマンドを実行して、HTTPサーバーへのTCP要求をシミュレートします。
curl -i "https://esa.example.com:8080"
NGINXサーバーのログを表示し、クライアントの実際のIPアドレスを取得できます。
PROXYプロトコルに対するHAProxyのサポートについては、「プロキシプロトコルを使用してクライアントのIPアドレスを保持する」をご参照ください。