문제상황
해결방법
적용과정
@app.before_request
def block_method():
if 'x-real-ip' in request.headers:
ip=request.headers['x-real-ip']
else:
ip=request.environ.get('REMOTE_ADDR')
print("Current IP Address:",ip,flush=True)
print("Current Process:",os.getpid(),flush=True)
splited_ip=ip.split('.')
ip_range=splited_ip[0]+'.'+splited_ip[1]
if ip not in app.config['GOOD_IP_LIST'] and ip_range not in app.config['GOOD_IP_RANGE']:
abort(403)
*위는 app에 접근하는 사용자의 ip를 추출하여 print를 통해 로깅한다.이때 print옵션에 flush=True를 줘야 로그에 출력된다.
gunicorn --worker-class=gevent --worker-connections=1000 --workers 9 --bind unix:/run/imageus_back.sock --log-level=debug --capture-output --error-logfile error.log --max-requests 1000 --max-requests-jitter 1000 --timeout 5 -m 007 app:create_app()
*log-level은 디버그레벨까지 설정,error-logfile은 로그 파일의 이름을 설정,capture-output는 print로 출력시킬 로그 설정
*tail -f는 가장 최근 10줄부터 모니터링하는 명령어 이다.파일 이름은 gunicorn의 error-logfile인자에 입력했던 파일 이름을 넣어준다.
*flush옵션을 넣어줬던 print를 통해 사용자가 요청마다 사용자의 ip를 출력하도록 로깅을 구현해놨다.
결과