Nginx--Web核心配置示例之二

Location 使用详解

语法规则:location [=||*|^~] /uri/ {…}

= # 用于标准uri前,需要请求字符串与uri精确匹配,如果匹配成功就停止向下匹配并立即处理请求。

~ # 用于标准uri前,表示包含正则表达式并且区分大小写,并且匹配

!~ # 用于标准uri前,表示包含正在表达式并且不区分大小写,并且不匹配

~* #用于标准uri前,表示包含正则表达式并且不区分大小写,并且匹配

!~* #用于标准uri前,表示包含正则表达式并且不区分大小写,并且不匹配

^~ # 用于标准uri前,表示包含正则表达式并且匹配以什么开头

$ # 用于标准uri前,表示包含正则表达式并且匹配以什么结尾

\ # 用于标准uri前,表示包含正则表达式并且转义字符。可以转. * ? 等

* # 用于标准uri前,表示包含正则表达式并且代表任意长度的任意字符

示例一:
[root@centos7-3 conf]# vim /data/usr/local/nginx/conf/conf.d/locatest.conf

server {
listen 80;
server_name www.jyc.net;
location / {
root /data/jyc;
index index.html;
}
location = /1.txt {
root /data/jyc/text;
index index.html;
}
}
准备页面:
[root@centos7-3 conf]# cd /data/jyc/
[root@centos7-3 jyc]# ll
total 4
-rw-r–r-- 1 root root 24 Jun 27 09:26 index.html
drwxr-xr-x 2 root root 19 Jun 27 09:26 text
[root@centos7-3 jyc]# cd text/
[root@centos7-3 text]# ll
total 4
-rw-r–r-- 1 root root 26 Jun 27 09:26 1.txt
页面测试
root@ubuntu-1:~# curl www.jyc.net
jyc page 192.168.48.143
root@ubuntu-1:~# curl www.jyc.net/1.txt #当uri匹配1.txt的时候,优先选择该location
1.txt page 192.168.48.143

示例二:
server {
listen 80;
server_name www.jyc.net;
location ^~ /image { ##表示以image开通
root /data/jyc;
index index.html;
}
location /image1 { ##精确匹配
root /data/jyc;
index index.html;
}
}

测试
root@ubuntu-1:~# curl www.jyc.net/image/
image page 192.168.48.143
root@ubuntu-1:~# curl www.jyc.net/image1/ ##当精确的时候,优先于正则表达式
image1 page 192.168.48.143

示例三:测试包含什么关键字的匹配顺序
server {
listen 80;
server_name www.jyc.net;
location / { ##
root /data/jyc;
index index.html;
}
location ~* .(jpg|tif) {
root /data/jyc/image1;
index index.html;
}
}

示例:
root@ubuntu-1:~# curl www.jyc.net/
jyc page 192.168.48.143
root@ubuntu-1:~# curl www.jyc.net/1.jpg
1.jpc 192.168.48.143

匹配优先级:
= > ^~ > ~ / ~* > /

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值