1. location的详细用法

location [ = | ~ | ~* | ^~ ] uri { ... }

用于实现从uri到文件系统的路径映射;ngnix会根据用户请求的URI来检查定义的所有location,并找出一个最佳匹配,而后应用其配置。

uri前符号说明

  • = 对URI做精确匹配;
  • ^~对URI的最左边部分做匹配检查,不区分字符大小写
  • ~对URI做正则表达式模式匹配,区分字符大小写
  • !~ 对URI做正则表达式模式不匹配,区分字符大小写
  • ~*对URI做正则表达式模式匹配,不区分字符大小写
  • 不带符号匹配起始于此uri的所有的uri
  • / 通用匹配。任何请求都会匹配到
  • * 表示任意长度的任意字符

匹配优先级从高到低如下:

(location =) > (location 完整路径) > (location ^~ 路径) > (location ~/~* 路径) > (location 部分起始路径) > (/)

1.1 精确匹配

#示例:精确匹配doc目录下的一个文件名。
#1.nginx的配置文件如下:
[root@nginx01 web1]# vim /etc/nginx/conf.d/virtualhost.conf
server {
listen 80;
server_name www.nginx01.com;
location / {
root /data/nginx/html/web1;
index index.html;
}
location = /file1.txt {
root /data/nginx/html/web1/doc;
index index.html;
}
} #2.重启nginx服务
[root@nginx01 web1]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@nginx01 web1]# systemctl reload nginx.service #3.新建doc目录和file1.txt文件
[root@nginx01 web1]# mkdir doc
[root@nginx01 web1]# echo "abc" > doc/file1.txt #4.客户端测试
[root@xuzhichao ~]# curl http://www.nginx01.com/file1.txt
abc

1.2 区分大小写

#1.nginx的配置如下:
[root@nginx01 web1]# vim /etc/nginx/conf.d/virtualhost.conf
server {
listen 80;
server_name www.nginx01.com;
location / {
root /data/nginx/html/web1;
index index.html;
}
location ~ /file1\.txt {
root /data/nginx/html/web1/doc;
index index.html;
}
} #2.重启nginx服务
[root@nginx01 web1]# systemctl reload nginx.service #3.在doc下新建文件
[root@nginx01 web1]# echo "qwe" > doc/file1.TXT
[root@nginx01 web1]# tree doc/
doc/
├── file1.txt
└── file1.TXT #4.客户端如果访问File1.txt或file.TXT,则无法匹配到location ~ /file1.txt语句块
[root@xuzhichao ~]# curl http://www.nginx01.com/file1.txt
abc
[root@xuzhichao ~]# curl http://www.nginx01.com/file1.TXT
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>

1.3 不区分大小写

对用户的请求的uri做模糊匹配,不区分大小写,无论是大写,小写还是大小写混合都会匹配,但是需要有对应的资源(大写,小写,大小写混合)才能访问成功。此模式通常用于匹配用户的静态资源。

#1.nginx的配置文件如下:
root@nginx01 web1]# vim /etc/nginx/conf.d/virtualhost.conf
server {
listen 80;
server_name www.nginx01.com;
location / {
root /data/nginx/html/web1;
index index.html;
}
location ~* /file1\.txt {
root /data/nginx/html/web1/doc;
index index.html;
}
} #2.doc下的文件如下:
[root@nginx01 web1]# tree doc/
doc/
├── file1.txt
└── file1.TXT #3.重启nginx服务
[root@nginx01 web1]# systemctl reload nginx.service #4.在客户端测试访问file1.txt和file1.TXT
[root@xuzhichao ~]# curl http://www.nginx01.com/file1.TXT
qwe
[root@xuzhichao ~]# curl http://www.nginx01.com/file1.txt
abc

1.4 匹配URI开始

