Skip to content

Commit 5323dfc

Browse files
author
cak
committed
Add docs for overriding Server header with Uvicorn
- Added note to `frameworks.md` about disabling Uvicorn's default Server header using the `--no-server-header` option or `server_header=False` in `uvicorn.run()`. - Mentioned limitation when using Uvicorn via Gunicorn as described in the GitHub issue. - Updated documentation to close GitHub issue #143. Closes #143.
1 parent 35dc516 commit 5323dfc

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

docs/frameworks.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,29 @@
2525

2626
---
2727

28+
### Note: Overriding the `Server` Header in Uvicorn-based Frameworks
29+
30+
If you're using Uvicorn as the ASGI server (commonly used with frameworks like FastAPI, Starlette, and others), Uvicorn automatically injects a `Server: uvicorn` header into all HTTP responses by default. This can lead to multiple `Server` headers when using `Secure.py` to set a custom `Server` header.
31+
32+
To prevent Uvicorn from adding its default `Server` header, you can disable it by passing the `--no-server-header` option when running Uvicorn, or by setting `server_header=False` in the `uvicorn.run()` method:
33+
34+
```python
35+
import uvicorn
36+
37+
uvicorn.run(
38+
app,
39+
host="0.0.0.0",
40+
port=8000,
41+
server_header=False, # Disable Uvicorn's default Server header
42+
)
43+
```
44+
45+
If you're using Uvicorn via Gunicorn (e.g., with the `UvicornWorker`), note that this setting is not passed through automatically. In such cases, you may need to subclass the worker to fully override the `Server` header.
46+
47+
For more information, refer to the [Uvicorn Settings](https://www.uvicorn.org/settings/#http).
48+
49+
---
50+
2851
## aiohttp
2952

3053
**[aiohttp](https://docs.aiohttp.org)** is an asynchronous HTTP client/server framework for asyncio and Python. It's designed for building efficient web applications with asynchronous capabilities.

docs/headers/server.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ This can then be applied as part of your Secure headers configuration:
4242
secure_headers = Secure(server=server_header)
4343
```
4444

45+
### Special Considerations for Frameworks
46+
47+
Some frameworks like Uvicorn automatically inject a `Server` header. If you're using Uvicorn and need to override or remove this header, refer to the [framework integration guide](../frameworks.md) for specific instructions on how to disable Uvicorn's default `Server` header.
48+
4549
## **Resources**
4650

4751
- [MDN Web Docs: Server Header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Server)

0 commit comments

Comments
 (0)