Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploy Django with Gunicorn and APACHE

I have a Django project and I wanna delivery it using gunicorn (and apache proxing). I can't use Nginx, so that's no possible.

I've set the Apache proxy and setup a runner script to gunicorn, but i am get this weird error

2012-08-27 14:03:12 [34355] [DEBUG] GET /
2012-08-27 14:03:12 [34355] [ERROR] Error handling request
Traceback (most recent call last):
     File "/home/tileone/venv/lib/python2.6/site-packages/gunicorn/workers/sync.py", line 93, in handle_request
     self.address, self.cfg)
     File "/home/tileone/venv/lib/python2.6/site-packages/gunicorn/http/wsgi.py", line 146, in create
         path_info = path_info.split(script_name, 1)[1]
     IndexError: list index out of range

I am running this script

#!/bin/bash
LOGFILE=/var/log/gunicorn/one-project.log
VENV_DIR=/path/to/venv/
LOGDIR=$(dirname $LOGFILE)
NUM_WORKERS=5
# user/group to run as
USER=USER
GROUP=GROUP
BIND=127.0.0.1:9999
cd /path_to_project
echo 'Setup Enviroment'
#some libraries
echo 'Setup Venv' 
source $VENV_DIR/bin/activate
export PYTHONPATH=$VENV_DIR/lib/python2.6/site-packages:$PYTHONPATH
#Setup Django Deploy
export DJANGO_DEPLOY_ENV=stage
echo 'Run Server'
test -d $LOGDIR || mkdir -p $LOGDIR
export SCRIPT_NAME='/home/tileone/one-project'
exec $VENV_DIR/bin/gunicorn_django -w $NUM_WORKERS --bind=$BIND\
              --user=$USER --group=$GROUP --log-level=debug \
              --log-file=$LOGFILE 2>>$LOGFILE

and my apache configuration is like this:

Alias /static/ /hpath_to_static/static/
Alias /media/ /path_to_static/media/
Alias /favicon.ico /path_to/favicon.ico

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

What am i doing wrong?

like image 646
Fernando Ferreira Avatar asked Aug 27 '12 15:08

Fernando Ferreira


2 Answers

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>
like image 126
Michael McAuliffe Avatar answered Oct 16 '22 09:10

Michael McAuliffe


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.

like image 33
MShekow Avatar answered Oct 16 '22 08:10

MShekow