Another way to have https
scheme in swagger page is to use SECURE_PROXY_SSL_HEADER
configuration.
Assuming that your Django REST API is sitting behind an Nginx that is doing SSL termination, you can let the Nginx forward X-Forwarded-Proto: https
to your Django application (Nginx might already forward this header by default depending on how you set things up). With the configuration below, your Django application will realize that it is behind a SSL terminating Nginx, and Django's internal function is_secure()
will return True
when the header is present. Refer to Django SSL Settings.
Once the is_secure()
returns True
, the swagger page scheme will automatically turn into https
.
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
I like this approach since it does not require any hard coding url or even configuring url from environment variables. Additionally, the is_secure()
function is used internally in other place as well so it is desirable to have the function work as it idealy should.