我正在使用 Nginx、uWSGI、Django 和 Postgresql 堆栈构建应用程序。该应用程序使用 Nginx 和 uWSGI 之间的 Unix 套接字。套接字上的文件权限是 775。但我仍然得到这个权限错误:
[error] 6978#0: *6725 connect() to unix:/path/to/socket failed (111: Connection refused) while connecting to upstream, client: 54.250.253.225, server: example.com, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/path/to/socket:", host: "example.com"
Nginx 配置
# uWSGI upstream
upstream app{
server unix:/path/to/socket;
}
# redirect www to non-www
server{
listen 80;
server_name www.example.com;
return 301 http://example.com$request_uri;
}
# configuration of the server
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
# substitute your machine's IP address or FQDN
server_name example.com
charset utf-8;
# root folder
root /path/to/root;
# max upload size
client_max_body_size 20M; # adjust to taste
access_log /path/to/access.log;
error_log /path/to/error.log;
# Django media
location /media {
# your Django project's media files - amend as required
alias /path/to/media;
}
location /static {
# your Django project's static files - amend as required
alias /path/to/static;
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass app;
# the uwsgi_params file you installed
include /path/to/uwsgi_params;
uwsgi_param Host $host;
uwsgi_param X-Real-IP $remote_addr;
uwsgi_param UWSGI_SCHEME $scheme;
uwsgi_param SERVER_SOFTWARE nginx/nginx_version;
}
}
我的虚拟环境中的文件都被 chown 到了 ubuntu:ubuntu。nginx用户是ubuntu。
uWSGI 日志
*** Starting uWSGI 2.0.12 (64bit) on [Sun Apr 17 17:24:37 2016] ***
compiled with version: 4.8.4 on 17 April 2016 14:40:25
os: Linux-3.13.0-85-generic #129-Ubuntu SMP Thu Mar 17 20:50:15 UTC 2016
nodename: ip-10-167-29-5
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 1
current working directory: /path/to/app
detected binary path: /path/to/bin/uwsgi
chdir() to /path/to/chdir
your processes number limit is 30035
your memory page size is 4096 bytes
*** WARNING: you have enabled harakiri without post buffering. Slow upload could be rejected on post-unbuffered webservers ***
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 /path/to/socket fd 3
Python version: 3.4.3 (default, Oct 14 2015, 20:31:36) [GCC 4.8.4]
Set PythonHome to /path/to/home
Python main interpreter initialized at 0xa4df10
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 436608 bytes (426 KB) for 5 cores
*** Operational MODE: preforking ***
/path/to/lib/python3.4/site-packages/cloudinary/models.py:24: RemovedInDjango110Warning: SubfieldBase has been deprecated. Use Field.from_db_value instead.
return meta(name, bases, d)
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0xa4df10 pid: 8567 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 8567)
spawned uWSGI worker 1 (pid: 8569, cores: 1)
spawned uWSGI worker 2 (pid: 8570, cores: 1)
spawned uWSGI worker 3 (pid: 8571, cores: 1)
spawned uWSGI worker 4 (pid: 8572, cores: 1)
spawned uWSGI worker 5 (pid: 8573, cores: 1)
有人可以告诉我是什么导致了这个权限问题吗?
看起来 uWSGI 服务正在侦听具有不同绝对路径的 Unix 套接字(由于
chdir()
调用)。您可以使用它lsof | grep "/path/to/socket"
来查找正在侦听的 Unix 套接字的绝对路径。我想是的/path/to/chdir/path/to/socket
。我能够自己解决问题。我重新启动了服务器。然后我重新启动了 Nginx 和 uWSGI
Nginx 重新启动没有问题。但是,uWSGI 返回了这个错误:
我试图切换到目录。然而,它并不存在。因此,我在 uwsgi 目录中创建了 init 目录:
然后我进入了新目录:
在那个目录中,我创建了 uWSGI 找不到的文件:do_command。
打开文件编辑器后,我粘贴了我找到的这个文件:https ://raw.githubusercontent.com/jbq/uwsgi/master/debian/uwsgi-files/init/do_command :
然后我重新启动了 uWSGI:
Django 应用程序正常启动。
首先,让配置更容易阅读,忘记上游的东西。我在 uWSGI 上工作了很长时间。我经常遇到套接字问题(特别是在 python 项目中),但是使用 PHP 和 uWSGI 我得到了套接字,非常好。
如果你真的坚持使用 uWSGI,你需要两件事!
第一个 config.ini,其中存储了与您的项目相关的所有 uWSGI 参数,以及 nginx 配置。
第二个 nginx.conf,请求被路由回 uWSGI 进行进一步处理。
/etc/主机:
nginx.conf:
projekte.conf(我们的 ini 文件):
您更改到 uWSGI 应用程序文件夹并使用以下命令启动 uWSGI:
启动 nginx:
并打开网址(本例:http://projekte.local)
问:你为什么坚持使用 uWSGI ?gunicorn 适用于足够好的独立机器,并且使用起来更简单。从 gunicorn 切换到 uWSGI 只需一分钟(无需更改一行代码)。
uWSGI 用于缩放……独立……嗯,我不知道。
最好的,驯兽师
所以基本上,我的是一个 Django 项目。重置我的root密码后,我重新激活了我的虚拟环境并重新启动了Nginx和Gunicorn。这解决了我的错误。
Nginx 重启:
sudo service nginx restart
Gunicorn 重启:
gunicorn --reload <your-wsgi-folder-name>.wsgi bind 0.0.0.0:8000