loop_detect

The `loop_detect` directive enables loop detection for requests passing through NGINX, utilizing the CDN-Loop header to prevent request cycles.

Syntaxloop_detect on | off;
Defaultoff
Contexthttp, server, location
Argumentsflag

Description

The loop_detect directive is part of the ngx_http_loop_detect_module designed to prevent request loops commonly associated with CDN configurations. When enabled, it activates mechanisms within NGINX to inspect the CDN-Loop header of incoming requests, allowing the server to track how many times a request has traversed different CDN nodes. This tracking is essential for identifying and preventing infinite loops that can occur during request processing across multiple CDN nodes.

The directive takes a simple on/off flag as its parameter. When set to 'on', NGINX will begin counting the hops recorded in the CDN-Loop header. If the number of hops exceeds a configured limit (set by loop_detect_max_allow_loops), the request will be blocked, and a specific HTTP status code (defined by loop_detect_status) will be returned to the client. This mechanism is crucial for maintaining optimal server performance and ensuring that erroneous requests do not overload the server or allow for unintended looping behavior in the network.

The directive can be defined within various contexts, namely http, server, or location blocks, ensuring flexibility based on the specific needs of your application. It's essential to properly configure this directive along with associated directives such as loop_detect_cdn_id and loop_detect_max_allow_loops to tailor the loop detection functionality to your infrastructure requirements.

Config Example

http {
    loop_detect on;
    loop_detect_cdn_id my_cdn_id;
    loop_detect_status 508;
    loop_detect_max_allow_loops 10;

    server {
        listen 80;
        server_name example.com;
        location / {
            proxy_set_header CDN-Loop $loop_detect_proxy_add_cdn_loop;
            proxy_pass http://example.upstream.com;
        }
    }
}

Ensure that the CDN-Loop header is correctly formatted and contains valid information to avoid unintentional request blocks.

Setting the loop_detect_max_allow_loops too low may lead to valid requests being blocked erroneously, especially in complex CDN configurations.

Improper configuration of associated directives may result in ineffective loop detection. Ensure loop_detect_cdn_id and loop_detect_status are set appropriately.

← Back to all directives