我有一个Django项目,我想用gunicorn(和apache proxing)交付它。 我不能使用Nginx,所以这是不可能的。
我已经设置了Apache代理并为gunicorn设置了一个跑步者脚本,但我得到了这个奇怪的错误
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
我正在运行此脚本
#!/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
我的apache配置是这样的:
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>
我做错了什么?
答案 0 :(得分:10)
如果有人有类似的问题,我设法通过删除相应的:
来解决这个问题RequestHeader set SCRIPT_NAME /home/tileone/one-project/
而是添加settings.py
相当于:
FORCE_SCRIPT_NAME = '/one-project'
当然,对于此,apache配置应该更像:
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>
答案 1 :(得分:0)
之所以接受接受的答案,是因为您需要在以下两种方法之一中做出决定:
FORCE_SCRIPT_NAME
设置。/one-project/admin
),并使用SCRIPT_NAME HTTP标头(或gunicorn-env-variable)。因为那样,gunicorn将在Django处理构建响应之前修改请求并从URL中剥离SCRIPT_NAME的值。我希望使用选项2,因为您只需要更改一个文件,Web服务器配置,并且所有更改都可以整齐地放在一起。