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. 字体Unicode编码

    客专家福利     有奖试读&征文——我们在互联网上奋斗的故事      10月推荐文章汇总      加入“技术热心人”,赢丰厚奖品 html-中文字体在CSS中的显示(Unicode编码) ...

  2. Thrift 个人实战--Thrift 的序列化机制

    前言: Thrift作为Facebook开源的RPC框架, 通过IDL中间语言, 并借助代码生成引擎生成各种主流语言的rpc框架服务端/客户端代码. 不过Thrift的实现, 简单使用离实际生产环境还 ...

  3. Windows 下的 Sublime Text 2 配置汇总, Sublime Text 3 亦可借鉴

    1)软件下载地址:http://www.sublimetext.com/2 2)安装 Package Control ,方便安装和管理插件,网络资源很多,这里附上一篇:http://www.imjef ...

  4. C++ Primer : 第十三章 : 拷贝控制示例

    /* Message.h */ #ifndef _MESSAGE_H_ #define _MESSAGE_H_ #include <iostream> #include <strin ...

  5. Objective-c——UI基础开发第十二天(相册展示)

    一.知识点 模仿新特性 UICollectionViewFlowLayout自定义布局 相册 瀑布流(淘宝购物之类的 二.复习 a.UICollectionView 和 tableview共享一套AP ...

  6. LEETCODE —— Single Number

    Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...

  7. docker swarm compose

    swarm docker run swarm --help compose curl -L https://github.com/docker/compose/releases/download/1. ...

  8. Java入门教程总目录

    Java入门教程总目录 持续更新中... 1.Java常识汇总 2.Java框架对比 3.Java技术路线 4.Java编码规范 5.Java环境变量配置 6.枚举 7.操作符 12.定时任务

  9. 服务发现之 Etcd VS Consul

    抄自这里 *********************************************************************************************** ...

  10. ASP.net 关于TextBox的TextMode=“PassWord”时的动态赋值

    1.在界面创建一个新的TBPwdTemp并设置TextMode=“SingleLine”,visible=“false”,将数据库密码值传给该TextBox: 2.然后分别用 this.TBPwd.A ...