nginx rewrite配置解读

本文详细解析了Nginx中ngx_http_rewrite_module的相关配置指令,包括break、if、return、rewrite、rewrite_log、set及uninitialized_variable_warn等,并通过一个具体的实例展示了这些指令的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文主要解析一下ngx_http_rewrite_module中的rewrite相关配置。

directives

名称默认配置作用域官方说明中文解读模块
breakserver, location, ifStops processing the current set of ngx_http_rewrite_module directives.中断当前的重写ngx_http_rewrite_module
ifserver, locationThe 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表达式成立则执行,if代码块里头的配置继承外部的配置; false和0都表示不成立; string比较,相等用=,不相等用!= ; 文件存在用-f,不存在用! -f ; 目录存在用-d,不存在用 ! -d ; 链接存在用-e,不存在用! -e ; 文件可执行用-x,不可执行用! -xngx_http_rewrite_module
return无,语法 return code [text]; return code URL; return URL;server, location, ifStops processing and returns the specified code to a client. The non-standard code 444 closes a connection without sending a response header.停止处理并返回,返回444的话则会关闭连接,连header都不会发送ngx_http_rewrite_module
rewrite无,语法 rewrite regex replacement [flag];server, location, ifIf 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 configuration 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停止匹配,使用重写后的url进行新的匹配; break停止当前的url重写; redirect进行302跳转; permanent进行301跳转ngx_http_rewrite_module
rewrite_logrewrite_log off;http, server, location, ifEnables or disables logging of ngx_http_rewrite_module module directives processing results into the error_log at the notice level.设置是否在error_log以notice级别开启rewrite的logngx_http_rewrite_module
set无,语法 set $variable value;server, location, ifSets a value for the specified variable. The value can contain text, variables, and their combination.设置变量ngx_http_rewrite_module
uninitialized_variable_warnuninitialized_variable_warn on;http, server, location, ifControls whether warnings about uninitialized variables are logged.设置是否打印使用了未初始化变量的logngx_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;
            }
    }
}

doc

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值