Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在共享开发环境中使用Django、Gunicorn和Ngnix提供静态内容_Django_Nginx_Gunicorn - Fatal编程技术网

在共享开发环境中使用Django、Gunicorn和Ngnix提供静态内容

在共享开发环境中使用Django、Gunicorn和Ngnix提供静态内容,django,nginx,gunicorn,Django,Nginx,Gunicorn,我在这里看到了很多关于这个问题的好答案,但不是我不理解,就是它们不适用于我的特殊情况。我看了这里:还有很多其他的。我遵循了这些答案中的建议,但我仍然没有有效的解决方案。我希望如果我解释清楚我在做什么,也许有人会告诉我哪里出了错 我与其他几个团队在一个共享环境中开发,我们共享一台ngnix服务器。我在这个共享服务器上有一个Django项目,sre-dev.example.com。Django项目的路径是/apps/capman/capman\u port10001/capman 在my setti

我在这里看到了很多关于这个问题的好答案,但不是我不理解,就是它们不适用于我的特殊情况。我看了这里:还有很多其他的。我遵循了这些答案中的建议,但我仍然没有有效的解决方案。我希望如果我解释清楚我在做什么,也许有人会告诉我哪里出了错

我与其他几个团队在一个共享环境中开发,我们共享一台ngnix服务器。我在这个共享服务器上有一个Django项目,
sre-dev.example.com
。Django项目的路径是
/apps/capman/capman\u port10001/capman

在my settings.py中,我设置了以下值:

STATIC_ROOT = '/apps/capman/capman_port10001/capman/static'
STATIC_URL = '/static/'
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'corsheaders',
    'rest_framework',
    'rest_framework.authtoken',
    ...
    'django_db_logger',
    'rest_framework_swagger'
]
我已经创建了一个
/etc/nginx/sites enabled
目录和sym链接

ln -s /apps/capman/capman_port10001/capman/nginx.conf caasa-dev.example.com
我还为
sre-dev
在DNS中创建了别名(CNAME),
caasa-dev.example.com
。并在
nginx.conf
文件中添加了
/apps/capman/capman\u port10001/capman/nginx.conf
,内容如下:

server {
    listen 10001;
    server_name caasa-dev.example.com;

    location /static {
        alias /apps/capman/capman_port10001/capman/static/;
    }
}
我已经执行了

python manage.py collectstatic
。。。现在有好几次了,文件都在那里:

$ pwd
/apps/capman/capman_port10001/capman/static
$ ls
admin  rest_framework  rest_framework_swagger
我重新启动了nginx和gunicron。我的gunicorn服务器有一个systemd配置,因此我可以执行以下操作:

systemctl restart gnuicorn-capman.service
如果我执行
systemctl-l status gnuicorn capman.service
我将得到以下输出:

# systemctl -l status gnuicorn-capman.service
● gnuicorn-capman.service - capman gnuicron service
   Loaded: loaded (/etc/systemd/system/gnuicorn-capman.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2018-12-14 12:42:40 GMT; 17min ago
  Process: 14949 ExecStopPost=/bin/rm -rf /run/gnuicorn-capman (code=exited, status=0/SUCCESS)
  Process: 14946 ExecStop=/bin/kill -s TERM $MAINPID (code=exited, status=0/SUCCESS)
  Process: 14955 ExecStartPre=/bin/chown -R sreuser:example /run/gnuicorn-capman /apps/capman/capman_port10001 (code=exited, status=0/SUCCESS)
  Process: 14953 ExecStartPre=/bin/mkdir /run/gnuicorn-capman (code=exited, status=0/SUCCESS)
 Main PID: 14960 (gunicorn)
   CGroup: /system.slice/gnuicorn-capman.service
           ├─14960 /apps/capman/capman_port10001/env/bin/python2.7 /apps/capman/capman_port10001/env/bin/gunicorn --workers 4 capman.wsgi --bind 0.0.0.0:10001 --access-logfile ./access.log --error-logfile ./error.log --timeout 600
           ├─14966 /apps/capman/capman_port10001/env/bin/python2.7 /apps/capman/capman_port10001/env/bin/gunicorn --workers 4 capman.wsgi --bind 0.0.0.0:10001 --access-logfile ./access.log --error-logfile ./error.log --timeout 600
           ├─14968 /apps/capman/capman_port10001/env/bin/python2.7 /apps/capman/capman_port10001/env/bin/gunicorn --workers 4 capman.wsgi --bind 0.0.0.0:10001 --access-logfile ./access.log --error-logfile ./error.log --timeout 600
           ├─14970 /apps/capman/capman_port10001/env/bin/python2.7 /apps/capman/capman_port10001/env/bin/gunicorn --workers 4 capman.wsgi --bind 0.0.0.0:10001 --access-logfile ./access.log --error-logfile ./error.log --timeout 600
           └─14971 /apps/capman/capman_port10001/env/bin/python2.7 /apps/capman/capman_port10001/env/bin/gunicorn --workers 4 capman.wsgi --bind 0.0.0.0:10001 --access-logfile ./access.log --error-logfile ./error.log --timeout 600
然而,当我试图访问caasa-dev.example.com:10001/admin/login/?next=/admin/I时,我仍然没有得到我的静态内容:

我也收到这样的信息

... "GET /static/admin/css/base.css HTTP/1.1" 404 2793 "http://caasa-dev.example.com:10001/admin/login/?next=/admin/" "Mozilla/5.0 (Macintosh; Intel Mac OS X
。。。在我的access.log中

那么我遗漏了什么呢?我做错了什么

更新:如果我转到
caasa-dev.example.com:10001/static/
I,我将看到标准的Django 404错误页面

更新2:

由于某些原因,我的日志文件为空:

[root@sre-dev sites-enabled]# systemctl -l status nginx
● nginx.service - The NGINX HTTP and reverse proxy server
   Loaded: loaded (/etc/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2018-12-14 12:42:37 GMT; 56min ago
  Process: 14926 ExecStop=/bin/kill -s QUIT $MAINPID (code=exited, status=0/SUCCESS)
  Process: 14932 ExecStart=/apps/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 14930 ExecStartPre=/apps/nginx/sbin/nginx -t (code=exited, status=0/SUCCESS)
 Main PID: 14934 (nginx)
   CGroup: /system.slice/nginx.service
           ├─14934 nginx: master process /apps/nginx/sbin/ngin
           └─14935 nginx: worker process

Dec 14 12:42:37 sre-dev systemd[1]: Starting The NGINX HTTP and reverse proxy server...
Dec 14 12:42:37 sre-dev nginx[14930]: nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
Dec 14 12:42:37 sre-dev nginx[14930]: nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
Dec 14 12:42:37 sre-dev systemd[1]: Started The NGINX HTTP and reverse proxy server.
[root@sre-dev sites-enabled]# ps -ef | grep nginx
root     14934     1  0 12:42 ?        00:00:00 nginx: master process /apps/nginx/sbin/nginx
nobody   14935 14934  0 12:42 ?        00:00:00 nginx: worker process
root     26500  3799  0 13:40 pts/8    00:00:00 grep --color=auto nginx
[root@sre-dev sites-enabled]# vim /etc/nginx/nginx.conf
[root@sre-dev sites-enabled]# grep log /etc/nginx/nginx.conf
error_log /var/log/nginx/error.log;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    access_log  /var/log/nginx/access.log  main;
[root@sre-dev sites-enabled]# ls -l /var/log/nginx/error.log
-rw-r--r--. 1 nginx root 0 Jun  1  2018 /var/log/nginx/error.log
[root@sre-dev sites-enabled]# ls -l /var/log/nginx/access.log
-rw-r--r--. 1 nginx nginx 0 Jun  7  2018 /var/log/nginx/access.log
更新3:

谢谢Ivan,但是我的
/apps/capman/capman\u port10001/capman/nginx.conf
应该是:

server {
    listen 80;
    server_name caasa-dev.example.com;

    location /static {
        alias /apps/capman/capman_port10001/capman/static/;
    }

    location / {
        proxy_pass http://127.0.0.1:10001;
    }
}

更新4:

我刚刚注意到我的/etc/nginx/nginx.conf不包括我的站点启用目录:

[root@sre-dev nginx]# grep -r include *
nginx.conf:include /usr/share/nginx/modules/*.conf;
nginx.conf:    include             /etc/nginx/mime.types;
nginx.conf:    # See http://nginx.org/en/docs/ngx_core_module.html#include
nginx.conf:    include /etc/nginx/conf.d/*.conf;
nginx.conf:        include /etc/nginx/default.d/*.conf;
nginx.conf:#        include /etc/nginx/default.d/*.conf;
nginx.conf.default:    include       mime.types;
nginx.conf.default:        #    include        fastcgi_params;
我应该添加行吗

include /etc/nginx/sites-enabled/*.conf
。。。到我的
/etc/nginx/nginx.conf
,如果是,在该文件中的什么位置?

gunicorn--workers 4 capman.wsgi--bind 0.0.0:10001

nginx:
listen10001

我猜nginx甚至没有启动(参见equalport?)。所有请求都由Django处理。换个港口

如果这是完整的Nginx配置,那么将请求转发到django的代理传递将丢失
proxy\u pass
gunicorn--workers 4 capman.wsgi--bind 0.0.0.0:10001

nginx:
listen10001

我猜nginx甚至没有启动(参见equalport?)。所有请求都由Django处理。换个港口


如果这是完整的Nginx配置,那么将请求转发给django的代理传递将丢失。问题是,你正在使用
gunicorn
为django服务,但Nginx没有监听该端口,因此将代理传递给你的域名。使用此nginx配置:

server {
listen 80;
server_name caasa-dev.example.com;

location /static {
    alias /apps/capman/capman_port10001/capman/static;
}

location / {
    proxy_pass http://127.0.0.1:10001;
}
}
在此配置中,使用的nginx充当反向代理,并将所有流量路由到域的端口80,再路由到gunicorn处理的内部网络
127.0.0.1:10001


注意您正在地址
0.0.0.0
上使用gunicorn。我不建议这样做。将其改为
127.0.0.1
,或者使用linux
.sock
文件进行更好的配置

问题在于,您正在使用
gunicorn
为django提供服务,但nginx没有监听该端口,因此无法将其反向代理给您的域名。使用此nginx配置:

server {
listen 80;
server_name caasa-dev.example.com;

location /static {
    alias /apps/capman/capman_port10001/capman/static;
}

location / {
    proxy_pass http://127.0.0.1:10001;
}
}
在此配置中,使用的nginx充当反向代理,并将所有流量路由到域的端口80,再路由到gunicorn处理的内部网络
127.0.0.1:10001


注意您正在地址
0.0.0.0
上使用gunicorn。我不建议这样做。将其改为
127.0.0.1
,或者使用linux
.sock
文件进行更好的配置

尝试手动打开静态url。什么是回应,错误信息?谢谢伊万。我已经用屏幕截图更新了我的OP。Nginx配置似乎没有被应用-
/static/
仍被转发给django。看到nginx日志中的任何内容了吗?您是否尝试了
root
而不是
alias
?尝试手动打开静态url。什么是回应,错误信息?谢谢伊万。我已经用屏幕截图更新了我的OP。Nginx配置似乎没有被应用-
/static/
仍被转发给django。看到nginx日志中的任何内容了吗?您是否尝试了
root
而不是
alias
?谢谢Ivan。我应该在哪个文件中更改端口?如果10001应该是发布端口,则将其留给nginx并更改gunicorn config。10001是我的团队应该用于django项目的端口,以避免干扰其他开发团队。如果我使用
python manage.py runserver
运行项目,一切都会正常工作。我也是nginx和gunicron的新手。如果你能更明确地告诉我,我会非常高兴。我不知道你的配置应该是什么。我发现了一个主要的缺陷,为这些服务选择哪个端口取决于您和您的团队。我不确定您是否可以接受这些服务中的任何一项,因为您正在与其他人共享基础设施。谢谢Ivan。我应该在哪个文件中更改端口?如果10001应该是发布端口,则将其留给nginx并更改gunicorn config。10001是我的团队应该用于django项目的端口,以避免干扰其他开发团队。如果我使用
python manage.py runserver
运行项目,一切都会正常工作。我也是nginx和gunicron的新手。如果你能更明确地告诉我,我会非常高兴。我不知道你的配置应该是什么。我发现了一个主要的缺陷,为这些服务选择哪个端口取决于您和您的团队。我不确定你是否可以拿80块钱