在 /usr/local/nginx/conf/nginx.conf 的默认 server 段中,保留默认的 location 信息(之前测试的 location 配置删除):

        location / {
root html;
index index.html index.htm;
}

 

在 /var/www 下创建 image 目录:

[root@localhost ~]# cd /var/www
[root@localhost www]# mkdir image

使用 wget 或者 ftp 在该目录下下载或者传输一张图片:test.jpg

修改 /usr/local/nginx/html/index.html,加上 <img src="./image/test.jpg">:

[root@localhost nginx]# vim html/index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<img src="./image/test.jpg">
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p> <p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p>
</body>
</html>

平滑重启 nginx。

由于 /usr/local/nginx/html/ 目录下并没有 image 目录,因此访问 192.168.254.100 时,页面上的图片是无法显示的:

  

直接访问图片地址:

此时在 location 配置下添加正则匹配的 location 配置(第 2 段),此时的 location 配置为:

        location / {
root html;
index index.html index.htm;
} location ~ image {
root /var/www/;
index index.html;
}

平滑重启 nginx。

访问 http://192.168.254.100/image/test.jpg,正则匹配和一般匹配都能匹配上该 uri 的时候,优先正则匹配:

/var/www/image 目录下的图片显示了。

直接访问 http://192.168.254.100/image/test.jpg:

匹配过程是:正则匹配中的 image 能够匹配到 uri 中的 /image/test.jpg,则正则匹配发挥作用,真正访问的是 /var/www/images/test.jpg

再测试,当多个普通匹配都能匹配到 uri ,哪个匹配能真正发挥作用(比较第 1 段和第 2 段)?

/usr/local/nginx/conf/nginx.conf:

        location / {
root html;
index index.html index.htm;
} location /foo {
root /var/www;
index index.htm index.html;
} location ~ image {
root /var/www/;
index index.html;
}

此时 /var/www 目录下有 foo 目录,下面有文件 index.htm : i'm /var/www/index.htm

对于 uri "/foo",两个 location 的 patt 都能匹配,即 "/" 和 "/foo" 都能左前缀匹配 "/foo",

访问 http://192.168.254.100/foo:

  

        location /foo {
root /var/www;
index index.htm index.html;
}

发挥了作用,原因是 "/foo" 更长,因此使用 "/foo" 的 location 进行匹配。

    

Nginx 笔记与总结(7)Location:正则匹配的更多相关文章

  1. nginx location 正则匹配

    nginx 统计语句1.根据访问IP统计UV awk '{print $1}' access.log|sort | uniq -c |wc -l2.统计访问URL统计PV awk '{print $7 ...

  2. nignx的location正则匹配

    原文链接:http://nginx.org/en/docs/http/ngx_http_core_module.html Syntax: location [ = | ~ | ~* | ^~ ] ur ...

  3. nginx获取经过层层代理后的客户端真实IP(使用正则匹配)

    今天帮兄弟项目搞了一个获取客户端真实IP的问题,网上这种问题很多,但是对于我们的场景都不太合用,现把我的解决方案share给大家,如有问题,请及时指出. 场景: 在请求到达后端服务之前,会经过层层代理 ...

  4. Nginx 笔记与总结(6)Location:精准匹配

    在 /usr/local/nginx/conf/nginx.conf 的 server 段中,location 表示根据 URI 来进行不同的定位:把网站的不同部分定位到不同的处理方式上,例如遇到 . ...

  5. 07 nginx Location之正则匹配

    一:Location之正则匹配 再来看,正则也来参与. location / { root   /usr/local/nginx/html; index  index.html index.htm; ...

  6. nginx location模块--匹配规则

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

  7. nginx之location的匹配规则

    nginx之location的匹配规则 一.语法规则 location [=|~|~*|^~] /uri/ { - } 符号 含义 = 开头表示精确匹配 ^~ 开头表示 uri 以某个常规字符串开头 ...

  8. 前端开发掌握nginx常用功能之server&location匹配规则

    nginx主要是公司运维同学必须掌握的知识,涉及到反向代理.负载均衡等服务器配置.前端开发尤其是纯前端开发来说对nginx接触的并不多,但是在一些情况下,nginx还是需要前端自己来搞:例如我们公司的 ...

  9. nginx多虚拟主机优先级location匹配规则及tryfiles的使用

    nginx多虚拟主机优先级location匹配规则及tryfiles的使用 .相同server_name多个虚拟主机优先级访问 .location匹配优先级 .try_files使用 .nginx的a ...

随机推荐

  1. C++ 提取字符串中的数字

    C++ 提取字符串中的数字 #include <iostream> using namespace std; int main() { ] = "1ab2cd3ef45g&quo ...

  2. OpenStack Swift集群部署流程与简单使用

    之前介绍了<OpenStack Swift All In One安装部署流程与简单使用>,那么接下来就说一说Swift集群部署吧. 1. 简介 本文档详细描述了使用两台PC部署一个小型Sw ...

  3. ytu 2030: 求实数绝对值(水题)

    2030: 求实数绝对值 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 10  Solved: 10[Submit][Status][Web Board] ...

  4. 怎么判定一个mac地址是multicast还是unicast.

    MAC地址是以太网二层使用的一个48bit(6字节十六进制数)的地址,用来标识设备位置.MAC地址分成两部分,前24位是组织唯一标识符(OUI, Organizationally unique ide ...

  5. poj 2245 水题

    求组合数,dfs即可 #include<cstdio> #include<iostream> #include<algorithm> #include<cst ...

  6. android 布局中 layout_gravity、gravity、orientation、layout_weight【转】

    线性布局中,有 4 个及其重要的参数,直接决定元素的布局和位置,这四个参数是 android:layout_gravity ( 是本元素相对于父元素的重力方向 ) android:gravity (是 ...

  7. IIS上发布WCF发布服务,访问不到

    1 环境是IIS7,发布WCF发布服务,访问不到. 一种原因站点自动生成“程序应用池”和站点的Framwork版本不一致. 解决的办法:新建一个“程序应用池”,然后站点指向这个新建的“程序应用池”

  8. SQL脚本--有关压缩数据库日志

    /*--压缩数据库的通用存储过程  压缩日志及数据库文件大小 因为要对数据库进行分离处理 所以存储过程不能创建在被压缩的数据库中 --邹建 2004.03(引用请保留此信息)--*/ /*--调用示例 ...

  9. HTML列表-框架

    <frameset frameborder="边框大小" 列cols="各窗口百分比,隔开" 行rows=“各窗口百分比”> <frame n ...

  10. BZOJ4068 : [Ctsc2015]app

    对于一个所选任务集合,如果对于任意时刻$i$,$i$前面所选任务数都不超过i的话,那么这些任务可以全选. 维护一棵线段树$T$,第$i$个位置一开始为$i$,每使用一个任务,$[t,T]$都要减$1$ ...