Nginx的location配置详解

💡Location 是 Nginx 中一个非常核心的配置,通过location块,Nginx可以根据URI的不同部分进行匹配,并指定不同的行为,如代理请求、提供静态文件、重定向等。这篇重点讲解一下 Location 的配置问题以及一些注意事项。

语法

关于 Location,举个简单的配置例子:

js 复制代码
http { 
  server {
      listen 80;
    	server_name www.yayujs.com;
    	location / {
      	root /home/www/ts/;
	      index index.html;
    	}
  }
}

大致的意思是,当你访问 www.yayujs.com 的 80 端口的时候,返回 /home/www/ts/index.html 文件。

我们看下 Location 的具体语法:

css 复制代码
location [ = | ~ | ~* | ^~ ] uri { ... }

重点看方括号中的 [ = | ~ | * | ^ ],其中 | 分隔的内容表示你可能会用到的语法,其中:

  • = 表示精确匹配,比如:
js 复制代码
location = /test {
  return 200 "hello";
}

# /test ok
# /test/ not ok
# /test2 not ok
# /test/2 not ok
  • ~ 表示区分大小写的正则匹配,比如:
js 复制代码
location ~ ^/test$ {
  [ configuration ] 
}

# /test ok
# /Test not ok
# /test/ not ok
# /test2 not ok
  • ~* 表示不区分大小写的正则匹配
js 复制代码
location ~* ^/test$ {     
	[ configuration ] 
}

# /test ok
# /Test ok
# /test/ not ok
# /test2 not ok
  • ^~ 表示 uri 以某个字符串开头
js 复制代码
location ^~ /images/ {    
	[ configuration ] 
}

# /images/1.gif ok

而当你不使用这些语法的时候,只写 uri 的时候:

/ 表示通用匹配:

ini 复制代码
location / {     
	[ configuration ] 
}

# /index.html ok
shell 复制代码
location /test {
    [ configuration ] 
}

# /test ok
# /test2 ok
# /test/ ok

匹配顺序

location 的定义分为两种:

  • 前缀字符串(prefix string)
  • 正则表达式(regular expression),具体为前面带 ~* 和 ~ 修饰符的

在优先级上,= 修饰符最高,^~ 次之,再者是正则,最后是前缀字符串匹配。在使用前缀字符串的 locations 中选择最长匹配的,并将结果进行储存。

示例

bash 复制代码
server {
    location /doc {
        [ configuration A ] 
    }
    location /docu {
        [ configuration B ] 
    }
}

# 请求 /document 使用 configuration B
# 虽然 /doc 也能匹配到,但在顺序上,前缀字符串顺序不重要,按照匹配长度来确定
bash 复制代码
server {
    location ~ ^/doc {
        [ configuration A ] 
    }
    location ~ ^/docu {
        [ configuration B ] 
    }
}

# 请求 /document 使用 configuration A
# 虽然 ~ ^/docu 也能匹配到,但正则表达式则按照定义顺序
bash 复制代码
server {
    location ^~ /doc {
        [ configuration A ] 
    }
    location ~ ^/docu {
        [ configuration B ] 
    }
}

# 请求 /document 使用 configuration A
# 虽然 ~ ^/docu 也能匹配到,但 ^~ 的优先级更高
bash 复制代码
server {
    location /document {
        [ configuration A ] 
    }
    location ~ ^/docu {
        [ configuration B ] 
    }
}

# 请求 /document 使用 configuration B
# 虽然 /document 也能匹配到,但正则的优先级更高

root 与 alias 的区别

当我们这样设置 root 的时候:

bash 复制代码
location /i/ {
    root /data/w3;
}

当请求 /i/top.gif ,/data/w3/i/top.gif 会被返回。

当我们这样设置 alias 的时候:

bash 复制代码
location /i/ {
    alias /data/w3/images/;
}

当请求 /i/top.gif ,/data/w3/images/top.gif 会被返回。

root 是直接拼接 root + location,alias英文意思为别名[ˈeɪliəs],不管请求地址是什么,都是在alias定义的目录下找。

比如刚才的请求时/i/top.gif ,直接的请求路径就是 /data/w3/images/ + top.gif

所以如果你这样使用 allias 定义一个路径:

bash 复制代码
location / {
    alias /data/w3/images/;
}

请求是 a/b/i/top.gif ,最终请求地址为 /data/w3/images/ top.gif

server 和 location 中的 root

server 和 location 中都可以使用 root,举个例子:

ini 复制代码
http { 
  server {
      listen 80;
    	server_name www.yayujs.com;
    	root /home/www/website/;
    	location / {
      	root /home/www/ts/;
	      index index.html;
    	}
  }
}

如果两者都出现,是怎样的优先级呢?

简单的来说,就是就近原则,如果 location 中能匹配到,就是用 location 中的 root 配置,忽略 server 中的 root,当 location 中匹配不到的时候,则使用 server 中的 root 配置。

相关推荐
苹果醋32 小时前
快速玩转 Mixtral 8x7B MOE大模型!阿里云机器学习 PAI 推出最佳实践
spring boot·nginx·毕业设计·layui·课程设计
大G哥9 小时前
记一次K8S 环境应用nginx stable-alpine 解析内部域名失败排查思路
运维·nginx·云原生·容器·kubernetes
妍妍的宝贝9 小时前
k8s 中微服务之 MetailLB 搭配 ingress-nginx 实现七层负载
nginx·微服务·kubernetes
叶北辰CHINA12 小时前
nginx反向代理,负载均衡,HTTP配置简述(说人话)
linux·运维·nginx·http·云原生·https·负载均衡
Lansonli14 小时前
云原生(四十八) | Nginx软件安装部署
nginx·云原生·ecs服务器
加油,旭杏20 小时前
【中间件学习】fastCG介绍和使用
学习·nginx·fastcgi
苹果醋31 天前
大模型实战--FastChat一行代码实现部署和各个组件详解
java·运维·spring boot·mysql·nginx
tanxiaomi1 天前
vue 不是spa 单页面应用吗? 配置路由工作模式为history 后 ,为什么配置Nginx的 try_files 可以根据url 找到对应的文件?
前端·vue.js·nginx
twins35201 天前
配置Nginx以支持通过HTTPS回源到CDN
网络·nginx·https
astuv1 天前
在树莓派上部署开源监控系统 ZoneMinder
linux·nginx·树莓派·监控·摄像头·zoneminder·apache2