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/

定位流程是 

1: 精准匹配中 ”/”   ,得到index页为  index.htm

2: 再次访问 /index.htm , 此次内部转跳uri已经是”/index.htm” ,

根目录为/usr/local/nginx/html(由于内部跳转uri为/index.htm,所以精准匹配不成功了,普通匹配,成功,虽然只匹配上一个/,但是普通匹配是按匹配长度来算的,所以也算匹配上了,所以根目录,自然就变成了,普通匹配中的/usr/local/nginx/html了)疑问:既然精准匹配已经成功了,为什么还要再匹配一次呢?是因为/var/www/html中没有index.htm 这个文件吗?

3: 最终结果,访问了 /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 重写

nginx官方文档:

官方文档中写到,rewrite的context如下图所示,意思是指rewrite的上下文是server,location,if,更明确的就是说rewrite可以写在server段中,也可以写到location段中,或者if中。

重写中用到的指令

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

set #设置变量

return #返回状态码

break #跳出rewrite

rewrite #重写

If  语法格式

If 空格 (条件) {

重写模式

}

条件又怎么写?

答:3种写法

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

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

~* 不区分大小写的正则

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

例子:

if  ($remote_addr = 192.168.1.100) {

return 403;//forbidden

}

if ($http_user_agent ~ MSIE) {

rewrite ^.*$ /ie.htm;

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

}

if (!-e $document_root$fastcgi_script_name) {

rewrite ^.*$ /404.html break;

}

注, 此处还要加break,

ls conf/  fastcgi.conf,nginx有权引用的变量都在这里记录着呢

以 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的效果

如下: 判断IE并重写,且不用break; 我们用set变量来达到目的

if ($http_user_agent ~* msie) {

set $isie 1;

}

if ($fastcgi_script_name = ie.html) {

set $isie 0;

}

if ($isie 1) {

rewrite ^.*$ ie.html;

}

Rewrite语法

Rewrite 正则表达式  定向后的位置 模式

Goods-3.html ---->Goods.php?goods_id=3

goods-([\d]+)\.html ---> goods.php?goods_id =$1

location /ecshop {

index index.php;

rewrite goods-([\d]+)\.html$ /ecshop/goods.php?id=$1;

rewrite article-([\d]+)\.html$ /ecshop/article.php?id=$1;

rewrite category-(\d+)-b(\d+)\.html /ecshop/category.php?id=$1&brand=$2;

rewrite category-(\d+)-b(\d+)-min(\d+)-max(\d+)-attr([\d\.]+)\.html /ecshop/category.php?id=$1&brand=$2&price_min=$3&price_max=$4&filter_attr=$5;

rewrite category-(\d+)-b(\d+)-min(\d+)-max(\d+)-attr([\d+\.])-(\d+)-([^-]+)-([^-]+)\.html /ecshop/category.php?id=$1&brand=$2&price_min=$3&price_max=$4&filter_attr=$5&page=$6&sort=$7&order=$8;

}

注意:用url重写时, 正则里如果有”{}”,正则要用双引号包起来

ecshop版本过低,运行的页面有很多报错信息,需要修改如下3个目录中的文件。

admin、根目录、instore 这3个目录里面的/include/init.php都改成 error_report(0)。记住要先修改ecshop的配置文件,然后再安装,否则可能不起作用。因为安装之后会有很多的缓存。

