[Solved]-Deploy Django with Gunicorn and APACHE

12👍

In case anyone has similar issues, I managed to fix this by removing the equivalent of:

RequestHeader set SCRIPT_NAME /home/tileone/one-project/

And instead adding to settings.py the equivalent of:

FORCE_SCRIPT_NAME = '/one-project'

Of course for this, the apache configuration should be more like:

ProxyPreserveHost On
<Location /one-project/>
    SSLRequireSSL
    ProxyPass http://127.0.0.1:9999/
    ProxyPassReverse http://127.0.0.1:9999/
    RequestHeader set X-FORWARDED-PROTOCOL ssl
    RequestHeader set X-FORWARDED-SSL on
</Location>

4👍

The reason for the fix proposed in the accepted answer is that you need to decide between one of the following two approaches:

  1. Let the HTTP server strip the location sub path BEFORE forwarding the request to the WSGI server (as explained above … this is done when both the Location and the ProxyPass directive end with a forward slash. Nginx behaves the same way). In this case, you may not use the SCRIPT_NAME HTTP header/gunicorn-env-variable, because gunicorn would try to strip the value from the incoming URL (but that fails because the web server, Apache, already did that). In this case, you’re forced to use Django’s FORCE_SCRIPT_NAME setting.
  2. Let the request URL passed to gunicorn unmodified (an example of an URL might be /one-project/admin), and use the SCRIPT_NAME HTTP header (or gunicorn-env-variable). Because then gunicorn will modify the request and strip the value of SCRIPT_NAME from the URL before Django handles building the response.

I would prefer option 2, because you only need to change one file, the web server configuration, and all changes are neatly together.

Leave a comment