Empty URI in redirect to named location when redirecting to named location on handling custom error in ingress-nginx

41 Views Asked by At

I set custom-http-errors in ingress-nginx helm chart to handle errors. But when the request URI is too long or nginx cannot parse the URI, it returns the default nginx error page, not my custom error page with the following log:

curl localhost:32080/% -v

*   Trying 127.0.0.1:32080...
* Connected to localhost (127.0.0.1) port 32080 (#0)
> GET /% HTTP/1.1
> Host: localhost:32080
> User-Agent: curl/7.81.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 500 Internal Server Error
< Date: Sat, 09 Mar 2024 14:41:36 GMT
< Content-Type: text/html
< Content-Length: 170
< Connection: close
<
<html>
<head><title>500 Internal Server Error</title></head>
<body>
<center><h1>500 Internal Server Error</h1></center>
<hr><center>nginx</center>
</body>
</html>

Log:

2024/03/09 14:41:36 [error] 30#30: *2048 empty URI in redirect to named location "@custom_upstream-default-backend_400" while reading client request line, client: 10.10.196.0, server: _, request: "GET /% HTTP/1.1"

I couldn't find a way to rewrite the URI if it's empty before error_page handles it in nginx or lua on openresty.

The configuration is simple

helm chart value:

controller:
  config:
    custom-http-errors: 400,401,402,403,404,405,406,...,599

defaultBackend:
  enabled: true
  image:
    image: ingress-nginx/nginx-errors

The generated nginx.conf:

http {
        error_page 400 = @custom_upstream-default-backend_400;
        error_page 401 = @custom_upstream-default-backend_401;
        error_page 402 = @custom_upstream-default-backend_402;
        error_page 403 = @custom_upstream-default-backend_403;
        .
        .
        .
        location @custom_upstream-default-backend_401 {
                        internal;

                        # Ensure that modsecurity will not run on custom error pages or they might be blocked

                        proxy_intercept_errors off;

                        proxy_set_header       X-Code             401;
                        proxy_set_header       X-Format           $http_accept;
                        proxy_set_header       X-Original-URI     $request_uri;
                        proxy_set_header       X-Namespace        $namespace;
                        proxy_set_header       X-Ingress-Name     $ingress_name;
                        proxy_set_header       X-Service-Name     $service_name;
                        proxy_set_header       X-Service-Port     $service_port;
                        proxy_set_header       X-Request-ID       $req_id;
                        proxy_set_header       X-Forwarded-For    $remote_addr;
                        proxy_set_header       Host               $best_http_host;

                        set $proxy_upstream_name "upstream-default-backend";

                        rewrite                (.*) / break;

                        proxy_pass            http://upstream_balancer;
                        log_by_lua_block {

                        }
                }
        .
        .
        .

}

How can I handle this error to return my custom error?

0

There are 0 best solutions below