用Nginx和Django设置Unix Socket [英] Setting up Unix Socket with Nginx and Django

查看:1272
本文介绍了用Nginx和Django设置Unix Socket的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 http:// uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html 。我已经把所有的工作都归结为 http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html#using-unix-sockets-instead-of-ports 。我正在使用亚马逊EC2上的ubuntu 14.4实例:

I'm trying to follow the tutorial at http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html. I've gotten everything working down to http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html#using-unix-sockets-instead-of-ports. I am working with an ubuntu 14.4 instance on amazon EC2:

Nginx配置文件以:

The Nginx config file starts with:

# the upstream component nginx needs to connect to
upstream django {
    server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    # server 127.0.0.1:8001; # for a web port socket (we'll use this first)
} 

我是混淆了设置unix套接字的语法。我查看了几个来源,包括 http://pymotw.com/2/socket/uds。 html 权限被拒绝 - nginx和uwsgi套接字 https://serverfault.com/questions/124517/whats- the-difference-between-unix-socket-and-tcp-ip-socket

I am confused about the syntax of setting up a unix socket. I've looked at several sources including http://pymotw.com/2/socket/uds.html , Permission denied - nginx and uwsgi socket and https://serverfault.com/questions/124517/whats-the-difference-between-unix-socket-and-tcp-ip-socket

看起来它们都以///开头如果我理解正确的路径只是指向根目录(在我的情况下)django项目,路径必须以.sock结尾。这是否正确?

It looks like they all start with "///" If I understand correctly the path just points to the root directory of ( in my case ) the django project and the path has to end with ".sock" . Does this seem right ?

编辑:我做了uwsgi配置文件,但是我试图用--socket开关来运行它:

edit: I've made the uwsgi config file, but I'm trying to run this with the --socket switch instead:

Nginx配置文件现在开始于:

The Nginx config file now starts with:

# the upstream component nginx needs to connect to
upstream django {
 server unix:///tmp/mysite.sock; # for a file socket
# server 127.0.0.1:8001; # for a web port socket (we'll use this first)
} 

我想你说的是,我在socket上运行uwsgi /tmp/mysite.sock。

Following what I think you are saying, I'm running uwsgi on socket /tmp/mysite.sock .

(env1)ubuntu@ip-172-31-28-196:~$ uwsgi --socket /tmp/mysite.sock --wsgi-file test.py --chmod-socket=664

当我打开: / p>

When I open:

http://52.10.----:8000/

在firefox中,我看到:

In firefox I see:

502 Bad Gateway

nginx的错误日志显示:

The error log for nginx shows:

2015/03/04 15:08:44 [crit] 18398#0: *3 connect() to unix:///home/ubuntu/mysite.sock failed (13: Permission denied) while connecting to upstream, client: 64.56.60.130, server: 52.10.200.38, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:///home/ubuntu/mysite.sock:", host: "52.10.200.38:8000"

编辑2:我评论了django相关的东西 -

edit 2: I commented out the django related stuff -

 # mysite_uwsgi.ini file
 [uwsgi]    

 # Django-related settings
 # the base directory (full path)
 #chdir           = /path/to/your/project
 # Django's wsgi file
 #module          = project.wsgi
 # the virtualenv (full path)
 #home            = /path/to/virtualenv     

 # process-related settings
 # master
 master          = true
 # maximum number of worker processes
 processes       = 1
 # 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 --wsgi-file test.py

但再次出现502错误。

but got a 502 error again.

2015/03/04 17:50:00 [crit] 18842#0: *1 connect() to unix:///tmp/mysite.sock failed (13: Permission denied) while connecting to upstream, client: 64.56.60.130, server: 52.10.200.38, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:///tmp/mysite.sock:", host: "52.10.200.38:8000"

编辑3:

