Skip to content

Instantly share code, notes, and snippets.

@c834606877
Last active November 15, 2023 10:24
Show Gist options
  • Save c834606877/aaceea60f349b7169bc31a9ff0676227 to your computer and use it in GitHub Desktop.
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.
@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!")
@c834606877
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment