简体   繁体   English

使用Gunicorn和APACHE部署Django

[英]Deploy Django with Gunicorn and APACHE

I have a Django project and I wanna delivery it using gunicorn (and apache proxing). 我有一个Django项目,我想使用gunicorn(和apache proxing)交付它。 I can't use Nginx, so that's no possible. 我不能使用Nginx,所以这是不可能的。

I've set the Apache proxy and setup a runner script to gunicorn, but i am get this weird error 我已经设置了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

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: 我的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>

What am i doing wrong? 我究竟做错了什么?

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: 而是添加到settings.py相当于:

FORCE_SCRIPT_NAME = '/one-project'

Of course for this, the apache configuration should be more like: 当然,对于这个,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>

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). 让HTTP服务器在将请求转发给WSGI服务器之前剥离位置子路径(如上所述......当Location和ProxyPass指令都以正斜杠结束时,这就完成了.Nginx的行为方式相同)。 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). 在这种情况下,您可能不会使用SCRIPT_NAME HTTP标头/ gunicorn-env变量,因为gunicorn会尝试从传入的URL中剥离值(但由于Web服务器Apache已经这样做了,因此失败)。 In this case, you're forced to use Django's FORCE_SCRIPT_NAME setting. 在这种情况下,您被迫使用Django的FORCE_SCRIPT_NAME设置。
  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). 让请求URL未经修改地传递给gunicorn(URL的示例可能是/one-project/admin ),并使用SCRIPT_NAME HTTP标头(或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. 因为在Django处理构建响应之前,gunicorn会修改请求并从URL中删除SCRIPT_NAME的值。

I would prefer option 2, because you only need to change one file, the web server configuration, and all changes are neatly together. 我更喜欢选项2,因为您只需要更改一个文件,Web服务器配置,并且所有更改都整齐地放在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM