Nginx return 444 depending on upstream response code

nginx

I have nginx setup to pass to an upstream using proxy pass. The upstream is written to return a 502 http response on certain requests, rather then returning the 502 with all the header I would like nginx to recoginse this and return 444 so nothing is returned. Is this possible?

I also tried to return 444 on any 50x error but it doesn't work either.

location / {
    return 444;
}


location ^~ /service/v1/ {
    proxy_pass http://127.0.0.1:3333;
    proxy_next_upstream error timeout http_502;
    error_page  500 502 503 504  /50x.html;
}

location = /50x.html {
    return 444;
}

error_page  404              /404.html;
location = /404.html {
    return 444;
}

Best Answer

Yes, it is possible. As per the documentation of error_page, it could be done like this...

error_page  500 502 503 504 =444 /50x.html;

In the same way...

error_page 404 =444 /404.html;

So, your entire sample configuration would become...

location / {
    return 444;
}

location ^~ /service/v1/ {
    proxy_pass http://127.0.0.1:3333;
    proxy_next_upstream error timeout http_502;
    error_page  500 502 503 504 =444 /50x.html;
}

error_page  404 =444 /404.html;