使用 apache 和 mod_wsgi 提供静态文件而不更改 apache 的配置?

发布于 2024-09-28 04:53:44 字数 274 浏览 3 评论 0原文

我有一个 Django 应用程序,并且使用共享服务器托管,因此我无法更改 apache 的配置文件。我唯一可以更改的是应用程序中的 .htaccess 文件。我还有一个标准的 django.wsgi python 文件作为入口点。

在开发环境中,我使用 Django 来提供静态文件,但官方文档中不鼓励这样做,说你应该使用 Web 服务器来执行此操作。

有没有一种方法可以通过 apache 提供静态文件,而无需访问 Apache 的配置,仅更改 .htaccess 或 django.wsgi 文件?

I have a Django application, and I'm using a shared server hosting, so I cannot change apache's config files. The only thing that I can change is the .htaccess file in my application. I also have a standard django.wsgi python file, as an entry point.

In dev environment, I'm using Django to serve the static files, but it is discouraged in the official documentation, saying that you should do it using the web server instead.

Is there a way to serve static files through apache without having access to Apache's configuration, changing only the .htaccess or django.wsgi files??

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

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

发布评论

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

评论(2

時窥 2024-10-05 04:53:44

第一步是仅添加

AddHandler wsgi-script .wsgi

到 .htaccess 文件中,不添加任何其他内容来将 wsgi 建立为处理程序。这将向 django.wsgi 和 django.wsgi/whatever 发出请求,并将其发送到您的 django 应用程序。

要使 URL 中的 django.wsgi 部分消失,您需要使用 mod_rewrite。希望您的主机已启用它。一个例子是,

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /django.wsgi/$1 [QSA,PT,L]

如果 URL 与文件匹配,它将提供该文件,如果不匹配,则由 django 提供服务。另一种选择是

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !^/?static/
RewriteRule ^(.*)$ /django.wsgi/$1 [QSA,PT,L]

对 /static/* 发出请求到文件本身,而其他所有内容都通过 django。

然后您需要从生成的 URL 中隐藏 django.wsgi。这可以在 django.wsgi 中使用这样的代码片段来完成。

def _application(environ, start_response):
    # The original application.
    ...

import posixpath

def application(environ, start_response):
    # Wrapper to set SCRIPT_NAME to actual mount point.
    environ['SCRIPT_NAME'] = posixpath.dirname(environ['SCRIPT_NAME'])
    if environ['SCRIPT_NAME'] == '/':
        environ['SCRIPT_NAME'] = ''
    return _application(environ, start_response)

如果这不太正确,那么请务必参考 http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines。我从那里提取了答案的大部分细节,但只是一步一步地将正确的部分组合在一起。

The first step is to add just

AddHandler wsgi-script .wsgi

to your .htaccess file with nothing else to establish wsgi as the handler. This will make requests to django.wsgi and django.wsgi/whatever go to your django app.

To make the django.wsgi part of the URL go away, you will need to use mod_rewrite. Hopefully your host has it enabled. An example is

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /django.wsgi/$1 [QSA,PT,L]

which will serve the file if the URL matches a file, or be served by django if it doesn't. Another option would be

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !^/?static/
RewriteRule ^(.*)$ /django.wsgi/$1 [QSA,PT,L]

to make requests for /static/* go to the file itself and everything else to go through django.

Then you will need to hide django.wsgi from generated URLs. This can be done with a snippet like this in your django.wsgi

def _application(environ, start_response):
    # The original application.
    ...

import posixpath

def application(environ, start_response):
    # Wrapper to set SCRIPT_NAME to actual mount point.
    environ['SCRIPT_NAME'] = posixpath.dirname(environ['SCRIPT_NAME'])
    if environ['SCRIPT_NAME'] == '/':
        environ['SCRIPT_NAME'] = ''
    return _application(environ, start_response)

If this doesn't work out quite exactly right, then be sure to consult http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines. I pulled most of the details of the answer from there but just got the right parts put together as a step by step.

℉絮湮 2024-10-05 04:53:44

从不同的虚拟主机提供静态文件。

Serve the static files from a different virtual host.

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