環境:
OS X 10.8.5
django 1.6
python 2.7.2
先貼一下我的設定
[uwsgi]
# socket = /Users/scarlex/Projects/venv/mysite/white.sock
socket = 127.0.0.1:3031
chdir = /Users/scarlex/Projects/venv/mysite/
module = sakuramai.wsgi:application
home = /Users/scarlex/Projects/venv
env = DJANGO_SETTINGS_MODULE=sakuramai.settings
static_files = false
master = true
processes = 5
chmod-socket = 664
vacuum = true
stats = 127.0.0.1:9191
upstream white {
server localhost:3031;
# server unix:///Users/scarlex/Projects/venv/mysite/white.sock;
}
server {
listen 80;
server_name mysite.com localhost;
charset utf-8;
location /static/ {
alias /Users/scarlex/Projects/venv/mysite/static/
expires 30d;
}
location / {
uwsgi_pass white;
include uwsgi_params;
uwsgi_param UWSGI_SCRIPT django_wsgi;
}
}
設定基本上都是按照下面這兩個教學來的
https://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html
https://docs.djangoproject. com/en/1.6/howto/deployment/wsgi/uwsgi/
如果用 nginx 和 uwsgi 都用 socket 的話,會報 502 錯誤,
uwsgi裡面一直都是下面這樣,看不到有請求到來。
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 88475)
spawned uWSGI worker 1 (pid: 88476, cores: 1)
spawned uWSGI worker 2 (pid: 88477, cores: 1)
spawned uWSGI worker 3 (pid: 88478, cores: 1)
spawned uWSGI worker 4 (pid: 88479, cores: 1)
spawned uWSGI worker 5 (pid: 88480, cores: 1)
*** Stats server enabled on 127.0.0.1:9191 fd: 17 ***
如果都用 127.0.0.1:3031 的話,都會報 400 錯誤。
但是 uwsgi 裡面可以看到有請求到來。
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 88497)
spawned uWSGI worker 1 (pid: 88498, cores: 1)
spawned uWSGI worker 2 (pid: 88499, cores: 1)
spawned uWSGI worker 3 (pid: 88500, cores: 1)
spawned uWSGI worker 4 (pid: 88501, cores: 1)
spawned uWSGI worker 5 (pid: 88502, cores: 1)
*** Stats server enabled on 127.0.0.1:9191 fd: 17 ***
[pid: 88500|app: 0|req: 1/1] 127.0.0.1 () {42 vars in 727 bytes} [Mon Feb 24 15:47:56 2014] GET / => generated 26 bytes in 406 msecs (HTTP/1.1 400) 1 headers in 53 bytes (1 switches on core 0)
[pid: 88501|app: 0|req: 1/2] 127.0.0.1 () {40 vars in 658 bytes} [Mon Feb 24 15:47:57 2014] GET /favicon.ico => generated 26 bytes in 190 msecs (HTTP/1.1 400) 1 headers in 53 bytes (1 switches on core 0)
煩了兩天了,不知道該怎麼解決...
另外想問 nginx 中的 include uwsgi_params; 作用是什麼?
我發現如果沒有這個的話,uwsgi就會報下面的錯誤:
Traceback (most recent call last):
File "/Users/scarlex/Projects/Python-projects/skm2014/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 196, in __call__
request = self.request_class(environ)
File "/Users/scarlex/Projects/Python-projects/skm2014/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 91, in __init__
self.method = environ['REQUEST_METHOD'].upper()
KeyError: u'REQUEST_METHOD'
nginx報502,表示upstream設定有誤或upstream指向的server報錯。
。从题主贴出的uwsgi配置来看,unix:///Users/scarlex/Projects/venv/mysite/white.sock
写法错误,多了两个斜杠,正确的是unix:/Users/scarlex/Projects/venv/mysite/white.sock
chmod-socket = 664
可能不当,尝试设为chmod-socket = 666
。nginx一般是以http用户启动的,664
可能無權寫入white.sock。這跟nginx無關了,純粹是uwsgi的配置或是django配置有誤(如果是自身的腳本原始碼出錯應該報5xx)。如果可能貼出django的settings.py吧。
提供一個可能性的猜測:
https://docs.djangoproject.com/en/1.6/ref/settings/#std%3asetting-ALLOWED_HOSTS