某个django项目,用户上传的文件存储在 /star/uploads目录;
对应的URI为 http://www.demo.com/attachments/mylist.csv 。
项目需要的静态文件(css、js、pic)存储在 /star/static目录;
对应的URI为 http://www.demo.com/static/js/jquery.js 。
1. 项目目录
tree -L 1 -d star/ star/ ├── star ├── static ├── templates ├── uploads └── web 5 directories
2. 这里使用Nginx反向代理django ,对于 URI能够匹配的/static 与 /star/static目录来说,我们可以使用nginx的 root指令,例如:
location ^~ /static { root /star/; }
3. 但是对于 URI不能匹配的/attachments 与 /star/uploads目录来说,我们该如何处理呢?
对啦答案就是nginx的alias指令,例如:
location ^~ /attachments { alias /star/uploads/; }
# 项目的nginx 配置文件
cat /etc/nginx/conf.d/default.conf server { listen 80; server_name www.demo.com; #..其它配置项目省略 # alias 到文件 location = /baidu_verify.html { alias /star/static/baidu_verify.html; } # alias 到目录 location ^~ /attachments { alias /star/uploads/; expires 180d; } location ^~ /static { root /star/; expires 60d; } location / { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $http_host; proxy_pass http://unix:/var/run/django.socket; proxy_redirect default; } }
语法: alias file-path|directory-path; 默认值: no 使用字段: location 功能: 这个指令为location指定一个路径,它类似于root指令但是$document_root没有改变,只是请求响应使用了别名目录的文件。
本文分享自微信公众号 - 运维录(gh_70d95b8f5f7c),作者:东南
原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。
原始发表时间:2019-10-14
本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。
我来说两句