nginx之location的匹配规则

时间:2023-01-14 05:50:37

nginx之location的匹配规则

一、语法规则

location [=|~|~*|^~] /uri/ { … }
符号 含义
= 开头表示精确匹配
^~ 开头表示 uri 以某个常规字符串开头
~ 开头表示区分大小写的正则匹配
~* 开头表示不区分大小写的正则匹配
/ 通用匹配,任何请求都会匹配到

匹配顺序: =, ^~,文件顺序,/

例如:

/规则:
匹配所有请求

/api/ 匹配:
http://localhost/api/
http://localhost/api/hello
http://localhost/api/o/hello

= /api 匹配:
http://localhost/api


^~ /static/ 匹配:
http://localhost/static/
http://localhost/static/css
http://localhost/static/img/a.png


~ \.(gif|jpg|png|js|css)$
匹配
http://localhost/logo.png
http://localhost/assert/style.css


~* \.(gif|jpg|png|js|css)$
匹配
http://localhost/logo.png
http://localhost/logo.PNG
http://localhost/assert/style.css
http://localhost/assert/style.CSS