简体   繁体   English

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

[英]Serving static content with Django, Gunicorn and Ngnix in shared development environment

I have seen many great answers to this question here on SO, but either I don't understand them or they don't apply to my particular circumstance. 在SO上,我已经看到了很多关于此问题的好答案,但是要么我听不懂它们,要么它们不适用于我的特定情况。 I have looked here: Serving static files with Nginx + Gunicorn + Django and many others. 我看过这里: 使用Nginx + Gunicorn + Django和许多其他文件提供静态文件 I have followed the recommendation in those answers and I still do not have solution that works. 我在这些答案中遵循了建议,但仍然没有可行的解决方案。 I hope that if I explain exactly what I am doing then maybe someone will tell me where I went wrong. 我希望,如果我确切解释自己在做什么,那么也许有人会告诉我我哪里出了问题。

I developing in a shared environment with several other teams and we share an ngnix server. 我与其他几个团队在共享环境中进行开发,我们共享一个ngnix服务器。 I have a Django project on this shared server, sre-dev.example.com . 我在此共享服务器sre-dev.example.com上有一个Django项目。 The path to the Django project is /apps/capman/capman_port10001/capman . Django项目的路径是/apps/capman/capman_port10001/capman

In my settings.py I have these values set: 在我的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'
]

I have created a /etc/nginx/sites-enabled directory and sym link, 我创建了一个/etc/nginx/sites-enabled目录和符号链接,

ln -s /apps/capman/capman_port10001/capman/nginx.conf caasa-dev.example.com

I also created alias (CNAME), caasa-dev.example.com in our DNS for sre-dev . 我还在DNS中为sre-dev创建了别名(CNAME) caasa-dev.example.com And in added a nginx.conf file, /apps/capman/capman_port10001/capman/nginx.conf with the contents of: 并在其中添加了一个nginx.conf文件/apps/capman/capman_port10001/capman/nginx.conf ,其内容如下:

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

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

I have executed ... 我已经执行...

python manage.py collectstatic

... several times now and the files are there: ...几次,文件在那里:

$ pwd
/apps/capman/capman_port10001/capman/static
$ ls
admin  rest_framework  rest_framework_swagger

I have restarted nginx and gunicron. 我已经重启了nginx和gunicron。 I have a systemd configuration for my gunicorn server so I can do stuff like: 我的gunicorn服务器具有系统配置,因此我可以执行以下操作:

systemctl restart gnuicorn-capman.service

If I do systemctl -l status gnuicorn-capman.service I get this output: 如果我执行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

Yet still when I attempt to go to caasa-dev.example.com:10001/admin/login/?next=/admin/ I do not get my static content: 但是,当我尝试访问caasa-dev.example.com:10001/admin/login/?next=/admin/时,仍然无法获得静态内容:

没有静态

I also get messages like this ... 我也收到这样的消息...

... "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

... in my access.log. ...在我的access.log中。

So what have I left out? 那我遗漏了什么呢? What am I doing wrong? 我究竟做错了什么?

Update: If I go to caasa-dev.example.com:10001/static/ I the standard Django 404 error page. 更新:如果我转到caasa-dev.example.com:10001/static/caasa-dev.example.com:10001/static/显示标准Django 404错误页面。

Update 2: 更新2:

For some reason my log files are empty: 由于某些原因,我的日志文件为空:

[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

Update 3: 更新3:

Thanks Ivan but should my /apps/capman/capman_port10001/capman/nginx.conf be: 感谢Ivan,但我的/apps/capman/capman_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;
    }
}

or 要么

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

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

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

Update 4: 更新4:

I just noticed that my /etc/nginx/nginx.conf does not include my sites-enabled directory: 我只是注意到我的/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;

Should I add the line ... 我应该添加行...

include /etc/nginx/sites-enabled/*.conf

... to my /etc/nginx/nginx.conf and if so where in that file? ...到我的/etc/nginx/nginx.conf ,如果可以的话,在该文件中的什么位置?

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

nginx: listen 10001; nginx: listen 10001;

I guess nginx did not even start (see equal port?). 我猜nginx甚至都没有启动(请参阅相等端口?)。 All the requests are processed by Django. 所有请求均由Django处理。 Change port. 更改端口。

And if this is full Nginx config you have then it's missing proxy_pass block to forward requests to django 如果这是完整的Nginx配置,则缺少用于将请求转发到Django的proxy_pass

The problem is that u are using gunicorn to serve django but nginx is not listening on that port so that reverse proxy ith to you domain name. 问题是您正在使用gunicorn服务于Django,但nginx没有在该端口上侦听,因此反向代理ith到您的域名。 use this nginx config: 使用以下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;
}
}

In this config, nginx with act as a reverse proxy, and route all traffic to port 80 of your domain, to internal network 127.0.0.1:10001 which is handled by gunicorn. 在此配置中,nginx与充当逆向代理,并将所有流量路由到您域的端口80,到内部网络127.0.0.1:10001 ,该网络由gunicorn处理。

note you are using gunicorn on address 0.0.0.0 . 请注意,您在地址0.0.0.0上使用gunicorn。 I donot recommend that. 我不建议这样做。 change it to 127.0.0.1 instead or use linux .sock files for better config. 将其更改为127.0.0.1或使用linux .sock文件进行更好的配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM