0

私はdjangoの完全な初心者であり、djangoプロジェクトをデプロイしたいのはこれが初めてです. django-oscar のサンドボックス プロジェクトを gunicorn と nginx でデプロイしようとしています。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'

環境辞書と os.environ 辞書を出力しましたが、キー REQUEST_URI を持つエントリは見当たりませんでした。

どうすればこれを修正できますか? wsgi スクリプトを編集する必要がありますか、それとも環境辞書を修正して REQUEST_URI キーを含める必要がありますか? どのように私はそれを行うことができますか?

前もって感謝します。

更新: virtualenv を使用してこのプロジェクトをデプロイしており、virtualenv をアクティブにするために使用している activate スクリプトは setak/bin/activate であるため、問題は activate_this 変数を設定する wsgi.py にあると考えられます。しかし、これを修正する方法はまだわかりません!

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"';
    }
}
4

0 に答える 0