break
The 'break' directive stops the processing of the current location and prevents further processing of rewrite or location blocks.
Description
The 'break' directive is used within location or if blocks in NGINX configuration to halt the evaluation of all further rewrite rules or nested location blocks. When triggered, it effectively changes the flow of request processing, enabling administrators to control whether a request should continue down the path of additional rules or handlers. This directive is useful for implementing conditional logic based on certain criteria, such as the state of URI or request parameters.
When the 'break' directive is encountered during the processing of a request, the NGINX server will stop evaluating any subsequent rewrite directives that follow it in the same context. After invoking 'break', NGINX continues processing the current configuration block, potentially skipping other defined behaviors or handlers that may affect the current request. It acts as an abrupt termination for processing in a controlled way, allowing for simplification and clarity in complex NGINX setups.
It is important to note that the 'break' directive does not return any value nor does it take any parameters. It is strictly a command that modifies control flow. There are no direct arguments or expressions needed for its correct use within the configuration file, making its implementation straightforward. However, it is mostly applicable where multiple nested directives may exist, such as in intricate location matching scenarios.
Config Example
location /example {
if ($arg_param = 'stop') {
break;
}
# Further processing can go here
}Using 'break' inside nested if blocks can lead to complex control flow that may be hard to debug.
The 'break' directive does not stop the entire server configuration processing, only the current request cycle.
Misplacing 'break' in an unexpected location context can lead to unintended behavior in request handling.