django与nginx uwsgi bad gateway 502

时间:2017-02-06 08:55:23

标签: django ubuntu nginx uwsgi

我想用nginx运行django我跟着这个instructions

  

的/ etc / nginx的/位点可用/ cartoview_one

upstream django {
    server unix:///root/cartoview_project/mysite.sock; # for a file socket    }

server {
    listen      80;
    server_name xx.xx.xx.xx;
    charset     utf-8;

    # max upload size
    client_max_body_size 2048M;   # adjust to taste

    # Django media
    location /media  {
        alias /root/cartoview_project/uploaded;
    }

    location /static {
        alias /root/cartoview_project/static;
    }
    location / {
        uwsgi_pass  django;
        include     /root/cartoview_project/uwsgi_params; # the uwsgi_params file you installed
    }
}

然后使用:

创建符号链接
sudo ln -s /etc/nginx/sites-available/cartoview_one /etc/nginx/sites-enabled

并在项目文件夹mysite_uwsgi.ini中:

[uwsgi]
project = cartoview_project
chdir           = /root/cartoview_project
module          = %(project).wsgi:application
home            = /root/env
master          = true
processes       = 10
socket          = /root/cartoview_project/mysite.sock
chmod-socket = 666
vacuum          = true
http-socket = 0.0.0.0:80

项目结构:

.
|-- apps
|-- cartoview_project
|   |-- __init__.py
|   |-- __init__.pyc
|   |-- local_settings.py
|   |-- local_settings.pyc
|   |-- local_settings.py.sample
|   |-- settings.py
|   |-- settings.pyc
|   |-- urls.py
|   |-- wsgi.py
|   `-- wsgi.pyc
|-- default_oauth_apps.json
|-- initial_data.json
|-- manage.py
|-- mysite_uwsgi.ini
`-- uwsgi_params

当我尝试使用myip访问网站时,会显示以下消息:

  

502 Bad Gateway

     

nginx / 1.10.0(Ubuntu)

error.log:

  

2017/02/06 09:46:41 [暴击] 6867#6867:* 1 connect()来   unix:///root/cartoview_project/mysite.sock失败(13:   权限被拒绝)连接到上游时,客户端:   xx.xx.xxx.xxx,服务器:xxx.xxx.xxx.xxx,请求:“GET / HTTP / 1.1”,上游:   “uwsgi:// unix:///root/cartoview_project/mysite.sock:”,主持人:   “xxx.xxx.xxx.xxx”

更新:

我删除了http-socket并运行以下命令/root/env/bin/uwsgi --ini mysite_uwsgi.ini并在项目中出现mysite.sock然后重新启动nginx但没有任何更改。

:~/cartoview_project# stat mysite.sock
  File: 'mysite.sock'
  Size: 0               Blocks: 0          IO Block: 4096   socket
Device: 802h/2050d      Inode: 17172079    Links: 1
Access: (0666/srw-rw-rw-)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2017-02-06 10:44:56.188168371 +0100
Modify: 2017-02-06 10:44:56.188168371 +0100
Change: 2017-02-06 10:44:56.188168371 +0100
 Birth: -

机器:vps(Ubuntu 16.04)用户:root

解决:include /root/cartoview_project/uwsgi_params更改为include /etc/nginx/uwsgi_params;

1 个答案:

答案 0 :(得分:0)

从描述中看起来你没有运行你的uwsgi进程 - 此时502错误意味着nginx正确传递了响应,但没有任何东西可以将连接到 ,或者没有得到正确的答复。

which uwsgi将为您提供相应uwsgi的路径(如果您正在运行它,请在您的virtualenv中执行此操作)。

e.g。

/root/cartoview_project/uwsgi --ini mysite_uwsgi.ini

你应该从uwsgi中取出http-socket配置变量 - nginx正在侦听端口80,并将其传递给你的uwsgi app创建的.sock文件。