rewrite_status

The `rewrite_status` directive modifies the response status code based on specified conditions.

Syntaxrewrite_status status [if=condition];
Defaultnone
Contexthttp, server, location
Arguments1-2

Description

The rewrite_status directive allows you to rewrite the response status code returned by the server. It can take one or two arguments: a status code (like 404, 500, etc.) and an optional condition in the form of 'if=condition'. If the specified condition evaluates to true, the response's status code will be changed to the defined status code. This feature is useful for customizing responses based on specific criteria, such as checking the value of a header or a variable.

The directive operates in various contexts, including http, server, and location. When invoked, the module collects the rules specified through the rewrite_status directive and applies these rules during the request processing stage. If multiple rules are defined, they are evaluated in the order they were added, ensuring that the first matching rule will determine the final status code reported back to the client.

Despite its powerful capabilities, developers should take care in using this directive so as to avoid unintentional overrides of critical status codes, which could mislead clients or affect the caching policies of proxies or browsers. Additionally, it is worth noting that this module is currently considered experimental, which may imply a lack of comprehensive testing or features and potential instability in production environments.

Config Example

server {
    listen 127.0.0.1:8080;
    server_name localhost;

    location / {
        rewrite_status 404 if=$http_rsp_404_status;
        proxy_pass http://foo.com;
    }
}

Ensure that the condition accurately reflects the state you want to test; an incorrect condition can lead to unexpected status code outputs.

Be cautious when rewriting essential status codes like 200 or 500 as this can misinform clients and affect caching behavior.

This directive is part of an experimental module; consider the stability and support implications before using it in production environments.

← Back to all directives