在 Apache 2.2 上运行 Django&mod_wsgi 时提供静态文件服务
我已经更改了 Apache 2.2 的 httpd.conf
#Serve static files
Alias /static/ "E:\Python\Django\carlsblog\static\"
<Directory "E:\Python\Django\carlsblog\static">
Order allow,deny
Options Indexes
Allow from all
IndexOptions FancyIndexing
</Directory>
#Start mod_wsgi as default handler
WSGIScriptAlias / "E:\Python\Django\carlsblog\django.wsgi"
<Directory "E:\Python\Django\carlsblog">
Allow from all
</Directory>
并且我的应用程序可以成功运行,只有静态文件无法访问,Django 抛出 404
但是,如果我将此行更改
Alias /static/ "E:\Python\Django\carlsblog\static\"
为
Alias /static/ "E:\Python\Django\carlsblog\static"
静态文件夹的索引页可以已看到,但无法访问文件。当我此时检查Apache的错误日志时,我发现了这样的内容:
File does not exist "E:\Python\Django\carlsblog\staticmy_static_file.txt"
似乎少了一个“\”,有人有解决这个问题的想法吗?
I have changed my Apache 2.2's httpd.conf
#Serve static files
Alias /static/ "E:\Python\Django\carlsblog\static\"
<Directory "E:\Python\Django\carlsblog\static">
Order allow,deny
Options Indexes
Allow from all
IndexOptions FancyIndexing
</Directory>
#Start mod_wsgi as default handler
WSGIScriptAlias / "E:\Python\Django\carlsblog\django.wsgi"
<Directory "E:\Python\Django\carlsblog">
Allow from all
</Directory>
and my app can be successfully run, only the static files cannot be accessed, a 404 is thrown by Django
However, if I change this line
Alias /static/ "E:\Python\Django\carlsblog\static\"
into
Alias /static/ "E:\Python\Django\carlsblog\static"
the index page of the static folder can be seen, but the files cannot be reached. When I checked Apache's error log at this time, I found something like this:
File does not exist "E:\Python\Django\carlsblog\staticmy_static_file.txt"
it seems there's a "\" missing, anyone has an idea on fixing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试从别名本身中删除尾部斜杠:
更新(2015):这个答案来自2011年。目前,使用gunicorn + nginx来为django应用程序提供服务被认为是更好的做法。
Try removing the trailing slash from the alias itself:
Update (2015): This answer is from 2011. Currently, it is considered a much better practice to use gunicorn + nginx to serve django apps.
你的模板还好吗?在浏览器中使用
View Source
检查生成的 html。Are your templates OK? Check your generated html using
View Source
in your browser.