环境:
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'
我想大声告诉你2017-05-16 17:30:00
如果用 nginx 和 uwsgi 都用 socket 的话,会报 502 错误
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。
如果都用 127.0.0.1:3031 的话,都会报 400 错误。
这就跟nginx无关了,纯粹是uwsgi的配置或者django配置有误(如果是自身的脚本源码出错应该报5xx)。如果可能贴出django的settings.py吧。
提供一个可能性的猜测:
Finally, if DEBUG is False, you also need to properly set the ALLOWED_HOSTS setting. Failing to do so will result in all requests being returned as “Bad Request (400)”
https://docs.djangoproject.com/en/1.6/ref/settings/#std%3asetting-ALLOWED_HOSTS