Interpretation of nginx rewrite configuration



This article mainly analyzes rewrite-related configurations in ngx_http_rewrite_module.


Name Default configuration action scope Official statement Interpretation in Chinese module
break No server, location, if Stops processing the current set of ngx_http_rewrite_module directives. Interrupt the current rewrite ngx_http_rewrite_module
if No server, location The specified condition is evaluated. If true, this module directives specified inside the braces are executed, and the request is assigned the configuration inside the if directive. Configurations inside the if directives are inherited from the previous configuration level. If the expression holds, it is executed, and the configuration in the if code block inherits the external configuration. False and 0 are both false; String comparison, equal =, not equal! = ; File exists -f, does not exist! -f ; Directory exists -d, does not exist! -d ; Links are useful -e, not useful! -e ; File executable -x, not executable! -x ngx_http_rewrite_module
return None, syntax return code [text]; return code URL; return URL; server, location, if Stops processing and returns the specified code to a client. The non-standard code 444 closes a connection without sending a response header. Stop processing and return, if return 444, the connection will be closed and even header will not be sent. ngx_http_rewrite_module
rewrite None, syntax rewrite regex replacement [flag]; server, location, if If the specified regular expression matches a request URI, URI is changed as specified in the replacement string. The rewrite directives are executed sequentially in order of their appearance in the configurat ion file. It is possible to terminate further processing of the directives using flags. If a replacement string starts with “http://”, “https://”, or “$scheme”, the processing stops and the redirect is returned to a client. Last stops matching and uses the rewritten url for new matching. Break stops the current url rewrite; Redirect performs 302 jumps; Permanent makes 301 jumps ngx_http_rewrite_module
rewrite_log rewrite_log off; http, server, location, if Enables or disables logging of ngx_http_rewrite_module module directives processing results into the error_log at the notice level. Sets whether to open rewrite’s log at the notice level at error_log ngx_http_rewrite_module
set No, grammar set $ variable value; server, location, if Sets a value for the specified variable. The value can contain text, variables, and their combination. Set variables ngx_http_rewrite_module
uninitialized_variable_warn uninitialized_variable_warn on; http, server, location, if Controls whether warnings about uninitialized variables are logged. Sets whether log with uninitialized variables is printed ngx_http_rewrite_module


http {
        # a special log format referencing variables we'll define later
        log_format imagelog '[$time_local] ' $image_file ' 
                            '$image_type ' 
                            ' $body_bytes_sent ' 
                            ' $status;
        # we want to enable rewrite-rule debugging to see if our rule does what we intend
        rewrite_log on;
        server {
            root /home/www;
            location / {
                # we specify which logfile should receive the rewriteruledebug messages
                error_log logs/rewrite.log notice;

                # our rewrite rule, utilizing captures and positional variables
                # note the quotes around the regular expression theseare required because we used {} within the expression itself
                rewrite '^/images/([a-z]{2})/([a-z0-9]{5})/(.*)\.(png|jpg|gif)$' /data?file=$3.$4;
                # note that we didn't use the 'last' parameter above; if we had, the variables below would not be set because NGINX would have ended rewrite module processing here we set the variables that are used in the custom log format 'imagelog'
                set $image_file $3;
                set $image_type $4;