【问题标题】:how to make django render the default home page instead of apache如何使 django 呈现默认主页而不是 apache
【发布时间】:2017-09-28 15:48:58
【问题描述】:

众所周知,在通过 web explorer 发出带有唯一 IP 或服务器名称的请求后,apache 会将其默认的静态主页返回给 explorer。我正在使用带有 mod_wsgi 的 django 和 apache 来部署我的网站(即 apache Web 服务器上的 django 应用程序)。当我向运行 apache 的服务器发出请求时,如何将处理路由到 views.py 中定义的 django 映射规则,以便为该站点动态生成主页(即索引页)?提前致谢!

我在主 Apache 'httpd.conf' 中添加了以下配置:

<VirtualHost 127.0.0.1:80>

    ServerName 127.0.0.1
    ServerAlias example.com
    ServerAdmin webmaster@example.com

    DocumentRoot /usr/local/www/documents

    Alias /robots.txt /usr/local/www/documents/robots.txt
    Alias /favicon.ico /usr/local/www/documents/favicon.ico

    Alias /media/ /usr/local/www/documents/media/

    <Directory /usr/local/www/documents>
    Require all granted
    </Directory>

    WSGIDaemonProcess 127.0.0.1 processes=2 threads=15 display-name=%{GROUP}
    WSGIProcessGroup 127.0.0.1

    WSGIScriptAlias / /usr/local/www/wsgi-scripts/myapp.wsgi

    <Directory /usr/local/www/wsgi-scripts>
    Order allow,deny
    Allow from all
    </Directory>

</VirtualHost>

myapp.wsgi 中的代码:

def application(environ, start_response):
    status = '200 OK'
    output = b'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

然后我发出127.0.0.1,资源管理器显示'It works!',这是apache生成的默认页面,而不是我想要'Hello world!'在这种情况下返回。我只是希望在刚刚发布一个IP或站点地址时,将生成默认主页的工作直接委托给django来动态处理,而不是由Apache返回一个静态的index.html。虽然我配置了 WSGIScriptAlias / /usr/local/www/wsgi-scripts/myapp.wsgi ,但这似乎仍然行不通。

【问题讨论】:

  • 不,它不知道。除非您特别要求,否则它不会提供“默认静态主页”。你最初是如何配置 mod_wsgi 的?
  • 正如@DanielRoseman 所要求的,提供您正在使用的配置。如果您添加了WSGIScriptAlias / /some/path/to/wsgi.py 和适当的其他配置以在适当的位置启用对该资源的访问,这将导致 WSGI 应用程序接管对站点根目录的请求。如果不是,那么您可能搞砸了虚拟主机配置。
  • @GrahamDumpleton 已提供配置和必要的信息。

标签: django apache mod-wsgi


【解决方案1】:

虚拟主机不适用于 127.0.0.1,因为它被视为有点特殊。除非这是您的配置中唯一的 VirtualHost,否则将会发生的情况是 Apache 将回退到使用它找到的第一个 VirtualHost 定义,通常是默认站点。

所以通常你会:

<VirtualHost *:80>
    ServerName my.fqdn.host.name
    ...
</VirtualHost>

也就是说,VirtualHost 指令中没有 IP,ServerName 是出现在 URL 中的实际主机名,而不是 IP 地址。

httpd.conf 中是否存在先前的 VirtualHost,或者是否包括 extra/httpd-vhosts.conf,这会导致 VirtualHost 取代这个?

【讨论】:

    猜你喜欢
    • 2012-05-27
    • 2021-07-14
    • 1970-01-01
    • 2021-10-27
    • 2012-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-02
    相关资源
    最近更新 更多