• 欢迎来到小爱博客,一个分享互联网IT技术的网站,喜欢就收藏吧!

Nginx location 匹配详解、location [=|~|~*|^~|!~|!~*] 详解

nginx 小爱 3周前 (10-03) 38次浏览 已收录 0个评论 扫描二维码

语法规则

location [=|~|~*|^~|!~|!~*] /uri/ { … }

模式 含义
location = /uri = 表示精确匹配
location ^~ /uri ^ 进行前缀匹配,~ 表示区分大小写
location ~ pattern ~ 区分大小写的匹配
location ~* pattern ~* 不区分大小写的匹配
location /uri 不带任何修饰符,也表示前缀匹配,但是在正则匹配之后
location / 通用匹配,任何未匹配到其它 location 的请求都会匹配到,相当于 switch 中的 default
location !~ 区分大小写不匹配
location !~* 不区分大小写不匹配

匹配优先级

  • 首先精确匹配 =
  • 其次前缀匹配 ^~
  • 其次是按文件中顺序的正则匹配
  • 然后匹配不带任何修饰的前缀匹配
  • 最后是交给 / 通用匹配
  • 当有匹配成功时候,停止匹配,按当前匹配规则处理请求

匹配的时候依照最佳匹配规则,按照能匹配到的最多的规则进行匹配
如 location ^~ /test/react/ 和 location ^~ /test/,请求 http://localhost/test/react/react.dev.js,会匹配 location /test/react/

使用示例

文件结构

<code class="prism language-bash has-numbering"><span class="token comment"># t.c => /index</span>
location <span class="token operator">=</span> / <span class="token punctuation">{</span>
	proxy_pass http://127.0.0.1:8888/index<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment"># http://t.c/static/react.development.js => /test-nginx/react.development.js</span>
location ^~ /static/ <span class="token punctuation">{</span>
 	root /home/uftp/test-nginx/<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment"># http://t.c/bizhi1.jpg => /test-nginx/static/assets/bizhi1.jpg</span>
location ~* \.<span class="token punctuation">(</span>gif<span class="token operator">|</span>jpg<span class="token operator">|</span>jpeg<span class="token operator">|</span>css<span class="token operator">|</span>js<span class="token operator">|</span>ico<span class="token punctuation">)</span>$ <span class="token punctuation">{</span>
	root /home/uftp/test-nginx/static/assets/<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment"># http://t.c/bizhi_sensitive.png 命中 casesensitive/bizhi_sensitive.png</span>
location ~ \.png$ <span class="token punctuation">{</span>
	root /home/uftp/test-nginx/static/casesensitive/<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment"># http://t.c/api/plmnji => http://127.0.0.1:8888/apitt/plmnji</span>
location ^~ /api <span class="token punctuation">{</span>
	proxy_pass http://127.0.0.1:8888/apitt<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment"># http://t.c/test/react/react.dev.js => http://127.0.0.1:8888/testreact/react.dev.js 不会匹配下面的规则,被上面的规则优先匹配了</span>
<span class="token comment"># http://t.c/test/react/akkk => http://127.0.0.1:8888/testreact/akkk</span>
location ^~ /test/react <span class="token punctuation">{</span>
	proxy_pass http://127.0.0.1:8888/testreact<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment"># http://t.c/test/qwerty => http://127.0.0.1:8888/test/qwerty</span>
location ^~ /test <span class="token punctuation">{</span>
	proxy_pass http://127.0.0.1:8888/test<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment"># http://t.c/vue/dasdas => http://127.0.0.1:8888/thisisvue/dasdas</span>
location  /vue <span class="token punctuation">{</span>
	proxy_pass http://127.0.0.1:8888/thisisvue<span class="token punctuation">;</span>
<span class="token punctuation">}</span></code>

小爱博客 , 版权所有
转载请注明原文链接:Nginx location 匹配详解、location [=|~|~*|^~|!~|!~*] 详解
喜欢 (0)
【你的支持, 我的动力】
分享 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址