我是django的新手,这是我第一次部署django项目。我正在尝试用gunicorn和nginx部署django-oscar的沙箱项目。我使用奥斯卡的nginx和wsgi部署脚本,我修改它们以满足我的需求,这里是:
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 config:
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键?我怎么做dat?
提前感谢。
更新:因为我使用virtualenv来部署这个项目,而我用来激活virtualenv的激活脚本是setak / bin / activate,我认为问题可能出在wsgi.py中,它设置了activate_this变量。但仍然不知道如何解决这个问题!
update2:这是nginx的上一个配置,它也没有用。我自己从一个教程编写了这个,然后当它没有工作时,我切换到奥斯卡提出的版本。
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"';
}
}