6

I've set up NGINX to return custom error documents for my proxied server, it shows the correct file but always returns a 200 OK header.

The relevant NGINX config is

server {
    listen       94.23.155.32:80;
    server_name  rmg.io www.rmg.io;

    proxy_intercept_errors on;

    location / {
        proxy_pass http://rmgshort/;
    }

    error_page 404 = /error/404.html;
error_page 500 501 502 503 503 = /error/500.html;

    location /error/ {
        root /var/rmg/;
    }
}

You can test this if you want, this page should return a 404 error, it returns the correct document but changes the status code to '200 OK' (Test HTTP headers here), if I replace root /var/rmg/ with internal; the correct header is returned but then my custom error page doesn't work.

How do I get NGINX to return my custom error document with the correct status header?

I'm running NGINX 1.0.4 on RHEL 6.1

4
  • RedHat 6.0, or RHEL 6.0?
    – womble
    Jul 30, 2011 at 12:18
  • @womble RHEL 6.1, sorry I just always refer to it as RedHat =)
    – user80776
    Jul 30, 2011 at 12:19
  • @womble Also not sure why I put 6.0 in the question #facepalm
    – user80776
    Jul 30, 2011 at 12:20
  • I don't think it actually matters here.
    – rvs
    Jul 30, 2011 at 12:59

1 Answer 1

11

Drop the = out of the error_page directive; you can specify any return code you like with =NNN (eg =401), but if you give a bare = it means "use the error code of the error handler", which for a static file will always be "200 OK". Without any =, you'll get the original error code returned.

Irritatingly, a straightforward reading of the (otherwise) fine manual (as at the time of writing this answer, anyway) might make you think that it's the other way around (= keeps the original return code), but local testing indicates that it definitely works the way I've described above.

2
  • 3
    This is not completely accurate. A bare = means that Nginx will use the returned status code of the designated error handler. Meaning if you do error_page 404 = /handler.phpand your handler.php script returns 200 then it uses 200, if handler.php returns 409 then it uses 409. I agree, though, that the wiki entry is worded poorly and I'll try to clarify it a bit. Jul 30, 2011 at 12:52
  • @Martin: Thanks for the clarification; I've never actually used a non-static error handler (and obviously a non-existent error handler's pointless), so I'd never come across a situation where the return code would be non-200. I've updated my answer accordingly.
    – womble
    Jul 30, 2011 at 12:53

You must log in to answer this question.