【发布时间】: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 已提供配置和必要的信息。