class documentation

class ProxyMiddleware:

View In Hierarchy

Proxy requests under a path to an external server, routing other requests to the app.

This middleware can only proxy HTTP requests, as HTTP is the only protocol handled by the WSGI server. Other protocols, such as WebSocket requests, cannot be proxied at this layer. This should only be used for development, in production a real proxy server should be used.

The middleware takes a dict mapping a path prefix to a dict describing the host to be proxied to:

app = ProxyMiddleware(app, {
    "/static/": {
        "target": "http://127.0.0.1:5001/",
    }
})

Each host has the following options:

target:
The target URL to dispatch to. This is required.
remove_prefix:
Whether to remove the prefix from the URL before dispatching it to the target. The default is False.
host:
"<auto>" (default):
The host header is automatically rewritten to the URL of the target.
None:
The host header is unmodified from the client request.
Any other value:
The host header is overwritten with the value.
headers:
A dictionary of headers to be sent with the request to the target. The default is {}.
ssl_context:
A ssl.SSLContext defining how to verify requests if the target is HTTPS. The default is None.

In the example above, everything under "/static/" is proxied to the server on port 5001. The host header is rewritten to the target, and the "/static/" prefix is removed from the URLs.

New in version 0.14.
Parameters
appThe WSGI application to wrap.
targetsProxy target configurations. See description above.
chunk​_sizeSize of chunks to read from input stream and write to target.
timeoutSeconds before an operation to a target fails.
Method __call__ Undocumented
Method __init__ Undocumented
Method proxy​_to Undocumented
Instance Variable app Undocumented
Instance Variable chunk​_size Undocumented
Instance Variable targets Undocumented
Instance Variable timeout Undocumented
def __call__(self, environ, start_response):

Undocumented

Parameters
environ:WSGIEnvironmentUndocumented
start​_response:StartResponseUndocumented
Returns
t.Iterable[bytes]Undocumented
def __init__(self, app, targets, chunk_size=2<<13, timeout=10):

Undocumented

Parameters
app:WSGIApplicationUndocumented
targets:t.Mapping[str, t.Dict[str, t.Any]]Undocumented
chunk​_size:intUndocumented
timeout:intUndocumented
def proxy_to(self, opts, path, prefix):

Undocumented

Parameters
opts:t.Dict[str, t.Any]Undocumented
path:strUndocumented
prefix:strUndocumented
Returns
WSGIApplicationUndocumented
app =

Undocumented

chunk_size =

Undocumented

targets =

Undocumented

timeout =

Undocumented