---
title: "nginx路径匹配"
author: "Du_wood"
date: "2019-12-27T10:14:50Z"
tags: ["nginx", "nginx配置"]
categories: ["开源软件"]
slug: "nginx"
draft: false
lastmod: "2019-12-27T10:14:50Z"
id: 2511491
url: "https://dushen.iteye.com/blog/2511491"
---

语法: *location \[=|~|~\*|^~] /uri/ { ... }*

 

1. ~* 正则匹配不区分大小写
2. ~ 正则匹配区分大小写
3. ^~ 路径匹配,匹配后立即终止,不再尝试匹配正则
4. = 完全匹配,匹配后立即终止,不再尝试其他任何匹配

优先级:

完全匹配 &gt; 带^~的路径匹配 &gt; 正则(多个正则时优先级按配置顺序) &gt; 路径匹配

 

官方举例:

 

```
location  = / {

  # matches the query / only.
  [ configuration A ] 
}
location  / {
  # matches any query, since all queries begin with /, but regular
  # expressions and any longer conventional blocks will be

  # matched first.
  [ configuration B ] 
}
location ^~ /images/ {
  # matches any query beginning with /images/ and halts searching,
  # so regular expressions will not be checked.

  [ configuration C ] 
}
location ~* \.(gif|jpg|jpeg)$ {
  # matches any request ending in gif, jpg, or jpeg. However, all
  # requests to the /images/ directory will be handled by

  # Configuration C.   
  [ configuration D ] 
}
```

 

Example requests:

- / -&gt; configuration A
- /documents/document.html -&gt; configuration B
- /images/1.gif -&gt; configuration C
- /documents/1.jpg -&gt; configuration D