
설정을 하려고 하는데자체 관리형 Docker appsearch 인스턴스, kibana 및 elasticsearch와 함께 uvicorn python 앱으로 쿼리되고 nginx 웹 서버로 프록시됨
현재 문제는 appsearch 로그에 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-헤더를 기록하기 시작했습니다 =)