NGINX return correct headers with custom error documents

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


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.