概述
1. Location的作用
Location指令的作用是根据用户请求的URI来执行不同的应用,也就是根据用户请求的网站URL进行匹配,匹配成功即进行相关的操作。
2.Location的语法
- 以
=
开头表示精确匹配,如 A 中只匹配根目录结尾的请求,后面不能带任何字符串。 ^~
开头表示uri以某个常规字符串开头,不是正则匹配~
开头表示区分大小写的正则匹配;~*
开头表示不区分大小写的正则匹配/
通用匹配, 如果没有其它匹配,任何请求都会匹配到
3.Location正则案例
1.精确匹配,/后面不能带任何字符:
<span style="color:#000000"><code class="language-java">server <span style="color:#969896">{</span>
listen <span style="color:#c76b29">80</span><span style="color:#969896">;</span>
server_name www<span style="color:#969896">.</span>baidu<span style="color:#969896">.</span>com<span style="color:#969896">;</span>
#精确匹配<span style="color:#969896">,</span>注解后面不能带任何字符
location <span style="color:#df5000">=</span><span style="color:#df5000">/</span> <span style="color:#969896">{</span>
proxy_pass http<span style="color:#df5000">:</span><span style="color:#df5000">/</span><span style="color:#df5000">/</span><span style="color:#c76b29">127.0</span><span style="color:#c76b29">.0</span><span style="color:#c76b29">.1</span><span style="color:#df5000">:</span><span style="color:#c76b29">8080</span><span style="color:#969896">;</span>
index index<span style="color:#969896">.</span>html index<span style="color:#969896">.</span>htm<span style="color:#969896">;</span>
<span style="color:#969896">}</span>
<span style="color:#969896">}</span>
</code></span>
2.匹配所有以/开头请求:
<span style="color:#000000"><code class="language-java">server <span style="color:#969896">{</span>
listen <span style="color:#c76b29">80</span><span style="color:#969896">;</span>
server_name www<span style="color:#969896">.</span>baidu<span style="color:#969896">.</span>com<span style="color:#969896">;</span>
#匹配所有以<span style="color:#df5000">/</span>开头请求
location <span style="color:#df5000">/</span> <span style="color:#969896">{</span>
proxy_pass http<span style="color:#df5000">:</span><span style="color:#df5000">/</span><span style="color:#df5000">/</span><span style="color:#c76b29">127.0</span><span style="color:#c76b29">.0</span><span style="color:#c76b29">.1</span><span style="color:#df5000">:</span><span style="color:#c76b29">8080</span><span style="color:#969896">;</span>
index index<span style="color:#969896">.</span>html index<span style="color:#969896">.</span>htm<span style="color:#969896">;</span>
<span style="color:#969896">}</span>
<span style="color:#969896">}</span>
</code></span>
3.比如以开头/ylw_8080拦截 默认开启不区分大小写:
<span style="color:#000000"><code class="language-java">server <span style="color:#969896">{</span>
listen <span style="color:#c76b29">80</span><span style="color:#969896">;</span>
server_name www<span style="color:#969896">.</span>baidu<span style="color:#969896">.</span>com<span style="color:#969896">;</span>
### 以开头<span style="color:#df5000">/</span>ylw_8080 最终跳转到http<span style="color:#df5000">:</span><span style="color:#df5000">/</span><span style="color:#df5000">/</span><span style="color:#c76b29">127.0</span><span style="color:#c76b29">.0</span><span style="color:#c76b29">.1</span><span style="color:#df5000">:</span><span style="color:#c76b29">8080</span><span style="color:#df5000">/</span><span style="color:#969896">;</span>
location <span style="color:#df5000">/</span>ylw_8080<span style="color:#df5000">/</span> <span style="color:#969896">{</span>
proxy_pass http<span style="color:#df5000">:</span><span style="color:#df5000">/</span><span style="color:#df5000">/</span><span style="color:#c76b29">127.0</span><span style="color:#c76b29">.0</span><span style="color:#c76b29">.1</span><span style="color:#df5000">:</span><span style="color:#c76b29">8080</span><span style="color:#df5000">/</span><span style="color:#969896">;</span>
index index<span style="color:#969896">.</span>html index<span style="color:#969896">.</span>htm<span style="color:#969896">;</span>
<span style="color:#969896">}</span>
### 以开头<span style="color:#df5000">/</span>ylw_8080 最终跳转到http<span style="color:#df5000">:</span><span style="color:#df5000">/</span><span style="color:#df5000">/</span><span style="color:#c76b29">127.0</span><span style="color:#c76b29">.0</span><span style="color:#c76b29">.1</span><span style="color:#df5000">:</span><span style="color:#c76b29">8081</span><span style="color:#df5000">/</span><span style="color:#969896">;</span>
location <span style="color:#df5000">/</span>ylw_8081<span style="color:#df5000">/</span> <span style="color:#969896">{</span>
proxy_pass http<span style="color:#df5000">:</span><span style="color:#df5000">/</span><span style="color:#df5000">/</span><span style="color:#c76b29">127.0</span><span style="color:#c76b29">.0</span><span style="color:#c76b29">.1</span><span style="color:#df5000">:</span><span style="color:#c76b29">8081</span><span style="color:#df5000">/</span><span style="color:#969896">;</span>
index index<span style="color:#969896">.</span>html index<span style="color:#969896">.</span>htm<span style="color:#969896">;</span>
<span style="color:#969896">}</span>
<span style="color:#969896">}</span>
</code></span>
注意:开头区分大小写!
最后
以上就是体贴发带为你收集整理的Nginx系列教程(07) - Location正则表达式的全部内容,希望文章能够帮你解决Nginx系列教程(07) - Location正则表达式所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复