Docs: Nginx + websockets
Nginx requires that we set the Connection/Upgrade headers explicitly in
the config, otherwise Nginx assumes that they shouldn't be forwarded.
More info:
@@ -247,6 +247,8 @@ You should ensure that the `X-Forwarded-For` and `X-Forwarded-Proto` headers are
Here's how a simple Nginx configuration might look. This example includes setting proxy headers, and using a UNIX domain socket to communicate with the application server.
+It also includes some basic configuration to forward websocket connections. For more info on this, check [Nginx recommendations][nginx_websocket].
+
```conf
http {
server {
@@ -259,6 +261,8 @@ http {
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
+ proxy_set_header Upgrade $http_upgrade;
+ proxy_set_header Connection $connection_upgrade;
proxy_redirect off;
proxy_buffering off;
proxy_pass http://uvicorn;
@@ -270,6 +274,11 @@ http {
}
}
+ map $http_upgrade $connection_upgrade {
+ default upgrade;
+ '' close;
+ }
+
upstream uvicorn {
server unix:/tmp/uvicorn.sock;
}
@@ -309,5 +318,6 @@ It also possible to use certificates with uvicorn's worker for gunicorn
$ gunicorn --keyfile=./key.pem --certfile=./cert.pem -k uvicorn.workers.UvicornWorker example:app
```
+[nginx_websocket]: https://nginx.org/en/docs/http/websocket.html
[letsencrypt]: https://letsencrypt.org/
[mkcert]: https://github.com/FiloSottile/mkcert
