Alechan / microsimu

GNU General Public License v3.0
0 stars 1 forks source link

Forward HTTPS requests so it reaches django instead of mapping them internally to HTTP requests (see desc) #96

Closed Alechan closed 3 years ago

Alechan commented 3 years ago

Problem

All the URLS automatically generated in the BrowsableAPI docs and in the "url" attributes of the JSON responses are for HTTP protocol even though the site was accessed through a HTTPS protocol. This is because:

Alternative 1

Consider this alternative:

  1. Use https://github.com/evertramos/docker-compose-letsencrypt-nginx-proxy-companion as a replacement for the nginx running in host, and forward the "outside traffic" to this new nginx container
  2. Add docker's network from the point above as an extenral network in the docker compose of microsimu
  3. Add volumes for cert files (created by the docker compose in point 1) in microsimu's docker-compose so its nginx and gunicorn containers can access them

The path the request would take would be:

  1. HTTPS request from a user to our server
  2. it is caught by the NGINX from https://github.com/evertramos/docker-compose-letsencrypt-nginx-proxy-companion
  3. forward HTTPS to microsimu's nginx container
  4. forward HTTPS to microsimu's gunicorn container
  5. forward HTTPS in same container into django's app

Theoretically, the whole path would be through HTTPS requests.

Alternative 2

There's a way to make django "trust" HTTP requests if the right header is set. See https://docs.djangoproject.com/en/3.1/ref/settings/#std:setting-SECURE_PROXY_SSL_HEADER

EDIT: Chosen solution

I chose Alternative 2 and implemented it by configuring the host's nginx to set the header property "X-Forwarded-Porto" like this:

    proxy_set_header X-Forwarded-Porto $scheme;

And the containerized nginx to forward that header like this:

    proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;

And finally set Django to trust requests with that header set like this

    SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')