
私はセットアップしようとしていますセルフマネージド Docker AppSearch インスタンス、kibanaとelasticsearchと連携し、uvicorn pythonアプリでクエリを実行し、nginxウェブサーバーでプロキシする
現在の問題は、appsearch ログに Python のデフォルトのユーザー エージェントと IP (つまり、python-requests/2.22.0 と LAN IP) が表示されることです。
リモート クライアントの正しい IP とユーザー エージェントを含む nginx カスタム ヘッダーを、kibana でクエリしやすい appsearch ログに転送したいと思います。
output.elasticsearch.headers
環境または filebeat.yml でカスタム ヘッダーを設定できる があることに気付きました。
これについて何かアイデアはありますか?
ありがとう。
答え1
まあ、ついにやりましたよ。
実際のクライアント IP とユーザー エージェントは静的ではなかったため、yml または環境変数を使用する必要も十分でもありませんでした。
まず、nginx.conf プロキシを次のように変更しました。
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-User-Agent $http_user_agent;
次に、各 uvicorn FastAPI メソッドに最初のrequest
パラメータを追加しました。
from fastapi import FastAPI
app = FastAPI()
# ...
@app.get("/search")
async def search_endpoint(request: Request):
# ... method implementation
search_endpoint は検索クラスを呼び出し、検索クラスは appsearch の Python クライアントを使用します。
import elastic_app_search
# ...
client = elastic_app_search.Client(api_key = XXX, base_endpoint = YYY, use_https=False)
そしてクライアントでヘッダーを更新します。
x_headers = {
'Connection': 'close',
'X-Forwarded-For': request.headers['X-Forwarded-For'],
'X-User-Agent': request.headers['X-User-Agent']
}
client.session.session.headers.update(x_headers)
# For current appsearch python client the repeated name was necessary
その後、アプリケーション ログにカスタム X ヘッダーが記録され始めました =)