Basic HTTP Proxy

  • class werkzeug.middleware.httpproxy.ProxyMiddleware(_app, targets, chunk_size=16384, timeout=10)
  • Proxy requests under a path to an external server, routing otherrequests to the app.

This middleware can only proxy HTTP requests, as that is the onlyprotocol handled by the WSGI server. Other protocols, such aswebsocket requests, cannot be proxied at this layer. This shouldonly be used for development, in production a real proxying servershould be used.

The middleware takes a dict that maps a path prefix to a dictdescribing the host to be proxied to:

  1. app = ProxyMiddleware(app, {
  2. "/static/": {
  3. "target": "http://127.0.0.1:5001/",
  4. }
  5. })

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 itto the target. The default is False.
  • host:
    • "<auto>" (default):
    • The host header is automatically rewritten to the URL of thetarget.
    • 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 thetarget. The default is {}.
  • ssl_context:
  • A ssl.SSLContext defining how to verify requests if thetarget is HTTPS. The default is None.In the example above, everything under "/static/" is proxied tothe server on port 5001. The host header is rewritten to the target,and the "/static/" prefix is removed from the URLs.

Parameters:

  • app – The WSGI application to wrap.
  • targets – Proxy target configurations. See description above.
  • chunk_size – Size of chunks to read from input stream andwrite to target.
  • timeout – Seconds before an operation to a target fails.

New in version 0.14.