(env1)ubuntu@ip-172-31-28-196:~$ uwsgi --ini /home/ubuntu/tproxy/tp/mysite_uwsgi.ini --wsgi-file te
st.py
[uWSGI] getting INI configuration from /home/ubuntu/tproxy/tp/mysite_uwsgi.ini
*** Starting uWSGI 2.0.9 (64bit) on [Wed Mar  4 18:41:43 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 !!!
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 0x205e710
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 145520 bytes (142 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x205e710 pid: 18868 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 18868)
spawned uWSGI worker 1 (pid: 18869, cores: 1)

编辑4: - 只是注意到

edit 4: - just noticed

♥SIGINT/SIGQUIT received...killing workers...
worker 1 buried after 1 seconds
goodbye to uWSGI.
VACUUM: unix socket /tmp/mysite.sock removed.

所以每次退出uWSGI,套接字都被删除。也许我应该关闭真空选项,以便保持chmod设置?

So everytime I exit uWSGI, the socket is being removed. Maybe I should turn the vaccum option off so the chmod settings are preserved?

我改变了:

vacuum          = false

但...

 (env1)ubuntu@ip-172-31-28-196:/tmp$ sudo chmod 777 mysite.sock
 (env1)ubuntu@ip-172-31-28-196:/tmp$ ls -la
 total 8
 drwxrwxrwt  2 root   root   4096 Mar  4 19:32 .
 drwxr-xr-x 22 root   root   4096 Mar  4 19:06 ..
 srwxrwxrwx  1 ubuntu ubuntu    0 Mar  4 19:32 mysite.sock
 (env1)ubuntu@ip-172-31-28-196:/tmp$ cd /home/ubuntu/
 (env1)ubuntu@ip-172-31-28-196:~$ ls
 host_type.py  requirements.txt  test.py  tproxy
 (env1)ubuntu@ip-172-31-28-196:~$ sudo /etc/init.d/nginx restart
  * Restarting nginx nginx
    ...done.
 (env1)ubuntu@ip-172-31-28-196:~$ uwsgi --ini /home/ubuntu/tproxy/tp/mysite_uwsgi.ini --wsgi-fi
 st.py
 [uWSGI] getting INI configuration from /home/ubuntu/tproxy/tp/mysite_uwsgi.ini
 *** Starting uWSGI 2.0.9 (64bit) on [Wed Mar  4 19:36:42 2015] ***
 .....
 spawned uWSGI worker 1 (pid: 1568, cores: 1)
 ♥SIGINT/SIGQUIT received...killing workers...
 worker 1 buried after 1 seconds
 goodbye to uWSGI.
 (env1)ubuntu@ip-172-31-28-196:~$ cd /tmp
 (env1)ubuntu@ip-172-31-28-196:/tmp$ ls -la
 total 8
 drwxrwxrwt  2 root   root   4096 Mar  4 19:36 .
 drwxr-xr-x 22 root   root   4096 Mar  4 19:06 ..
 srw-rw-r--  1 ubuntu ubuntu    0 Mar  4 19:36 mysite.sock

所以chmod不保存..

so chmod is not preserved..

编辑5:

(env1)ubuntu@ip-172-31-28-196:/tmp$ uwsgi --ini /home/ubuntu/tproxy/tp/mysite_uwsgi.ini --wsgi-file
 test.py --chmod-socket=777
[uWSGI] getting INI configuration from /home/ubuntu/tproxy/tp/mysite_uwsgi.ini
*** Starting uWSGI 2.0.9 (64bit) on [Wed Mar  4 20:54:21 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: /tmp
detected binary path: /home/ubuntu/.virtualenvs/env1/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
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 0x18b4790
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 145520 bytes (142 KB) for 1 cores
*** Operational MODE: single process ***
failed to open python file test.py
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 master process (pid: 1858)
spawned uWSGI worker 1 (pid: 1859, cores: 1)
--- no python application found, check your startup logs for errors ---
[pid: 1859|app: -1|req: -1/1] 64.56.60.130 () {36 vars in 555 bytes} [Wed Mar  4 20:54:34 2015] GET
/ => generated 21 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 83 bytes (0 switches on core 0)

现在得到内部服务器错误:

Now getting internal server error:

2015/03/04 19:33:26 [crit] 1531#0: *1 connect() to unix:///tmp/mysite.sock failed (13: Permission denied) while connecting to upstream, client: 64.56.60.130, server: 52.10.200.38, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:///tmp/mysite.sock:", host: "52.10.200.38:8000"


推荐答案

将其更改为说服务器 unix:///tmp/mysite.sock; 然后在您制作的uwsgi ini文件,在 / tmp /

Change it to say server unix:///tmp/mysite.sock; and then in the uwsgi ini file you make, create the unix socket in /tmp/

这篇关于用Nginx和Django设置Unix Socket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