资讯专栏INFORMATION COLUMN

nginx rewrite配置解读

IamDLY / 1261人阅读

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

directives
名称 默认配置 作用域 官方说明 中文解读 模块
break server, location, if Stops processing the current set of ngx_http_rewrite_module directives. 中断当前的重写 ngx_http_rewrite_module
if 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表达式成立则执行,if代码块里头的配置继承外部的配置; false和0都表示不成立; string比较,相等用=,不相等用!= ; 文件存在用-f,不存在用! -f ; 目录存在用-d,不存在用 ! -d ; 链接存在用-e,不存在用! -e ; 文件可执行用-x,不可执行用! -x ngx_http_rewrite_module
return 无,语法 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. 停止处理并返回,返回444的话则会关闭连接,连header都不会发送 ngx_http_rewrite_module
rewrite 无,语法 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 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_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. 设置是否在error_log以notice级别开启rewrite的log ngx_http_rewrite_module
set 无,语法 set $variable value; server, location, if Sets a value for the specified variable. The value can contain text, variables, and their combination. 设置变量 ngx_http_rewrite_module
uninitialized_variable_warn uninitialized_variable_warn on; http, server, location, if Controls whether warnings about uninitialized variables are logged. 设置是否打印使用了未初始化变量的log 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;
            }
    }
}
doc

ngx_http_rewrite_module

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/39748.html

相关文章

  • nginx lua api解读

    摘要:对于需要进一步注意的是参数的使用,可以传入所定义的所有的状态码常量如等和两个模块内核常量只支持和这两个,如果传入其他的如等则进程住。 序 本文主要解读下nginx lua module的主要方法和api。 ngx_lua运行阶段 showImg(https://segmentfault.com/img/bVHFqI?w=1005&h=910); initialization phase...

    shery 评论0 收藏0
  • Nginx 安装与配置规则入门

    摘要:即别名,与的匹配规则稍有不同。与的区别只能作用在中,而可以存在和中。后面必须要用结束,否则会找不到文件,而则对可有可无。 Nginx 安装与配置规则入门 nginx 安装与运行 (Mac OS环境) nginx 规则配置入门 一些命令行的配置 一、nginx 安装与运行 (Mac OS环境) 1. 安装 nginx 可通过 Homebrew 可直接安装: $brew install...

    senntyou 评论0 收藏0
  • nginx 常用配置记录

    摘要:如果状态码附带文字段落,该文本将被放置在响应主体。相反,如果状态码后面是一个,该将成为头部值。没有状态码的将被视为一个状态码,这种情况下需要以或者开头。因为和不能简单的只返回状态码,还必须有重定向的,这就是指令无法返回的原因了。 HTTP模块(核心模块,也是主要用到的模块) server模块 server模块是http的子模块,它用来定义一个虚拟主机 例子: server { ...

    Youngs 评论0 收藏0
  • 在开发环境下使用nginx重写uri及代理功能

    摘要:本文同步在个人博客上,欢迎关注这篇文章整理了在前端开发中,在开发环境下使用重写及代理功能的方法。表示该规则是使用正则定义的,区分大小写。因此牢记在上下文中使用,而在上下文中使用。 本文同步在个人博客shymean.com上,欢迎关注 这篇文章整理了在前端开发中,在开发环境下使用nginx重写uri及代理功能的方法。 参考 nginx中文文档 前端开发者必备的 Nginx 知识 Ngin...

    LMou 评论0 收藏0
  • Nginx重定向Rewrite分析

    摘要:之前写过重定向了,,但当时比较模糊。这里主要说两种常用的重定向,都是中的重定向。我测了下,这样其实就搞定了,重定向就好了,不要向网上说的个语句。这里重定向到后,解析的的规则用的又是上面用到的,原理也一样,把分割,然后匹配。 http://homeway.me showImg(http://xiaocao.u.qiniudn.com/blog/logo-nginx.png); ...

    wums 评论0 收藏0

发表评论

0条评论

IamDLY

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<