Last active
November 15, 2023 10:24
-
-
Save c834606877/aaceea60f349b7169bc31a9ff0676227 to your computer and use it in GitHub Desktop.
Replace the Real IP in flask's logger when flask is behind of nginx or proxy.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@app.before_request | |
def before_request_func(): | |
from flask import redirect, request | |
real_ip = request.headers.get("X-Real-IP", "") | |
if real_ip and "127.0.0.1" == request.environ['REMOTE_ADDR']: | |
request.environ['REMOTE_ADDR'] = real_ip | |
#print("before_request executing!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The origional logger function is came from werkzeug's log_request functin in WSGIRequestHandler
https://github.com/pallets/werkzeug/blob/726eaa28593d859548da3477859c914732f012ef/src/werkzeug/serving.py#L414