django - 如何在 Django 中提供静态媒体文件?
问题描述
我是 Web Server Apache 的初学者。我想问你们如何在 Django 中提供媒体文件。
我读到这个:https ://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/modwsgi/#serving-files
This example sets up Django at the site root, but serves robots.txt, favicon.ico, and anything in the /static/ and /media/ URL space as a static file. All other URLs will be served using mod_wsgi:
Alias /robots.txt /path/to/mysite.com/static/robots.txt
Alias /favicon.ico /path/to/mysite.com/static/favicon.ico
Alias /media/ /path/to/mysite.com/media/
Alias /static/ /path/to/mysite.com/static/
<Directory /path/to/mysite.com/static>
Require all granted
</Directory>
<Directory /path/to/mysite.com/media>
Require all granted
</Directory>
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
If you are using a version of Apache older than 2.4, replace Require all granted with Allow from all and also add the line Order deny,allow above it.
我问 Web Server 支持者,他们说我的服务器已经支持 mod_wsgi 模式,他们对我说我可以在.htaccess中配置它。但我真的对此一无所知。请帮我重写网址:
mydomain.com/served/
用 path 将URL 重写为文件夹/home/mydomain.com/public_html/media
。中的所有文件都/home/mydomain.com/public_html/media/filename.jpg
将使用mydomain.com/served/
解决方案
您应该能够使用该Alias
指令为相关媒体文件夹设置别名/served/
。例如:
Alias /served/ /home/example.com/public_html/media/
这意味着对的请求http://example.com/served/filename.jpg
将映射到/home/example.com/public_html/media/filename.jpg
。您可以对/static/
.
(请注意,上面的示例使用example.com
而不是mydomain.com
由于SO 限制。)
推荐阅读
- for-loop - 如何重用具有相同参数的循环方法
- networking - 深入了解 DNS 查询的工作原理
- .net - 无法从启用 cors 的 .net API 上的不同 Angular 项目 url 获取令牌
- angular - 如何将ngrx商店嵌入基于角度的包中,同时可以安装在消费者身上?
- typescript - 如何修补打字稿声明文件以导出 UMD 全局?
- python - Django - makemigrations - 未检测到更改/找不到应用程序
- java - 无法为调试器绑定到本地 8632
- azure-ad-b2c - 如何使用自定义策略根据扩展声明类型防止 AD B2C 中的登录
- go - 如何找出执行路径中的 time.Sleep()?
- python - 如何在 python 中使用仅捕获部分单词的 RegEx 搜索来获取完整字符串?