#1.nginx的配置文件如下:
[root@nginx01 ~]# vim /etc/nginx/conf.d/virtualhost.conf
server {
listen 80;
server_name www.nginx01.com;
location / {
root /data/nginx/html/web1;
index index.html;
}
location ^~ /doc {
root /data/nginx/html/web1;
index index.html;
}
location /doc1 {
alias /data/nginx/html/web1;
index index.html;
}
} #2.web1站点的目录结构如下:
[root@nginx01 web1]# cat /data/nginx/html/web1/index.html
www.nginx01.com
[root@nginx01 web1]# cat /data/nginx/html/web1/doc/index.html
doc.index #3.重启nginx服务
[root@nginx01 web1]# systemctl reload nginx.service #4.客户端测试
[root@xuzhichao ~]# curl http://www.nginx01.com/doc1/
www.nginx01.com
[root@xuzhichao ~]# curl http://www.nginx01.com/doc/
doc.index

1.5 测试location的优先级

#1.nginx的配置如下:
[root@nginx01 ~]# cat /etc/nginx/conf.d/virtualhost.conf
server {
listen 80;
server_name www.nginx01.com; location = / {
default_type text/html;
return 200 'location = /\n';
}
location / {
default_type text/html;
return 200 'location /\n';
}
location /doc/dir1 {
default_type text/html;
return 200 'location /doc/dir1\n';
}
location ^~ /images {
default_type text/html;
return 200 'location ^~ /images\n';
}
location /doc {
default_type text/html;
return 200 'location /doc\n';
}
location ~* \.(jpg|gif|mp4)$ {
default_type text/html;
return 200 'location ~* \.(jpg|gif|mp4)\n';
}
} #2.重启nginx服务
[root@nginx01 web1]# systemctl reload nginx.service #3.使用客户端访问不同的uri测试location的优先级
[root@xuzhichao ~]# curl http://www.nginx01.com/
location = / [root@xuzhichao ~]# curl http://www.nginx01.com/index.html
location / [root@xuzhichao ~]# curl http://www.nginx01.com/doc
location /doc [root@xuzhichao ~]# curl http://www.nginx01.com/doc/dir1
location /doc/dir1 [root@xuzhichao ~]# curl http://www.nginx01.com/doc/1.txt
location /doc [root@xuzhichao ~]# curl http://www.nginx01.com/images/1.jpg
location ^~ /images [root@xuzhichao ~]# curl http://www.nginx01.com/doc/1.jpg
location ~* \.(jpg|gif|mp4) [root@xuzhichao ~]# curl http://www.nginx01.com/doc/dir1/1.jpg
location ~* \.(jpg|gif|mp4)

1.6 location的生产使用示例

#1.直接匹配网站根会加速nginx的访问处理
location / {
......;
} #2.访问静态资源,不区分大小写
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
......;
} #3.精准匹配
location = /nginx_status {
......;
} #4.区分大小写,匹配php资源
location ~ \.php$ {
......;
} #5.多应用配置
location ~* /app1 {
......;
}
location ~* /app2 {
......;
}

WEB服务与NGINX(6)-location使用详解的更多相关文章

  1. nginx之location配置详解及案例

    语法规则: location [=|~|~*|^~] /uri/ { … } =  开头表示精确匹配 ^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可.nginx不对url做编码, ...

  2. nginx与location语法详解

    Location语法优先级排列 匹配符 匹配规则 优先级 = 精确匹配 ^~ 以某个字符串开头 ~ 区分大小写的正则匹配 ~* 不区分大小写的正则匹配 !~ 区分大小写不匹配的正则 !~* 不区分大小 ...

  3. nginx 与location语法详解

    Location语法优先级排列   匹配符 匹配规则 优先级 = 精确匹配 1 ^~ 以某个字符串开头 2 ~ 区分大小写的正则匹配 3 ~* 不区分大小写的正则匹配 4 !~ 区分大小写不匹配的正则 ...

  4. nginx的location配置详解

    语法规则: location [=|~|~*|^~] /uri/ { … } =开头表示精确匹配 ^~开头表示uri以某个常规字符串开头,理解为匹配url路径即可.nginx不对url做编码,因此请求 ...

  5. 【转】nginx服务器安装及配置文件详解

    原文:http://seanlook.com/2015/05/17/nginx-install-and-config/ nginx服务器安装及配置文件详解 nginx在工作中已经有好几个环境在使用了, ...

  6. [转帖]nginx服务器安装及配置文件详解

    nginx服务器安装及配置文件详解 http://seanlook.com/2015/05/17/nginx-install-and-config/  发表于 2015-05-17 |  更新于: 2 ...

  7. Nginx安装及配置详解【转】

    nginx概述 nginx是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是一个IMAP.POP3.SMTP代理服务器:nginx可以作为一个HTTP服务器进行网站的发布处理,另外 ...

  8. [转帖]Nginx安装及配置详解 From https://www.cnblogs.com/zhouxinfei/p/7862285.html

    Nginx安装及配置详解   nginx概述 nginx是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是一个IMAP.POP3.SMTP代理服务器:nginx可以作为一个HTTP ...

  9. 【第六课】Nginx常用配置下详解

    目录 Nginx常用配置下详解 1.Nginx虚拟主机 2.部署wordpress开源博客 3.部署discuz开源论坛 4.域名重定向 5.Nginx用户认证 6.Nginx访问日志配置 7.Ngi ...

  10. Nginx简介及配置文件详解

    http://blog.csdn.net/hzsunshine/article/details/63687054 一 Nginx简介    Nginx是一款开源代码的高性能HTTP服务器和反向代理服务 ...

