使用 mod_wsgi 和 Django 提供静态文件

发布于 2024-07-17 14:54:37 字数 508 浏览 5 评论 0原文

我有一个使用 mod_python 的 django 应用程序,这是相当典型的配置,除了媒体文件由文档根目录中的(我知道,不推荐)“media”目录提供服务。 我想测试并可能使用 mod_wsgi 进行部署,但我不知道如何创建一些简单的东西来提供静态文件。 mod_python 允许使用 Apache 指令,例如:

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

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

django 文档似乎指出上面的第二个块是为 mod_wsgi 生成类似异常的正确方法,但在我的测试中,root 以下的所有内容仍然被发送到 wsgi 应用程序。 有没有一种使用 mod_wsgi 设置静态媒体目录的好方法,或者出于令人信服的技术原因,我试图做的事情是故意不受支持的? 欢迎指出完全不同方法的答案。

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:

<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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染火枫林 2024-07-24 14:54:37

我在同一台服务器上运行十多个 Django 站点,以下是我配置媒体 URL 的方法。

每个虚拟主机都有以下配置:

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

这样我就可以在一个文件中对媒体处理进行任何更改。

然后,我的 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

这对我来说非常有效,并从 YSlow (另请参阅 杰夫·阿特伍德 (Jeff Atwood) 在 YSlow 上)。

另请注意,对于根目录,我使用以下配置:

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

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

I run a a dozen or so Django sites on the same server and here's how I configure the media URL's.

Each VirtualHost has the following configuration:

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:

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).

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)

等数载,海棠开 2024-07-24 14:54:37

mod_wsgi 文档解释了如何设置静态文件,这些文件出现在 WSGI 应用程序安装所在 URL 下方。 请参阅:

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

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

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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文