我是 django 的新手,这是我第一次想部署 django 项目。我正在尝试使用 gunicorn 和 nginx 部署 django-oscar 的沙箱项目。我为 nginx 和 wsgi 使用了 oscar 的部署脚本,并修改了它们以满足我的需求,它们是:
wsgi.py:
import os
import sys
import site
import urllib
sys.stdout = sys.stderr
# Project root
root = '/home/ashkan/setak/setak/sites/sandbox'
sys.path.insert(0, root)
# Packages from virtualenv
activate_this = '/home/ashkan/setak/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
# Set environmental variable for Django and fire WSGI handler
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
_application = django.core.handlers.wsgi.WSGIHandler()
def application(environ, start_response):
environ['PATH_INFO'] = urllib.unquote(environ['REQUEST_URI'].split('?')[0])
return _application(environ, start_response)
nginx配置:
server {
listen 80;
server_name setakshop.ir;
# set_real_ip_from 192.168.125.252;
# real_ip_header X-Forwarded-For;
# Ensure only one domain
if ($host != 'setakshop.ir') {
rewrite / $scheme://setakshop.ir$request_uri permanent;
}
access_log /home/ashkan/setak/setak/logs/nginx_access.log;
error_log /home/ashkan/setak/setak/logs/nginx_error.log;
gzip on;
gzip_proxied any;
gzip_static on;
gzip_types text/plain application/xml application/x-javascript text/javascript text/css application/x-json application/json;
client_max_body_size 3M;
keepalive_timeout 5;
location =/robots.txt {
root /home/ashkan/setak/setak/sites/sandbox/public/static/;
expires max;
}
location =/favicon.ico {
root /home/ashkan/setak/setak/sites/sandbox/public/static/oscar/;
expires max;
}
location /media/ {
alias /home/ashkan/setak/media/latest/;
expires max;
}
location /static/ {
root /home/ashkan/setak/setak/sites/sandbox/public;
expires max;
}
location / {
uwsgi_pass unix:/home/ashkan/setak/run/latest/uwsgi.sock;
uwsgi_send_timeout 5;
include uwsgi_params;
proxy_pass http://127.0.0.1:8001;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
}
现在每当有人进入网站时我得到的错误是:
Traceback (most recent call last):
File "/home/ashkan/setak/local/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 130, in handle
self.handle_request(listener, req, client, addr)
File "/home/ashkan/setak/local/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 171, in handle_request
respiter = self.wsgi(environ, resp.start_response)
File "/home/ashkan/setak/setak/sites/sandbox/wsgi.py", line 22, in application
environ['PATH_INFO'] = urllib.unquote(environ['REQUEST_URI'].split('?')[0])
KeyError: 'REQUEST_URI'
[2015-05-05 16:46:04 +0000] [12506] [ERROR] Error handling request
Traceback (most recent call last):
File "/home/ashkan/setak/local/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 130, in handle
self.handle_request(listener, req, client, addr)
File "/home/ashkan/setak/local/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 171, in handle_request
respiter = self.wsgi(environ, resp.start_response)
File "/home/ashkan/setak/setak/sites/sandbox/wsgi.py", line 22, in application
environ['PATH_INFO'] = urllib.unquote(environ['REQUEST_URI'].split('?')[0])
KeyError: 'REQUEST_URI'
我打印了 environ 和 os.environ 字典,但没有看到任何带有键 REQUEST_URI 的条目。
我怎样才能解决这个问题?我应该编辑 wsgi 脚本还是应该修复 environ 字典以包含 REQUEST_URI 键?我该怎么做?
提前致谢。
更新:因为我使用 virtualenv 来部署这个项目,并且我用来激活 virtualenv 的激活脚本是 setak/bin/activate,所以我认为问题可能出在 wsgi.py 中,它设置了 activate_this 变量。但仍然不知道如何解决这个问题!
update2:这是 nginx 以前的配置,也没有工作。我是从教程中自己写的,然后当它不起作用时,我切换到 oscar 提出的版本。
server {
server_name setakshop.ir;
access_log off;
location /static/ {
alias /home/ashkan/setak/setak/sites/sandbox/static/;
}
location / {
proxy_pass http://127.0.0.1:8001;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
}