nginx-location rewrite的更多相关文章

  1. nginx location rewrite 禁止访问某个目录

    Location 指令,是用来为匹配的 URI 进行配置 http://www.baidu.com/test/index.php?a=1&b=ture   这里面/test/index.php ...

  2. nginx 重定向 rewrite 规则

    1.rewrite配置 server { listen 80 default_server; server_name jeson.t.imooc.io; access_log /var/log/ngi ...

  3. nginx location匹配及rewrite规则

    location匹配规则 1. 实例 server{ location = \ { [配置A] } location / { [配置B] } location = /images/ { [配置C] } ...

  4. 17.Nginx 重写(location rewrite)

    Nginx 重写(location / rewrite) 目录 Nginx 重写(location / rewrite) 常见的nginx正则表达式 location lication的分类 loca ...

  5. Nginx Location配置总结

    Nginx Location配置总结 语法规则: location [=|~|~*|^~] /uri/ { - }= 开头表示精确匹配^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即 ...

  6. nginx 配置rewrite 笔记

    nginx 配置rewrite笔记: 通过下面的示例来说明一下,1. 先说说location : location 表示匹配传入的url地址,其中配置符有多种,各种情况的意义不一样: location ...

  7. nginx配置rewrite

    1. uri  和 url读取区别 区别就是URI定义资源,而URL不单定义这个资源,还定义了如何找到这个资源. 比如说,一个服务器上,到一个文件夹/网页的绝对地址(absolute path)就是U ...

  8. 使用nginx的rewrite实现代理指定文件夹命令方法

    使用nginx的rewrite实现代理指定文件夹命令方法 使用nginx代理Tomcat,Tomcat公布web的时候通常都是带着项目名称的. 比方项目名称为"aven".那么公布 ...

  9. Nginx的Rewrite规则与实例

    通过Rewrite规则可以实现规范的URL.根据变量来做URL转向及选择配置,用好Rewrite有时起到事半功倍的效果. 语法 Nginx的Rewrite相比Apache的要好理解很多,主要使用指令有 ...

  10. Nginx location配置详细解释

    nginx location配置详细解释 语法规则: location [=|~|~*|^~] /uri/ { - } = 开头表示精确匹配 ^~ 开头表示uri以某个常规字符串开头,理解为匹配 ur ...

随机推荐

  1. 常用类--Date日期类,SimpleDateFormat日期格式类,Calendar日历类,Math数学工具类,Random随机数类

    Date日期类 Date表示特定的时间,精确到毫秒; 构造方法: public Data() public Date(long date) 常用方法: public long getTime() pu ...

  2. 使用Gson解析JSON数据

    本文使用gson对json进行解析处理 首先,下载gson包 ,本文使用(gson-1.6.jar) package com.whroid.java.json; import com.google.g ...

  3. 【bzoj2430】[Poi2003]Chocolate 贪心

    题目描述 有一块n*m的矩形巧克力,准备将它切成n*m块.巧克力上共有n-1条横线和m-1条竖线,你每次可以沿着其中的一条横线或竖线将巧克力切开,无论切割的长短,沿着每条横线切一次的代价依次为y1,y ...

  4. 【bzoj3251】树上三角形 朴素LCA+暴力

    题目描述 给定一大小为n的有点权树,每次询问一对点(u,v),问是否能在u到v的简单路径上取三个点权,以这三个权值为边长构成一个三角形.同时还支持单点修改. 输入 第一行两个整数n.q表示树的点数和操 ...

  5. HUST——1103Party(拓扑排序+个人见解)

    1103: Party Time Limit: 2 Sec  Memory Limit: 64 MB Submit: 11  Solved: 7 Description N students were ...

  6. [POJ3728]The merchant(tanrjan_lca + DP)

    传送门 比着题解写还错... 查了两个小时没查出来,心态爆炸啊 以后再查 ——代码(WA) #include <cstdio> #include <cstring> #incl ...

  7. linux maven安装(三)

    1.下载maven http://maven.apache.org/download.cgi 我下载的是:apache-maven-3.3.9-bin.tar.gz 解压:tar -zxvf apac ...

  8. eval()函数的巧用

    eval的功能 将字符串str当成有效的表达式来执行.. 写函数,专门计算图形的面积 其中嵌套函数,计算圆的面积,正方形的面积和长方形的面积 调用函数area(‘圆形’,圆半径)  返回圆的面积 调用 ...

  9. eclipse中maven项目交付svn忽略配置文件

    eclipse与maven插件的结合为我们快速搭建开发环境提供了便捷条件,然而maven编译出来的class文件.配置文件和打包文件实际上都不需要进行版本控制,团队中每个人的开发环境可能不太一样,将. ...

  10. 三读bootmem【转】

    转自:http://blog.csdn.net/lights_joy/article/details/2704788 版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[-] 11  ...