您需要以下方面的帮助:
当我使用nginx / supervisord启动我的django应用程序时,我的静态图像文件没有加载,但是当我开始使用 django manage.py 时,图像加载没有问题。
我搜索了许多django / nginx静态文档无济于事。我的图像显示我何时运行
python manage.py runserver 0.0.0.0:9000
但是当我使用nginx / supervisor运行时,除静态目录中的图像外,所有页面加载都很好。我已经确认我的nginx配置没有语法问题:
nginx -t -c /etc/nginx/nginx.conf
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
nginx conf: (实际上下面是来自nginx-django.conf,nginx.conf只是安装时附带的默认配置)
#HTTPS server configuration
upstream django_app {
server 127.0.0.1:9000;
}
server {
listen 8443 ssl;
server_name _;
ssl on;
ssl_certificate /etc/nginx/ssl/cert.crt;
ssl_certificate_key /etc/nginx/ssl/cert.key;
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
ssl_prefer_server_ciphers on;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
location / {
proxy_pass http://django_app;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_connect_timeout 1800;
proxy_read_timeout 1800;
proxy_send_timeout 1800;
}
location ^/static/ {
alias /home/vagrant/gentools/static;
expires 3d;
}
}
我的 supervisord 配置如下,启动时没有错误:
[unix_http_server]
file=/tmp/supervisor.sock ; (the path to the socket file)
[supervisord]
logfile=/tmp/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10 ; (num of main logfile rotation backups;default 10)
loglevel=info ; (log level;default info; others: debug,warn,trace)
pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false ; (start in foreground if true;default false)
minfds=1024 ; (min. avail startup file descriptors;default 1024)
minprocs=200 ; (min. avail process descriptors;default 200)
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[program:gunicorn]
directory=/home/vagrant/gentools
command=/home/vagrant/venv/bin/gunicorn --workers 1 --bind 0.0.0.0:9000 gentools.wsgi:application
autostart=true
autorestart=true
stderr_logfile=/tmp/gunicorn.out.log
stdout_logfile=/tmp/gunicorn.err.log
user=vagrant
environment=PYTHONPATH=/home/vagrant/gentools/gentools,DJANGO_SETTINGS_MODULE=settings,LANG=en_US.UTF-8,LC_ALL=en_US.UTF-8
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket
我的文件夹结构如下:
├── gentools
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── settings.py
│ ├── settings.pyc
│ ├── urls.py
│ ├── urls.pyc
│ ├── wsgi.py
│ └── wsgi.pyc
├── manage.py
├── static
│ ├── admin
│ ├── css
│ ├── images
│ └── js
├── supervisor
│ ├── conf.d
│ └── supervisord.conf
└── templates
├── base.html
├── home.html
├── logged_out.html
└── login.html
正如我所提到的,我可以使用nginx / supervisor成功启动django应用程序,我在日志中看到的唯一错误与未加载的文件有关:
Not Found: /static/images/my_logo.jpg
来自 settings.py :
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates'),],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
STATIC_URL = '/static/'
STATICFILES_DIRS = [
'/home/vagrant/gentools/static/',
os.path.join(BASE_DIR, "static"),
]
来自 urls.py:
from django.conf.urls import url
from django.contrib import admin
from django.contrib.auth import views as auth_views
from django.views.generic.base import TemplateView
from django.contrib.auth.decorators import login_required
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', login_required(TemplateView.as_view(template_name='home.html')), name='home'),
url(r'^login/$', auth_views.login, {'template_name': 'login.html'}, name='login'),
url(r'^logout/$', auth_views.logout, {'template_name': 'logged_out.html'}, name='logout'),
]
最后来自我的模板 base.html :
{% load static %}
<img src={% static "images/my_logo.jpg" %}>
提前感谢!
答案 0 :(得分:0)
nginx.conf的location ^/static {
部分解决了这个问题。
有关详细信息,请参阅nginx文档: http://nginx.org/en/docs/http/ngx_http_core_module.html#location
此外,您可能希望改用uWSGI。
答案 1 :(得分:0)
感谢您及时回复qnnnnez。我得到了一个很好的阅读感谢链接,但没有找到为什么该行导致问题。我尝试了 / static / , ^ / static / 等的所有排列。我做了一些谷歌搜索和摆弄,这似乎解决了我的问题:
修改 urls.py 以包含:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
....
] + staticfiles_urlpatterns()
我还修改了nginx配置的位置部分,不确定这是否有帮助我没有时间回去检查:
nginx config:
location /static/ {
autoindex on;
alias /home/vagrant/gentools/static/;
expires 3d;
}
我不再从gunicorn那里得到错误。现在我获得了nginx权限,我现在要解决这个问题并超出了这个问题的范围。
希望以上帮助某人。