X-Forwarded-For Proxy Fix¶
This module provides a middleware that adjusts the WSGI environ based on
X-Forwarded- headers that proxies in front of an application may
When an application is running behind a proxy server, WSGI may see the request as coming from that server rather than the real client. Proxies set various headers to track where the request actually came from.
This middleware should only be used if the application is actually behind such a proxy, and should be configured with the number of proxies that are chained in front of it. Not all proxies set all the headers. Since incoming headers can be faked, you must set how many proxies are setting each header so the middleware knows what to trust.
- class werkzeug.middleware.proxy_fix.ProxyFix(app, x_for=1, x_proto=1, x_host=0, x_port=0, x_prefix=0)¶
Adjust the WSGI environ based on
X-Forwarded-that proxies in front of the application may set.
You must tell the middleware how many proxies set each header so it knows what values to trust. It is a security issue to trust values that came from the client rather than a proxy.
The original values of the headers are stored in the WSGI environ as
werkzeug.proxy_fix.orig, a dict.
app (WSGIApplication) – The WSGI application to wrap.
x_for (int) – Number of values to trust for
x_proto (int) – Number of values to trust for
x_host (int) – Number of values to trust for
x_port (int) – Number of values to trust for
x_prefix (int) – Number of values to trust for
- Return type
from werkzeug.middleware.proxy_fix import ProxyFix # App is behind one proxy that sets the -For and -Host headers. app = ProxyFix(app, x_for=1, x_host=1)
Changed in version 1.0: Deprecated code has been removed:
num_proxiesargument and attribute.
The environ keys
Changed in version 0.15: All headers support multiple values. The
num_proxiesargument is deprecated. Each header is configured with a separate number of trusted proxies.
Changed in version 0.15: Original WSGI environ values are stored in the
orig_http_hostare deprecated and will be removed in 1.0.
Changed in version 0.15: Support
Changed in version 0.15: