settings配置
# 网站引用静态文件时都会加上该地址,如:http://www.xxx.com/static/css/mini.css
STATIC_URL = '/static/'
# 静态文件根目录,执行命令`python manage.py collectstatic`可以把静态文件都收集到该目录下
STATIC_ROOT = BASE_DIR + "/performance/static/"
# 静态文件原始文件所在目录
STATICFILES_DIRS = (
os.path.join(os.path.dirname(__file__), '../performance/templates/').replace('\\', '/'),
# os.path.join(os.path.dirname(__file__), 'static/').replace('\\', '/'),
)
引用静态文件
在html模板中引入
{% load static %}
<link href="{% static "css/bootstrap.min.css" %}" rel="stylesheet" type="text/css" />
开发环境不需要其他配置就可以直接请求静态文件
部署环境的配置(nginx为例)
在配置文件.conf文件中设置
upstream bbs1.linuxtone.com {
server 127.0.0.1:8552;
}
server {
listen 8555;
server_name auto1.apitest.com;
index index.php index.html index.htm;
// 直接copy需要删掉注释:
// 静态文件配置,static只是个别名,可以改成其他的别名
// 目录root+static(D:/A/B/static/)是django的
// settings中的STATIC_ROOT,需要能够找到实际的静态文件
location ~ ^/static/ {
root D:/A/B/;
expires 30d;
break;
}
location ~ ^/ {
root D:/A/;
proxy_redirect off ;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 50m;
client_body_buffer_size 256k;
proxy_connect_timeout 30;
proxy_send_timeout 30;
proxy_read_timeout 60;
proxy_buffer_size 256k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
proxy_pass http://bbs1.linuxtone.com;
}