随机推荐

  1. Python爬虫爬取京东某商品评论信息存入mysql数据库

    1 """ 2 https://club.jd.com/comment/productPageComments.action?callback=fetchJSON_com ...

  2. Linux——ssh登录很慢解决方法

    1.背景 在同一机房中,有多台安装了CentOS 7操作系统的服务器,它们的配置除了IP地址不同外基本相同.这些服务器的资源利用率都不高,但在使用SSH连接时,发现有几台服务器连接速度较慢,可能需要等 ...

  3. kube-apiserver限流机制原理

    本文分享自华为云社区<kube-apiserver限流机制原理>,作者:可以交个朋友. 背景 apiserver是kubernetes中最重要的组件,一旦遇到恶意刷接口或请求量超过承载范围 ...

  4. 在linux上使用Qt开发动态库项目,怎么只生成一个so文件

    背景: 在linux系统上,我们使用 Qt 开发动态库项目时,会默认生成四个文件:x.so  .x.so.1 .x.so.1.0.x.so.1.0.0 四个文件,只有一个真实的so库,剩下的三个都是链 ...

  5. HarmonyOS 极客马拉松2023 正式启动,诚邀极客们用键盘码出无限可能!

      原文:https://mp.weixin.qq.com/s/p2yIs0rMmDE2BwhzsAtr7A,点击链接查看更多技术内容. 2023年6月15日, HarmonyOS极客马拉松2023开 ...

  6. c# 多线程传值注意的地方

    前言 下面介绍多线程传值的几种方式,并说明注意点. 正文 static void Main(string[] args) { SampleTread thead = new SampleTread(1 ...

  7. 使用Quorum Journal Manager(QJM)的HDFS NameNode高可用配置

    前面的一篇文章写到了hadoop hdfs 3.2集群的部署,其中是部署的单个namenode的hdfs集群,一旦其中namenode出现故障会导致整个hdfs存储不可用,如果是要求比较高的集群,有必 ...

  8. C#微服务必学清单

    在 C# 领域,有一些不错的微服务书籍和开源框架,对于学习微服务相关知识非常有帮助.以下是一些建议您阅读的微服务书目和开源框架. 微服务书目: 1. <Building Microservice ...

  9. 最最最简单使用Docker部署Wordpress

    普通Docker部署 这种方式我用过,但是总体来说是比较麻烦的.但是可以简单说一下流程,总体流程如下: 安装Docker环境 拉取Wordpress镜像,运行镜像 拉取MySql镜像,运行镜像 Wor ...

  10. 用百度和神策做埋点为何pv差异很大?

    近期ClkLog收到一个客户反馈说我们与百度统计的PV数据差异很大.为了验证问题,开发进行了一次对页面浏览量统计的测试.针对同一个IP同一个时间的页面浏览量统计发现,百度的统计数据只有一条,而ClkL ...