Nginx location配置详解

2023年1月5日 553点热度 0人点赞 0条评论
结论,简单理解为:代理的地址在端口以后如果有东西(包括目录或者/),转发地址会去除location匹配的目录(根据匹配的字符,如果是/api则去除api,如果是/api/则去除/api/)
如果代理地址到端口就没了(没有目录或/),那么转发地址会保留匹配目录
后期对照 alias与root的区别

参考一 :proxy_pass

1.location和proxy_pass都带/,则真实地址不带location匹配目录

<code class=" language-cpp">location <span class="token operator">/</span>api<span class="token operator">/</span><span class="token punctuation">{</span>proxy_pass http<span class="token operator">:</span><span class="token operator">/</span><span class="token operator">/</span><span class="token number">127.0</span><span class="token number">.0</span><span class="token number">.1</span><span class="token operator">:</span><span class="token number">8080</span><span class="token operator">/</span><span class="token punctuation">;</span><span class="token punctuation">}</span>
</code>

访问地址:www.test.com/api/upload-->http://127.0.0.1:8080/upload

2.location不带/,proxy_pass带/,则真实地址会带/

<code class=" language-cpp">location <span class="token operator">/</span>api<span class="token punctuation">{</span>proxy_pass http<span class="token operator">:</span><span class="token operator">/</span><span class="token operator">/</span><span class="token number">127.0</span><span class="token number">.0</span><span class="token number">.1</span><span class="token operator">:</span><span class="token number">8080</span><span class="token operator">/</span><span class="token punctuation">;</span><span class="token punctuation">}</span>
</code>

访问地址: www.test.com/api/upload-->http://127.0.0.1:8080//upload

3.location带/,proxy_pass不带/,则真实地址会带location匹配目录/api/

<code class=" language-cpp">location <span class="token operator">/</span>api<span class="token operator">/</span><span class="token punctuation">{</span>proxy_pass http<span class="token operator">:</span><span class="token operator">/</span><span class="token operator">/</span><span class="token number">127.0</span><span class="token number">.0</span><span class="token number">.1</span><span class="token operator">:</span><span class="token number">8080</span><span class="token punctuation">;</span><span class="token punctuation">}</span>
</code>

访问地址: www.test.com/api/upload-->http://127.0.0.1:8080/api/upload

支付 ¥4.99 购买本节后解锁剩余73%的内容

小小调酒师

此刻打盹,你将做梦; 此刻学习,你将圆梦。 个人邮箱:shellways@foxmail.com

文章评论