S
S
step2n2016-02-04 11:20:09
Nginx
step2n, 2016-02-04 11:20:09

How to pass error 500 from node.js (express.js) to nginx?

There is a page of 500s, which is shown in the case of one of the 500s:

error_page 500 501 502 503 504 @500;

    location @500 {
        internal;
        rewrite ^(.*)$ /500;
    }

    location = /500 {
        rewrite ^(.*)$ /500.$lang.html;
    }

    location ~ /500 {
        root $root/500/$bundle;
    }

Works great when the server is not up.
But then how do you test it? You need to somehow process the url, for example /500, which nginx will do.
Something like this:
app.get('/503', function(req, res) {
    res.status(500).send('internal error');
});

But nothing happens, as if nginx needs to be tuned.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lynn "Coffee Man", 2016-02-04
@Lynn

Gotta tune it up. By default, nginx does not try to process responses from upstream (from the node) in any way, but simply passes them on to the client. You need the proxy_intercept_errors directive .
Well, your error handling is written strangely.
In general, it should be something like this:

error_page 500 501 502 503 504 @500;

location / {
  # проксирование в node
  proxy_pass http://127.0.0.1:3000;
  proxy_intercept_errors on;
}

location @500 {
  # internal не нужен, в именованый location по другому всё равно не попасть
  root $root/500/$bundle;
  rewrite ^ /500.$lang.html break;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question