Question

I have a bare WSGI application in a module called app (app.py) that looks like this:

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']

I am serving this app using nginx + uwsgi with this configuration.

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;
    }
}

I start uwsgi with this command:

uwsgi --socket 127.0.0.1:9090 --module app

The app behaves as expected:

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>

However, I am not happy with the HTTP 404 'Page Not Found' response. I would want that when my WSGI app wants to send HTTP 404 response, the default HTTP 404 error page of nginx is sent to the client.

Here is how the default HTTP 404 response of nginx looks like. Note the response below is from the default virtual host (not the myhost virtual host used in the examples above). The default virtual host doesn't have any WSGI application and hence you are able to see the default HTTP 404 error page of nginx in the output below.

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:~#

Is there a way for my WSGI application to tell the web server serving it to send its HTTP 404 response to the client?

Note:

  1. I want the solution to be web-server agnostic, i.e. I shouldn't have to hard-code the web server's HTTP 404 page in my code or template, or I shouldn't have to read some HTML that is very specific to nginx. The solution should work on nginx or any other web-server like Apache, lightttpd, etc.
  2. I am aware that one should be using WSGI framework to do actual web development. I am probably going to settle for bottle.py but before doing so, I am trying to understand the capabilities and limitations of WSGI so that I know what is going on behind the scenes when I use bottle.
Was it helpful?

Solution

From the discussion in the comments section, the answer seems to be:

No!

OTHER TIPS

See if uWSGI in combination with nginx supports:

If it doesn't support it with its native protocol, you may have to run uWSGI in HTTP accept mode and use normal nginx proxy mode via HTTP. Then you could definitely use it.

Either way, get on IRC on the #uwsgi channel and ask unbit. Likely they will add the feature before you can finish the conversation. He is like that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top