Nginx detailed location matching rules

Bobo roast duck 2020-11-13 05:00:12
nginx detailed location matching rules

In this paper, we will give you a detailed introduction to Nginx In the core configuration file Location Matching rules .
location Will try to use the URI To match the above /uri expression , If it can match , Just choose location{} Block to handle user requests . Of course , There are many ways to match ,
Let's introduce location Matching rules of .
grammar :

location [=|~|~*] /uri/ {

~ # The wavy line means performing a regular match , Case sensitive
~* # Means to perform a regular match , Case insensitive
= # Make exact matching of common characters

URI Matching mode

location There are two matching patterns
1. Ordinary string matching : With = Start with or without a regular guide sign (~) The rules
2. Regular matching : With () The beginning or (*) The beginning represents a regular match

Normal matching pattern

location / {

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

root html;
index demo.html;

1. Configure two location, The first is to match the root path ”/”, The other is matching “/demo” route
2. stay html Create one in the directory demo Catalog , because /demo It is equivalent to a virtual host directory , The final address you visit will become /demo/demo.html
3. Reload the configuration file ’./nginx -s reload’
4. By typing http://localhost/ as well as http://localhost/demo You can see that we have access to the corresponding path

Matching rules :
location It's not a strict match , It is a “ Prefix matching ” The process , So in the case above , Two location Can match , But normal matching follows a longest matching rule , In the above request , Final uri Will match to the maximum length location. That is to say /demo

Precise matching patterns

In the normal matching pattern , It can also be subdivided into a precise matching pattern , That is, through the equal sign to match directly

location =/demo {

root html;
index gp.html;
location /demo {

root html;
index demo.html;

Let's continue to add an example based on location =/demo Matching rules of , Then the matching at this time is precise matching . What's the difference between precise matching and normal matching ? And what the matching order looks like ?
http://localhost/demo √
http://localhost/demo/demo.html √
http://localhost/demo/gp.html √ ( Precise matching )
But it doesn't prove that it's an exact match here

#location =/demo {

# root html; 
# index gp.html;
location /demo {

root html;
index demo.html;

Comment out the exact match
http://localhost/demo √
http://localhost/demo/demo.html √
http://localhost/demo/gp.html √ ( Normal match )

location =/demo {

root html;
index gp.html;
#location /demo {

# root html;
# index demo.html;

We annotate the normal match and find that :
http://localhost/demo ×
http://localhost/demo/demo.html √
http://localhost/demo/gp.html √
The first way is not accessible . But it doesn't prove that precise matching has a higher priority than normal matching , In this case, we can set it like this

location =/index.html {
 # Precise matching 
root html/gp1;
index index.html;
location /index.html {
 # Normal match 
root html/gp2;
index index.html;

Then we will visit
 Insert picture description here
What I visited was gp1 In the directory , That means normal matching doesn't work .

Regular match pattern

Regular matching will also be used more in practical applications , For example, let's show you a case based on regular matching

location ~* \.(jpg|png|css|js|gif)$ {

root html/images;

And then we were in html Create under directory images Folder , Put a picture in it .Reload nginx After service , visit

 Insert picture description here

What is the priority of regular matching in the three matching patterns ? We talked about general matching , Finally, the largest prefix match will be chosen . But matching doesn't stop matching , The maximum match is just a temporary result ,nginx You also need to continue checking for regularities location. So what are regular matching rules like ? According to regularity location Match the physical order in the configuration file . If it matches a regular location, I don't think about the rules behind

Matching priority

  1. First of all, see if there is a precise match , If there is , Stop the matching process
  2. Judge normal hit , If there are multiple hits ,“ Record ” The longest hit result ( Record but not end )
  3. Continue to judge regular expressions , Follow the order of regular expressions in the configuration , Match from top to bottom , Once a match is made , Immediately return the result and end
    a) Normal hit , The order is irrelevant , Because it's determined by the length of the hit
    b) Regular hit , The order matters , Because it's from the past to the back

 Insert picture description here

Suggestions for practical use

So in practice , There are at least three matching rule definitions
Matches the site root directly , It is more frequent to visit the homepage of the website through domain name , Using this will speed things up
This is forwarded directly to the backend application server , It can also be a static home page
The first mandatory rule

location =/ {

proxy_pass http://tomcat:8080/index

The second required rule is to process static file requests , This is a nginx As http Server strengths
There are two configuration modes , Directory matching or suffix matching , Choose one or both

location ^~ /static/ {

root /webroot/static/;
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {

root /webroot/res/;

The third rule is the general rule , Used to forward dynamic requests to backend application servers
Non static file requests are dynamic by default , Oneself according to actual control

location / {

proxy_pass http://tomcat:8080/
本文为[Bobo roast duck]所创,转载请带上原文链接,感谢

  1. [front end -- JavaScript] knowledge point (IV) -- memory leakage in the project (I)
  2. This mechanism in JS
  3. Vue 3.0 source code learning 1 --- rendering process of components
  4. Learning the realization of canvas and simple drawing
  5. gin里获取http请求过来的参数
  6. vue3的新特性
  7. Get the parameters from HTTP request in gin
  8. New features of vue3
  9. vue-cli 引入腾讯地图(最新 api,rocketmq原理面试
  10. Vue 学习笔记(3,免费Java高级工程师学习资源
  11. Vue 学习笔记(2,Java编程视频教程
  12. Vue cli introduces Tencent maps (the latest API, rocketmq)
  13. Vue learning notes (3, free Java senior engineer learning resources)
  14. Vue learning notes (2, Java programming video tutorial)
  15. 【Vue】—props属性
  16. 【Vue】—创建组件
  17. [Vue] - props attribute
  18. [Vue] - create component
  19. 浅谈vue响应式原理及发布订阅模式和观察者模式
  20. On Vue responsive principle, publish subscribe mode and observer mode
  21. 浅谈vue响应式原理及发布订阅模式和观察者模式
  22. On Vue responsive principle, publish subscribe mode and observer mode
  23. Xiaobai can understand it. It only takes 4 steps to solve the problem of Vue keep alive cache component
  24. Publish, subscribe and observer of design patterns
  25. Summary of common content added in ES6 + (II)
  26. No.8 Vue element admin learning (III) vuex learning and login method analysis
  27. Write a mini webpack project construction tool
  28. Shopping cart (front-end static page preparation)
  29. Introduction to the fluent platform
  30. Webpack5 cache
  31. The difference between drop-down box select option and datalist
  32. CSS review (III)
  33. Node.js学习笔记【七】
  34. Node.js learning notes [VII]
  35. Vue Router根据后台数据加载不同的组件(思考->实现->不止于实现)
  36. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  37. 【JQuery框架,Java编程教程视频下载
  38. [jQuery framework, Java programming tutorial video download
  39. Vue Router根据后台数据加载不同的组件(思考->实现->不止于实现)
  40. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  41. 【Vue,阿里P8大佬亲自教你
  42. 【Vue基础知识总结 5,字节跳动算法工程师面试经验
  43. [Vue, Ali P8 teaches you personally
  44. [Vue basic knowledge summary 5. Interview experience of byte beating Algorithm Engineer
  45. 【问题记录】- 谷歌浏览器 Html生成PDF
  46. [problem record] - PDF generated by Google browser HTML
  47. 【问题记录】- 谷歌浏览器 Html生成PDF
  48. [problem record] - PDF generated by Google browser HTML
  49. 【JavaScript】查漏补缺 —数组中reduce()方法
  50. [JavaScript] leak checking and defect filling - reduce() method in array
  51. 【重识 HTML (3),350道Java面试真题分享
  52. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  53. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  54. [re recognize HTML (3) and share 350 real Java interview questions
  55. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  56. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  57. 【重识 HTML ,nginx面试题阿里
  58. 【重识 HTML (4),ELK原来这么简单
  59. [re recognize HTML, nginx interview questions]
  60. [re recognize HTML (4). Elk is so simple