使用uwsgi在nginx上部署django app会返回502 - 权限被拒绝

时间:2016-06-18 22:21:46

标签: django nginx uwsgi

我正在尝试使用uswgi在nginx上设置一个简单的django应用程序。 当我发送请求时,它返回502坏网关。

这是我的uwsgi.ini

[uwsgi]
master = true
socket = /usr/share/nginx/firstsite/nginx.sock
chdir = /usr/share/nginx/firstsite
wsgi-file = /usr/share/nginx/firstsite/firstsite/wsgi.py
chmod-socket = 664
vacuum = true

uwsgi输出:

uwsgi --ini uwsgi.ini
[uWSGI] getting INI configuration from uwsgi.ini
*** Starting uWSGI 2.0.11.2 (64bit) on [Sat Jun 18 15:09:30 2016] ***
compiled with version: 4.8.3 20140911 (Red Hat 4.8.3-9) on 02 December 2015 19:47:02
os: Linux-3.10.0-327.4.4.el7.x86_64 #1 SMP Tue Jan 5 16:07:00 UTC 2016
nodename: centos_prod
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 8
current working directory: /usr/share/nginx/firstsite
detected binary path: /root/venv/bin/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
chdir() to /usr/share/nginx/firstsite
your processes number limit is 94006
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 /usr/share/nginx/firstsite/nginx.sock fd 3
Python version: 2.7.5 (default, Nov 20 2015, 02:00:19)  [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x21f6ce0
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 145536 bytes (142 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x21f6ce0 pid: 3421 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 3421)
spawned uWSGI worker 1 (pid: 3426, cores: 1)

nginx.conf

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/conf.d/*.conf;

    upstream django {
        server unix:///usr/share/nginx/firstsite/nginx.sock;
    }
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/firstsite;

        client_max_body_size 300M;   # adjust to taste

        location /media  {
            alias /usr/share/nginx/firstsite/media;
        }

        location /static {
            alias /usr/share/nginx/firstsite/static;
        }

        # Finally, send all non-media requests to the Django server.
        location / {
            uwsgi_pass  django;
            include     /usr/share/nginx/firstsite/uwsgi_params; # the uwsgi_params file you installed
        }
    }
}

我没有使用virtualenv,我的manage.py位于/ usr / share / nginx / firstsite / 我可以在uwsgi上运行没有nginx的应用程序:

uwsgi --wsgi-file /usr/share/nginx/firstsite/firstsite/wsgi.py --http :80

但我要做的是让nginx使用端口80和uwsgi使用文件套接字。 请帮忙。

更新: 我不知道为什么我之前没有看到任何错误,但现在nginx错误日志说:

nginx Permission denied while connecting to upstream

所以看this

我尝试将这些选项添加到uwsgi:

--uid root --gid www-data

所以套接字文件由root拥有,root是www-data组的一部分。 我仍然收到许可错误。

[crit] 6804#0: *1 connect() to unix:///usr/share/nginx/firstsite/nginx.sock failed (13: Permission denied) while connecting to upstream, client: 192.168.168.201, server: _, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:///usr/share/nginx/firstsite/nginx.sock:"

更新

我仍然遇到许可问题。 我创建了一个django用户和一个django组。 nginx和django用户都是django组的成员。 更改了所有文件夹的所有权 的/ usr /共享/ nginx的/ firstsite django拥有。 设置--gid和 - 到django。 然

uwsgi --ini /usr/share/nginx/firstsite/uwsgi.ini

以root身份。我收到了许可错误。 RAN:

runuser -l django -c "uwsgi --ini /usr/share/nginx/firstsite/uwsgi.ini"
同样的。 请注意/ usr / share / nginx归root所有。 我尝试更改nginx配置文件:

uwsgi_pass unix:///usr/share/nginx/firstsite/nginx.sock;

重新启动nginx并运行uwsgi。我收到了这个错误:

connect() to unix:///usr/share/nginx/firstsite/nginx.sock failed (13: Permission denied) while connecting to upstream, client: 192.123.123.123, server: _, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:///usr/share/nginx/firstsite/nginx.sock:"

我还必须设置一个python virtualenv并在uwsgi.ini中设置home以检测django,但我无法通过此套接字失败。

0 个答案:

没有答案