简体   繁体   English

使用mod_wsgi和Django提供静态文件

[英]Serving static files with mod_wsgi and Django

I have a django application using mod_python, fairly typical configuration except that media files are being served by a (I know, not recommended) 'media' directory in the document root. 我有一个使用mod_python的django应用程序,相当典型的配置,除了媒体文件由文档根目录中的(我知道,不推荐)'media'目录提供服务。 I would like to test and maybe deploy with mod_wsgi but I cannot figure out how to create something simple to serve static files. 我想测试并使用mod_wsgi进行部署,但我无法弄清楚如何创建简单的服务静态文件。 mod_python allows the use of Apache directives like: mod_python允许使用Apache指令,如:

<Location '/'>
    SetHandler MyApplication.xyz.....
</Location>

<Location '/media'>
    SetHandler None
</Location>

The django docs seem to point to the second block above as the correct way to make a similar exception for mod_wsgi, but in my tests everything below root is still being sent to the wsgi app. django文档似乎指向上面的第二个块作为为mod_wsgi制作类似异常的正确方法,但在我的测试中,根目录下的所有内容仍然被发送到wsgi应用程序。 Is there a good way set a static media directory with mod_wsgi, or is what I am trying to do intentionally unsupported for compelling technical reasons? 有没有一个很好的方法用mod_wsgi设置一个静态媒体目录,或者我是否因为引人注目的技术原因故意不支持? Answers that point to entirely different approaches are welcome. 欢迎指出完全不同的方法的答案。

I run aa dozen or so Django sites on the same server and here's how I configure the media URL's. 我在同一台服务器上运行了十几个Django站点,这是我配置媒体URL的方式。

Each VirtualHost has the following configuration: 每个VirtualHost都具有以下配置:

Alias /media /path/to/media/
<Directory /path/to/media>
    Include /etc/apache2/vhosts.d/media.include
</Directory>

This way I can make any changes to the media handling in one file. 这样我就可以在一个文件中对媒体处理进行任何更改。

Then, my media.include file looks like this: 然后,我的media.include文件如下所示:

Order allow,deny
Allow from all
SetHandler None
FileETag none
Options FollowSymLinks

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/gif "access plus 30 days"
    ExpiresByType image/jpg "access plus 30 days"
    ExpiresByType image/png "access plus 30 days"
    ExpiresByType image/jpeg "access plus 30 days"
    ExpiresByType text/css "access plus 30 days"
    ExpiresByType application/x-javascript "modification plus 2 years"
</IfModule>

<IfModule mod_headers.c>
    Header append Vary Accept-Encoding
</IfModule>

AddOutputFilterByType DEFLATE text/html text/css text/plain

This has worked very well for me, and gets an A grade from YSlow (also see Jeff Atwood on YSlow ). 这对我来说非常有效,并从YSlow获得A等级( 在YSlow上也见到Jeff Atwood )。

Also note, for the root dir I use the following configuration: 另请注意,对于根目录,我使用以下配置:

WSGIScriptAlias / /path/to/app.wsgi
<Directory /path/to>
    Options +ExecCGI
    Allow from all
</Directory>

... which should be after the Alias /media in your configuration file (because Apache looks at the aliases in order) ...应该在配置文件中的Alias / media 之后 (因为Apache按顺序查看别名)

The mod_wsgi documentation explains how to setup static files which appear at a URL underneath that which the WSGI application is mounted at. mod_wsgi文档解释了如何设置静态文件,这些文件出现在安装了WSGI应用程序的URL下面。 See: 看到:

http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines#Hosting_Of_Static_Files http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines#Hosting_Of_Static_Files

Note that 'Options +ExecCGI' is not need when using WSGIScriptAlias directive to mount the WSGI application. 请注意,使用WSGIScriptAlias指令安装WSGI应用程序时不需要“Options + ExecCGI”。 The 'ExecCGI' option is only required when using AddHandler to mount applications as resources. 只有在使用AddHandler将应用程序作为资源挂载时,才需要“ExecCGI”选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM