用nginx和uWSGI服务django

我跟着这个post来服务我的Django项目。 该项目与manage.py runserver运行服务器运行良好,我想设置为生产。 这是我的设置文件:

nginx.conf

 upstream django { server /tmp/vc.sock; #server 10.9.1.137:8002; } server { listen 8001; server_name 10.9.1.137; charset utf-8; client_max_body_size 25M; location /media { alias /home/deploy/vc/media; } location /static { alias /home/deploy/vc/static; } location / { uwsgi_pass django; include /etc/nginx/uwsgi_params; } } 

uwsgi.ini

 [uwsgi] chdir = /home/deploy/vc wsgi-file = vc/wsgi.py master = true processes = 2 #socket = :8002 socket = /tmp/vc.sock chmod-socket = 666 vacuum = true 

如果我使用TCP端口套接字( server 10.9.1.137:8002socket = :8002 ),它会没事的。 但是,如果我注释掉它们并使用Unix套接字( server /tmp/vc.socksocket = /tmp/vc.sock ),服务器将返回502错误。 我应该如何解决它?

下面是运行/etc/init.d/nginx restart时的nginx错误日志

 nginx: [emerg] invalid host in upstream "/tmp/vc.sock" in /etc/nginx/conf.d/vc.conf:2 nginx: configuration file /etc/nginx/nginx.conf test failed 

这是我运行uwsgi --ini vc/uwsgi.ini时的警告:

 *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 

我不能以root身份运行uWSGI吗?

使用Unix域套接字的upstream server必须声明如下:

 upstream django { server unix:/tmp/vc.sock; 

是的,我想你可以以root身份运行uWSGI,但绝对不能 。 这是安全101. uWSGI项目甚至可以称之为常识 :

常识:不要以root身份运行uWSGI实例。 您可以以root身份启动您的uWSGI,但请确保使用uidgid选项删除权限。


顺便说一下,你的server块可以使用root指令。 这将让你摆脱你的静态资产那些毫无意义的冗余location

  root /home/deploy/vc;