【问题标题】:502 Bad Gateway connecting Nginx to django via socket502 Bad Gateway 通过套接字将 Nginx 连接到 django
【发布时间】:2015-05-07 03:32:03
【问题描述】:

我正在尝试按照http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html 的教程进行操作。我已经把所有工作都归结为http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html#running-the-django-application-with-uwsgi-and-nginx。我正在亚马逊 EC2 上使用 ubuntu 14.4 实例:

我的 django 项目叫做 tp.截图如下:

按照我所做的说明:

(env1)ubuntu@ip-172-31-28-196:~$ uwsgi --socket /tmp/mysite.sock --module /home/ubuntu/tproxy/tp/tp
/wsgi.py --chmod-socket=664
*** Starting uWSGI 2.0.9 (64bit) on [Thu Mar  5 16:50:02 2015] ***
compiled with version: 4.8.2 on 03 March 2015 02:58:28
os: Linux-3.13.0-44-generic #73-Ubuntu SMP Tue Dec 16 00:22:43 UTC 2014
nodename: ip-172-31-28-196
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /home/ubuntu
detected binary path: /home/ubuntu/.virtualenvs/env1/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 7862
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address /tmp/mysite.sock fd 3
Python version: 3.4.0 (default, Apr 11 2014, 13:08:40)  [GCC 4.8.2]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x1fe5320
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72760 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
ImportError: No module named '/home/ubuntu/tproxy/tp/tp/wsgi'
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 24393, cores: 1)

显然你可以看到:

ImportError: No module named '/home/ubuntu/tproxy/tp/tp/wsgi'
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***

如何设置 wsgi.py 文件的路径?

编辑 1:这是我的 ini 文件:

# mysite_uwsgi.ini file
 [uwsgi]
 # Django-related settings
 # the base directory (full path)
 chdir           = /home/ubuntu/tproxy/tp
 # Django's wsgi file
 module          = uwsgi
 # the virtualenv (full path)
 home            = /home/ubuntu/.virtualenvs/env1
 wsgi-file       = /home/ubuntu/tproxy/tp/tp/wsgi.py
 # process-related settings
 # master
 master          = true
 # maximum number of worker processes
 processes       = 3
 # the socket (use the full path to be safe
 socket          = /tmp/mysite.sock
 # ... with appropriate permissions - may be needed
 chmod-socket    = 664 
 # clear environment on exit
 vacuum          = true

我跑了:

(env1)ubuntu@ip-172-31-28-196:~$ uwsgi --ini /home/ubuntu/tproxy/tp/mysite_uwsgi.ini

但仍然收到 502 错误。

日志:

2015/03/05 18:10:00 [crit] 1828#0: *51 connect() to unix:///tmp/mysite.sock failed (13: Permission denied) while connecting to upstream, client: 107.0.193.131, server: 52.10.**-**, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:///tmp/mysite.sock:", host: "52.10.**-**:8000"

edit 2 : home 是 virtualenv 的目录吗?我设置:

home            = /home/ubuntu/tproxy/

现在:

(env1)ubuntu@ip-172-31-28-196:~$ uwsgi --ini /home/ubuntu/tproxy/tp/mysite_uwsgi.ini
[uWSGI] getting INI configuration from /home/ubuntu/tproxy/tp/mysite_uwsgi.ini
*** Starting uWSGI 2.0.9 (64bit) on [Thu Mar  5 18:59:27 2015] ***
compiled with version: 4.8.2 on 03 March 2015 02:58:28
os: Linux-3.13.0-44-generic #73-Ubuntu SMP Tue Dec 16 00:22:43 UTC 2014
nodename: ip-172-31-28-196
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /home/ubuntu
detected binary path: /home/ubuntu/.virtualenvs/env1/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
chdir() to /home/ubuntu/tproxy/tp
your processes number limit is 7862
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address /tmp/mysite.sock fd 3
Python version: 3.4.0 (default, Apr 11 2014, 13:08:40)  [GCC 4.8.2]
Set PythonHome to /home/ubuntu/tproxy/
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'
Aborted (core dumped)

【问题讨论】:

    标签: python linux django sockets nginx


    【解决方案1】:

    你把它放在你的 uwsgi ini 文件中。这是一个例子:

     [uwsgi]
     # Django-related settings
     # the base directory (full path)
     chdir           = /var/www/virtualenv/project
     # Django's wsgi file
     module          = uwsgi
     # the virtualenv (full path)
     home            = /var/www/virtualenv/
     wsgi-file       = /var/www/virtualenv/project/projectsettings/wsgi.py
     # process-related settings
     # master
     master          = true
     # maximum number of worker processes
     processes       = 10
     # the socket (use the full path to be safe
     socket          = /tmp/mysite.sock
     # ... with appropriate permissions - may be needed
     chmod-socket    = 664 
     # clear environment on exit
     vacuum          = true
    

    这是我的 ini 文件减去我的项目路径。我自己安装时也遇到了同样的问题,但是通过查看谷歌上的其他配置能够找到合适的答案。

    【讨论】:

    • 谢谢,对于virtualenv,你需要指定哪个virtualenv吗?
    • 更新虚拟环境到你的路径。因此,您需要使用每个目录的路径更新的是:chdir、home 和 wsgi-file
    • 差不多,改家到/home/ubuntu/tproxy/
    • 等等,你的项目文件不在你的 virtualenv 文件夹中吗?我以为 tproxy 是您的 virtualenv 文件夹的名称...
    猜你喜欢
    • 2023-03-20
    • 2022-11-27
    • 1970-01-01
    • 2022-11-22
    • 2016-04-20
    • 2022-11-22
    • 2019-09-05
    • 1970-01-01
    • 2014-02-07
    相关资源
    最近更新 更多