This project has been archived by its maintainers, and is no longer receiving any updates.
Django HTTP/2 Server Push redirects
A Django middleware that adds a HTTP/2 Server Push header to plain Django redirect responses.
This approach requires Django to be proxied by a server with
HTTP/2 Server Push support. E.g. nginx>=1.13.9, apache2>=2.4.26 with
mod_http2 enabled or a CDN services like Cloudflare.
Installation
This package is available on PyPI and can be installed with pip:
pip install django-push-redirect
Configuration
First configure your webserver to enable HTTP/2 and enable server push.
This looks something like this for nginx:
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
...
location @python {
proxy_set_header X-Forwarded-Proto $scheme;
http2_push_preload on;
...
}
}
The configuration for Apache and other servers/services is left as an exercise for the reader (in other words, I don't know, please contribute if you do!).
Now make sure Django is able to detect if a request is secure
by configuring the SECURE_PROXY_SSL_HEADER setting, e.g.:
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
Then add 'push_redirect.middleware.Http2ServerPushRedirectMiddleware'
to your project's MIDDLEWARE, make sure it's before Django's
CommonMiddleware:
MIDDLEWARE = [
...
'push_redirect.middleware.Http2ServerPushRedirectMiddleware',
'django.middleware.common.CommonMiddleware',
...
]
This middleware adds the Link rel=preload header on redirect
responses that should be preloaded.
If everything is configured correctly you should see that redirects no longer require an extra request the the webserver.
Response opt-out
It is possible for a response to explicitly opt-out from the having
a preload header added. This is done by setting allow_push_redirect
to False on the response object, e.g.:
def opt_out(request):
response = HttpResponseRedirect("/")
response.allow_push_redirect = False
return response
Inspiration / References
- https://twitter.com/simonw/status/1047865898717966337
- https://www.ctrl.blog/entry/http2-push-redirects
- https://www.nginx.com/blog/nginx-1-13-9-http2-server-push/
- https://httpd.apache.org/docs/2.4/howto/http2.html#push
- https://www.cloudflare.com/website-optimization/http2/serverpush/
- https://code.djangoproject.com/ticket/29925
Metadata
Release files for django-push-redirect 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django-push-redirect-1.1.0.tar.gz | 8.7 kB | Without provenance | Details for django-push-redirect-1.1.0.tar.gz |
Release files / django-push-redirect-1.1.0.tar.gz
| Download URL | django-push-redirect-1.1.0.tar.gz |
|---|---|
| Size | 8.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3c6f2402a5b9a7b4950b96677529a31a805268f3a67810a8b64cd767a5a89979
|
|
BLAKE2b-256 checksum How to use checksums |
5ab711fae3dd81b2f5ee69022ac140765d524a56d5e0fa36fecbf127753877ab
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.0 CPython/3.6.6
|
Security for django-push-redirect 1.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.