python 使用 mod_wsgi 和 Django 提供静态文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/732190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 20:46:04  来源:igfitidea点击:

Serving static files with mod_wsgi and Django

pythondjangomod-wsgimod-python

提问by unmounted

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. 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_python allows the use of Apache directives like:

我有一个使用 mod_python 的 django 应用程序,这是相当典型的配置,除了媒体文件由文档根目录中的(我知道,不推荐)“媒体”目录提供服务。我想测试并可能使用 mod_wsgi 进行部署,但我不知道如何创建一些简单的东西来提供静态文件。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. 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? Answers that point to entirely different approaches are welcome.

django 文档似乎指出上面的第二个块是对 mod_wsgi 进行类似例外的正确方法,但在我的测试中,root 下的所有内容仍被发送到 wsgi 应用程序。是否有使用 mod_wsgi 设置静态媒体目录的好方法,或者由于令人信服的技术原因,我故意不支持我尝试做的事情?欢迎提供指向完全不同方法的答案。

回答by Van Gale

I run a a 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 Agrade 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 afterthe Alias /media in your configuration file (because Apache looks at the aliases in order)

...应该在配置文件中的 Alias /media之后(因为 Apache 按顺序查看别名)

回答by Graham Dumpleton

The mod_wsgi documentation explains how to setup static files which appear at a URL underneath that which the WSGI application is mounted at. See:

mod_wsgi 文档解释了如何设置静态文件,这些文件出现在挂载 WSGI 应用程序的 URL 下方。看:

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. The 'ExecCGI' option is only required when using AddHandler to mount applications as resources.

请注意,使用 WSGIScriptAlias 指令挂载 WSGI 应用程序时不需要“Options +ExecCGI”。'ExecCGI' 选项仅在使用 AddHandler 将应用程序挂载为资源时才需要。