繁体   English   中英

WSGI应用程序可以告诉服务它的Web服务器发送其默认的HTTP 404错误页面吗?

[英]Can a WSGI application tell the web server serving it to send its default HTTP 404 error page?

我在名为app (app.py)的模块中有一个裸露的WSGI应用程序,如下所示:

def application(environ, start_response):
    path = environ['PATH_INFO']
    if path == '/':
        start_response('200 OK', [('Content-Type','text/html')])
        return [b'<p>It works!</p>\n']
    elif path == '/foo':
        start_response('200 OK', [('Content-Type','text/html')])
        return [b'<p>Hello World</p>\n']
    else:
        start_response('404 Not Found', [('Content-Type','text/html')])
        return [b'<p>Page Not Found</p>\n']

我在此配置下使用nginx + uwsgi服务此应用程序。

nifty:/www# cat /etc/nginx/sites-enabled/myhost
server {
        listen 8080;
        root /www/myhost;
        index index.html index.htm;
        server_name myhost;
    location / {
        uwsgi_pass 127.0.0.1:9090;
        include uwsgi_params;
    }
}

我使用以下命令启动uwsgi:

uwsgi --socket 127.0.0.1:9090 --module app

该应用程序的行为符合预期:

debian:~# curl -i http://myhost:8080/
HTTP/1.1 200 OK
Server: nginx/1.4.4
Date: Mon, 24 Mar 2014 13:05:51 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive

<p>It works!</p>
debian:~# curl -i http://myhost:8080/foo
HTTP/1.1 200 OK
Server: nginx/1.4.4
Date: Mon, 24 Mar 2014 13:05:55 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive

<p>Hello World</p>
debian:~# curl -i http://myhost:8080/bar
HTTP/1.1 404 Not Found
Server: nginx/1.4.4
Date: Mon, 24 Mar 2014 13:05:58 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive

<p>Page Not Found</p>

但是,我对HTTP 404“找不到页面”响应不满意。 我想当我的WSGI应用程序想要发送HTTP 404响应时,将nginx的默认HTTP 404错误页面发送到客户端。

这是nginx的默认HTTP 404响应的样子。 请注意,以下响应来自默认虚拟主机(不是以上示例中使用的myhost虚拟主机)。 默认的虚拟主机没有任何WSGI应用程序,因此您可以在下面的输出中看到nginx的默认HTTP 404错误页面。

debian:~# curl -i http://localhost:8080/bar
HTTP/1.1 404 Not Found
Server: nginx/1.4.4
Date: Mon, 24 Mar 2014 13:06:06 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive

<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.4.4</center>
</body>
</html>
debian:~#

我的WSGI应用程序是否可以告诉服务它的Web服务器将其HTTP 404响应发送给客户端?

注意:

  1. 我希望解决方案与Web服务器无关,即,我不必在代码或模板中对Web服务器的HTTP 404页面进行硬编码,或者我不必阅读一些特定于nginx的HTML。 该解决方案应可在Nginx或任何其他Web服务器(如Apache,lightttpd等)上使用。
  2. 我知道应该使用WSGI框架进行实际的Web开发。 我可能会选择bottle.py,但在此之前,我试图了解WSGI的功能和局限性,以便我了解使用Bottle时的幕后情况。

从评论部分的讨论中,答案似乎是:

没有!

查看结合了nginx的uWSGI是否支持:

如果它的本机协议不支持它,则可能必须在HTTP接受模式下运行uWSGI并通过HTTP使用普通的nginx代理模式。 然后,您肯定可以使用它。

无论哪种方式,请在#uwsgi频道上进入IRC并要求取消。 他们可能会在您完成对话之前添加该功能。 他就是那样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM