nginx + uwsgi failed to deploy django, either 400 or 502....
伊谢尔伦
伊谢尔伦 2017-05-16 17:28:00
0
1
737

environment:
OS X 10.8.5
django 1.6
python 2.7.2
First post my configuration

[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;
    }
}

The configuration is basically based on the following two tutorials
https://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html
https://docs.djangoproject. com/en/1.6/howto/deployment/wsgi/uwsgi/

If you use socket with both nginx and uwsgi, a 502 error will be reported,
The following is always the case in uwsgi, and no request is seen.

*** 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 ***

If both use 127.0.0.1:3031, a 400 error will be reported.
But you can see in uwsgi that there are requests coming.

*** 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)

I’ve been annoyed for two days and don’t know how to solve it...


Also I would like to ask what is the function of include uwsgi_params; in nginx?
I found that without this, uwsgi will report the following error:

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'
伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(1)
我想大声告诉你

If you use socket with both nginx and uwsgi, a 502 error will be reported

nginx reports 502, indicating that the upstream setting is incorrect or the server pointed to by upstream reports an error. unix:///Users/scarlex/Projects/venv/mysite/white.sock写法错误,多了两个斜杠,正确的是unix:/Users/scarlex/Projects/venv/mysite/white.sock。从题主贴出的uwsgi配置来看,chmod-socket = 664可能不当,尝试设为chmod-socket = 666。nginx一般是以http用户启动的,664Maybe you don’t have permission to write to white.sock.

If you use 127.0.0.1:3031, a 400 error will be reported.

This has nothing to do with nginx, it is purely an error in the configuration of uwsgi or django (if there is an error in the source code of the script itself, it should report 5xx). If possible, post django's settings.py.

Provide a possible guess:

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!