簡體   English   中英

在共享開發環境中使用Django,Gunicorn和Ngnix提供靜態內容

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

在SO上,我已經看到了很多關於此問題的好答案,但是要么我聽不懂它們,要么它們不適用於我的特定情況。 我看過這里: 使用Nginx + Gunicorn + Django和許多其他文件提供靜態文件 我在這些答案中遵循了建議,但仍然沒有可行的解決方案。 我希望,如果我確切解釋自己在做什么,那么也許有人會告訴我我哪里出了問題。

我與其他幾個團隊在共享環境中進行開發,我們共享一個ngnix服務器。 我在此共享服務器sre-dev.example.com上有一個Django項目。 Django項目的路徑是/apps/capman/capman_port10001/capman

在我的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目錄和符號鏈接,

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

我還在DNS中為sre-dev創建了別名(CNAME) caasa-dev.example.com 並在其中添加了一個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/;
    }
}

我已經執行...

python manage.py collectstatic

...幾次,文件在那里:

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

我已經重啟了nginx和gunicron。 我的gunicorn服務器具有系統配置,因此我可以執行以下操作:

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/時,仍然無法獲得靜態內容:

沒有靜態

我也收到這樣的消息...

... "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/caasa-dev.example.com:10001/static/顯示標准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_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;
    }
}

要么

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;
    }
}

更新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.0:10001

nginx: listen 10001;

我猜nginx甚至都沒有啟動(請參閱相等端口?)。 所有請求均由Django處理。 更改端口。

如果這是完整的Nginx配置,則缺少用於將請求轉發到Django的proxy_pass

問題是您正在使用gunicorn服務於Django,但nginx沒有在該端口上偵聽,因此反向代理ith到您的域名。 使用以下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,到內部網絡127.0.0.1:10001 ,該網絡由gunicorn處理。

請注意,您在地址0.0.0.0上使用gunicorn。 我不建議這樣做。 將其更改為127.0.0.1或使用linux .sock文件進行更好的配置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM