Nginx系列教程:HTTP Rewrite模块

ngx_http_rewrite_module

该模块允许使用正则表达式改变URI,并且根据变量来转向以及选择配置。

如果在server级别设置该选项,那么他们将在location之前生效。如果在location还有更进一步的重写规则,location部分的规则依然会被执行。如果这个URI重写是因为location部分的规则造成的,那么location部分会再次被执行作为新的URI。

这个循环会执行10次,然后Nginx会返回一个500错误。

指令

[#break break]
[#if if]
[#return return]
[#rewrite rewrite]
[#set set]
[#uninitialized_variable_warn uninitialized_variable_warn]

break

语法: break

默认值: none

作用域: server, location, if

Completes the current set of rules. 作用是完成当前的规则列

示例:

if ($slow) {: limit_rate 10k;: break;}

if

语法: if (condition) { ... }

默认: none

作用域: server, location

Checks the truth of a condition. If the condition evaluates to true, then the code indicated in the curly braces is carried out and the request is processed in accordance with the configuration within the following block. Configuration inside directive if is inherited from the previous level.

条件语句可以使下列的几种:

一个变量的名称;如果变量的值为空字符串""或者任何以0开始的字符串,则表示条件语句的值为假 the comparison of variable with the line with using the = and != operators; pattern matching with regular expressions using the symbols ~* and ~: 正则表达式形式的模式匹配,如~*和~ ‘~’表示
大小写敏感的匹配 ‘~*’表示大小写不敏感的匹配(
例如:“firefox”字符串可以成功匹配“FireFox”)  !~和!~*代表跟后面的正则匹配规则相反的规则,表示不能匹配当前正则表达式规则的字符串执行后面的处理语句 使用-f参数以及!-f参数检测一个文件是否存在 使用-d参数以及!-d参数检测一个目录(路径)是否存在 使用-e以及!-e检测是否存在一个文件,一个目录或者一个符号链接。 使用-x以及!-x检测一个文件是否可执行

正则表达式部分可以嵌套,表达式后面的部分如果用到前面的表达式可以用 $1 到$9 变量表示。

例子如下:

if ($http_user_agent ~ MSIE) {: rewrite ^(.*)$ /msie/$1 break;}if ($http_cookie ~* "id=([^;] +)(?:;|$)" ) {: set $id $1;}if ($request_method = POST ) {: return 405;}if (!-f $request_filename) {: break;: proxy_pass http://127.0.0.1;}if ($slow) {: limit_rate 10k;}if ($invalid_referer) {: return 403;}

The value of the built-in variable $invalid_referer is given by the directive valid_referers.

时间: 2024-09-14 02:37:02

Nginx系列教程:HTTP Rewrite模块的相关文章

Nginx系列教程:nginx_module_development模块开发

好像这是唯一的一份模块开发文档.. 以下简称 guide 读代码!! nginx 程序启动 把 nginx.conf 读入内存 处理模块 ngx_http_module_t 定义的 configuration. 应该是如下的执行顺序 pre create_main_conf 执行 ngx_command_t 里面定义的函数. 可以视为 init_main_conf 的自动处理部分 init_main_conf ... postconfig listen fork 子进程(真正的服务器进程 mas

Nginx系列教程:rewrite规则使用说明

在网上看到一篇关于nginx rewrite规则的贴子,写得非常不错,里面对于Nginx nginx rewrite规则有着很详细的说明及示例,对于我这样的新手来说,这些资料真的是太有用啦!可以让你少走弯路,更容易的上手与使用nginx rewrite规则. 关于rewrite规则,最重要的还是对于正则表达式的理解,所以大家在看之前应该对正则表达式有一些基础的认识与了解,这样在理解rewrite规则的时候就更容易上手啦! 好了,下面我们来看看原文的内容. 最近在VPS上尝试配置安装一个网站,VP

Nginx系列教程:HTTP模块

控制Nginx HTTP 进程的核心属性. 指令 alias 语法: alias file-path|directory-path; 默认值: no 作用域: location 该指令设置指定location使用的路径.注意它跟 root 相似,但是不改变文件的根路径,仅仅是使用文件系统路径.比如: location&http://www.aliyun.com/zixun/aggregation/37954.html">nbsp; /i/ {  alias  /spool/w3/i

Nginx系列教程:nginx_substitutions_filter模块

nginx_substitutions_filter 请注意:此模块不是Nginx源的分布,可点击此链接找到安装说明, http://wiki.nginx.org/NginxHttpSubsModule#Installation . 概述 nginx_substitutions_filter 是一个过滤器模块,它可以在响应主体上运行正则表达式和固定字符串替换.该 模块不同于Nginx的本地替代模块.它能够扫描输出链缓冲区和匹配逐行字符串,类似于http://www.aliyun.com/zixu

Nginx系列教程:ngx_cache_purge模块

ngx_cache_purge 本模块由第三方提供,不包含在 Nginx 的源码发布版中. 概述 ngx_cache_purge是nginx模块,用于从FastCGI.proxy.SCGI 和uWSGI5603.html">缓存中增加内容清除功能. 安装 下载模块源码:ngx_cache_purge-1.2(更新记录)(SHA1: d9468cf42432e81ea3a110ec63aae2eb273f5516) 其他版本 解压,然后编译: ./configuremake &&am

Nginx系列教程:NginxHttpOwnerMatch模块(解决链接型文件跨站访问)

概述 该模块提供了一个简单文件基于所有者的http://www.aliyun.com/zixun/aggregation/38609.html">访问控制. nginx_http_owner_match_module模块使得它可以控制具体文件的所有者和组织的访问. 访问规则检查是根据其声明的顺序. 配置范例: location / {  omallow heiher;  # allow access files of heiher  omallow jack sftp; # allow a

Nginx系列教程:事件模块

设置Nginx处理连接请求 指令 accept_mutex Syntax: accept_mutex [ on | off ] Default: on nginx 使用连接互斥锁进行顺序的accept()系统调用. accept_mutex_delay Syntax: accept_mutex_delay Nms; Default: 500ms 如果一个进程没有互斥锁,它将延迟至少多长时间.默认情况下,延迟是500ms . debug_connection Syntax: debug_conne

Nginx系列教程:常用PHP程序Rewrite(伪静态规则)

Rewrite主要的功能就是实现URL的重写,它的正则表达式是基于Perl语言.可基于服务器级的(httpd.conf)和目录级的(.htaccess)两种方式.如果要想用到rewrite模块,必须先安装或加载rewrite模块. Nginx Rewrite规则相关指令 Nginx Rewrite规则相关指令有if.rewrite.set.return.break等,其中rewrite是最关键的指令.一个简单的Nginx Rewrite规则语法如下: rewrite ^/b/(.*)\.html

Nginx系列教程:HTTP Secure Link模块

此模块计算和检查要求所需的安全链接网址.此模块默认情况下不会被编译启用,要启用它请使用指令 --with-http_secure_link_module 参与编译安装 nginx .注意:此模块仅在 nginx 版本为0.7.18或更高时才被支持. 配置实例: location /p/ { location ~ ^/p/(?<secure>[\w\-=]+,\d+)(?<file>/.+)$ {  secure_link $secure; secure_link_md5 $secu

Nginx系列教程:LNMP多用户动态进程管理虚拟主机方案

A. 特点1. 高效.内存使用少.2. 权限分离,用户间互不干扰.3. 动态进程管理,资源分配均衡. B. 应用程序说明Nginx:事件驱动的 Web 服务器,采用模块化设计,小巧.高效.PHP-FPM:支持快速进程管理的 PHP FastCGI 接口版本,用它实现动态进程管理,提高资源使用效率. C. 整个架构的简单说明Nginx 处理所有的 Web 请求,它将 PHP 的请求 Match 出,发送给上游服务器处理,这里的上游服务器就是 PHP-CGI.PHP-CGI 工作在 FastCGI