码迷,mamicode.com
首页 > 其他好文 > 详细

Nginx Rewrite语法详解

时间:2017-10-02 15:48:48      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:https   str   大小   set   htm   sch   rate   系统调用   length   

重写中用到的指令

if  (条件) {}  设定条件,再进行重写

set #设置变量

return #返回状态码

break #跳出rewrite

rewrite #重写

If  语法格式

If 空格 (条件) {

    重写模式

}

条件又怎么写?

答:3种写法

1: “=”来判断相等, 用于字符串比较

2: “~” 用正则来匹配(此处的正则区分大小写)

   ~* 不区分大小写的正则

3: -f -d -e来判断是否为文件,为目录,是否存在.

 

例子:

if  ($remote_addr = 192.168.1.100) {

                return 403;

 }

 if ($http_user_agent ~ MSIE) {

                rewrite ^.*$ /ie.htm;

                break; #(不break会循环重定向)

 }

 

if (!-e $document_root$fastcgi_script_name) {

                rewrite ^.*$ /404.html break;

 }

 注, 此处还要加break,

 

nginx有以下内置变量

$args, 请求中的参数;

$content_length, HTTP请求信息里的"Content-Length";

$content_type, 请求信息里的"Content-Type";

$document_root, 针对当前请求的根路径设置值;

$document_uri, 与$uri相同;

$host, 请求信息中的"Host",如果请求中没有Host行,则等于设置的服务器名;

$limit_rate, 对连接速率的限制;

$request_method, 请求的方法,比如"GET"、"POST"等;

$remote_addr, 客户端地址;

$remote_port, 客户端端口号;

$remote_user, 客户端用户名,认证用;

$request_filename, 当前请求的文件路径名

$request_body_file

$request_uri, 请求的URI,带查询字符串;

$query_string, 与$args相同;

$scheme, 所用的协议,比如http或者是https,比如rewrite  ^(.+)$  $scheme://example.com$1  redirect;

$server_protocol, 请求的协议版本,"HTTP/1.0"或"HTTP/1.1";

$server_addr, 服务器地址,如果没有用listen指明服务器地址,使用这个变量将发起一次系统调用以取得地址(造成资源浪费);

$server_name, 请求到达的服务器名;

$server_port, 请求到达的服务器端口号;

$uri, 请求的URI,可能和最初的值有不同,比如经过重定向之类的。

这些变量可以用在rewrite规则里,也可以打印日志的时候用

Nginx Rewrite语法详解

标签:https   str   大小   set   htm   sch   rate   系统调用   length   

原文地址:http://www.cnblogs.com/microtiger/p/7620174.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!