Location的语法:

location 有”定位”的意思, 根据Uri来进行不同的定位.

在虚拟主机的配置中,是必不可少的,location可以把网站的不同部分,定位到不同的处理方式上.

比如, 碰到.php, 如何调用PHP解释器?  --这时就需要location

location 的语法:

location [=|~|~*|^~] patt {

}

中括号可以不写任何参数,此时称为一般匹配

也可以写参数,因此,大类型可以分为3种

 location = patt {} [精准匹配]
location patt{} [一般匹配]
location ~ patt{} [正则匹配]

如何发挥作用?

 首先看有没有精准匹配,如果有,则停止匹配过程.
location = patt {
config A
}
如果 $uri == patt,匹配成功,使用configA
location = / {
root /var/www/html/;
index index.htm index.html;
} location / {
root /usr/local/nginx/html;
index index.html index.htm;
} 如果访问  http://xxx.com/
定位流程是 
: 精准匹配中 ”/” ,得到index页为  index.htm
: 再次访问 /index.htm , 此次内部转跳uri已经是”/index.htm” ,
根目录为/usr/local/nginx/html
: 最终结果,访问了 /usr/local/nginx/html/index.htm

再来看,正则也来参与.

 location / {
root /usr/local/nginx/html;
index index.html index.htm;
} location ~ image {
root /var/www/image;
index index.html;
} 如果我们访问 http://xx.com/image/logo.png
此时, “/” 与”/image/logo.png” 匹配
同时,”image”正则 与”image/logo.png”也能匹配,谁发挥作用?
正则表达式的成果将会使用. 图片真正会访问 /var/www/image/logo.png

我们接下来再看一下下面的这个例子:

 location / {
root /usr/local/nginx/html;
index index.html index.htm;
} location /foo {
root /var/www/html;
index index.html;
}
我们访问 http://xxx.com/foo
对于uri “/foo”, 两个location的patt,都能匹配他们
即 ‘/’能从左前缀匹配 ‘/foo’, ‘/foo’也能左前缀匹配’/foo’,
此时, 真正访问 /var/www/html/index.html
原因:’/foo’匹配的更长,因此使用之.;

rewrite 重写

下面看一下具体的语法:

重写中用到的指令
if (条件) {} 设定条件,再进行重写
set #设置变量
return #返回状态码
break #跳出rewrite
rewrite #重写 If 语法格式
If 空格 (条件) {
重写模式
} 条件又怎么写?
答:3种写法
: “=”来判断相等, 用于字符串比较
: “~” 用正则来匹配(此处的正则区分大小写)
~* 不区分大小写的正则
: -f -d -e来判断是否为文件,为目录,是否存在.

我们看一下下面的具体的例子:

  if  ($remote_addr = 192.168.1.100) {
return ;
} //判断是不是IE的用户
If ($http_user_agent ~ MSIE) {
rewrite ^.*$ /ie.htm;
break; #(不break会循环重定向)
} if (!-e $document_root$fastcgi_script_name) {
rewrite ^.*$ /.html
break;
}
注, 此处还要加break,

以 xx.com/dsafsd.html这个不存在页面为例,

我们观察访问日志, 日志中显示的访问路径,依然是GET /dsafsd.html HTTP/1.1

提示: 服务器内部的rewrite和302跳转不一样.

跳转的话URL都变了,变成重新http请求404.html, 而内部rewrite, 上下文没变,

就是说 fastcgi_script_name 仍然是 dsafsd.html,因此 会循环重定向.

set 是设置变量用的, 可以用来达到多条件判断时作标志用.

达到apache下的 rewrite_condition的效果

我们来看一下set变量的一些用法:

 if ($http_user_agent ~* msie) {
set $isie ;
} if ($fastcgi_script_name = ie.html) {
set $isie ;
} if ($isie ) {
rewrite ^.*$ ie.html;
}

此时我们不需要加break进行

nginx的Location的总结以及rewrite规则的总结的更多相关文章

  1. nginx+tomcat集群配置(4)--rewrite规则和多应用根目录设定思路

    前言: nginx中有一块很重要的概念, 就是rewrite规则. 它会对URL进行修改, 然后进行内部的重定向. rewrite授予了nginx更多的自由, 使得后级服务的接入更加地方便. 本文将简 ...

  2. Nginx 配置 location 以及 return、rewrite 和 try_files 指令

    正则表达式 Nginx 内置的全局变量 location 前缀字符串及优先级 示例 location 匹配原则 if 和 break 指令 if break return.rewrite 和 try_ ...

  3. nginx配置location项的URL匹配规则

    Localtion URL的正则匹配规则 示例 location / { try_files $uri @apache; } #所有的路径都是/开头,表示匹配所有 location @apache { ...

  4. Nginx配置location总结及rewrite规则写法

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 32.0px "Helvetica Neue"; color: #323333 } p. ...

  5. rewrite规则写法及nginx配置location总结

    rewrite只能放在server{},location{},if{}中,并且只能对域名后边的除去传递的参数外的字符串起作用. 例如http://seanlook.com/a/we/index.php ...

  6. 转:Nginx 配置 location 总结及 rewrite 规则写法

    转: http://www.linuxidc.com/Linux/2015-06/119398.htm 1. location正则写法 一个示例: location =/{ # 精确匹配 / ,主机名 ...

  7. 【转】nginx配置location总结及rewrite规则写法

    原文:http://seanlook.com/2015/05/17/nginx-location-rewrite/ 1. location正则写法 location = / { # 精确匹配 / ,主 ...

  8. nginx配置 location及rewrite规则详解

    1. location正则写法 语法规则: location [=|~|~*|^~] /uri/ { … } =    开头表示精确匹配 ^~  开头表示uri以某个常规字符串开头,理解为匹配 url ...

  9. nginx配置location总结及rewrite规则写法(转)

    一个示例: location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # 因为所有的地址都以 / 开头,所以这 ...

随机推荐

  1. WPF的Binding学习笔记(一)

    原文: http://www.cnblogs.com/pasoraku/archive/2012/10/20/2732427.html 一.binding的一般步骤 1,准备数据源     数据源需要 ...

  2. Python::OS 模块 -- 文件和目录操作

    os模块的简介参看 Python::OS 模块 -- 简介 os模块的进程管理 Python::OS 模块 -- 进程管理 os模块的进程参数 Python::OS 模块 -- 进程参数 os模块中包 ...

  3. 通过printf设置Linux终端输出的颜色和显示方式

    转载自:http://www.cnblogs.com/clover-toeic/p/4031618.html 在Linux终端下调试程序时,有时需要输出大量信息.若能控制字体的颜色和显示方式,可使输出 ...

  4. JSONObject转换JSON--将Date转换为指定格式

    项目中,经常会用JSONObject插件将JavaBean或List<JavaBean>转换为JSON格式的字符串,而JavaBean的属性有时候会有java.util.Date这个类型的 ...

  5. segmentation fault

    今天在hanoi问题上出现了segmentation fault 在gcc编译的过程中 没出现error,然而程序运行到一半就出现segmentation fault: 上网发现 这条语句是非法的内存 ...

  6. HDU 4471 矩阵快速幂 Homework

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4471 解题思路,矩阵快速幂····特殊点特殊处理····· 令h为计算某个数最多须知前h个数,于是写 ...

  7. iOS的URL处理

    前两天处理iOSapp过程中(我是用swift语言写的,资料较少),被一个“字符串”搞了一晚上的时间到第二天才处理好,在此记下,望见过此文的学生有一天遇到该情况能三分钟搞定不浪费时间: 先看如下代码 ...

  8. android之OptionMenu

    一.现在我给大家介绍两个不同版本的模拟器(2.3.3和4.0.3) 1.布局文件 (1)打开“res/layout/activity_main.xml”文件. 先看mian布局文件 <Relat ...

  9. Apache配置代理服务器的方法(1)

    众所周知Apache是目前最优秀的HTTP服务器.实际上它不仅能当作服务器使用,也能够被用来架设代理服务器.本文就如何使用Apache架设HTTP代理服务器进行说明. 本文将基于Win32版的Apac ...

  10. phpunit4.1的干净测试

    一般而言,写测试时需要加载一些文件来进行自动加载 但在phpunit4.1中只要其中一个测试文件加载了,其他测试文件就不需要再加载