Skip to content

Commit

Permalink
Fix ReverseProxied middleware and add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
RobbeSneyders committed Feb 10, 2024
1 parent f8f461c commit 69e1242
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
29 changes: 29 additions & 0 deletions docs/cookbook.rst
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,32 @@ Starlette. You can add it to your application, ideally in front of the ``Routing
:noindex:

.. _CORSMiddleware: https://www.starlette.io/middleware/#corsmiddleware

Reverse Proxy
-------------

When running behind a reverse proxy with stripped path prefix, you need to configure your
application to properly handle this.

Single known path prefix
''''''''''''''''''''''''

If there is only a single known prefix your application will be running behind, you can simply
pass this path prefix as the `root_path` to your ASGI server:

.. code-block:: bash
$ uvicorn run:app --root-path <root_path>
.. code-block:: bash
$ gunicorn -k uvicorn.workers.UvicornWorker run:app --root-path <root_path>
Dynamic path prefix
'''''''''''''''''''

If you are running behind multiple proxies, or the path is not known, you can wrap your
application in a `ReverseProxied` middleware as shown in `this example`_.

.. _this example: https://github.com/spec-first/connexion/tree/main/examples/reverseproxy
8 changes: 4 additions & 4 deletions examples/reverseproxy/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send):
root_path = value.decode()
break
if root_path:
scope["root_path"] = "/" + root_path.strip("/")
path_info = scope.get("PATH_INFO", scope.get("path"))
if path_info.startswith(root_path):
scope["PATH_INFO"] = path_info[len(root_path) :]
root_path = "/" + root_path.strip("/")
scope["root_path"] = root_path
scope["path"] = root_path + scope.get("path", "")
scope["raw_path"] = root_path.encode() + scope.get("raw_path", "")

scope["scheme"] = scope.get("scheme") or self.scheme
scope["server"] = scope.get("server") or (self.server, None)
Expand Down
8 changes: 4 additions & 4 deletions tests/api/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send):
root_path = value.decode()
break
if root_path:
scope["root_path"] = "/" + root_path.strip("/")
path_info = scope.get("PATH_INFO", scope.get("path"))
if path_info.startswith(root_path):
scope["PATH_INFO"] = path_info[len(root_path) :]
root_path = "/" + root_path.strip("/")
scope["root_path"] = root_path
scope["path"] = root_path + scope.get("path", "")
scope["raw_path"] = root_path.encode() + scope.get("raw_path", "")

scope["scheme"] = scope.get("scheme") or self.scheme
scope["server"] = scope.get("server") or (self.server, None)
Expand Down

0 comments on commit 69e1242

Please sign in to comment.