18,nginx proxy_pass 规则


 location /ios/ {                            #这种情况,这里一定要匹配的是/ios/,不能是/ios

    proxy_pass http://192.168.1.102:8090;       #一定要保证192.168.1.102机器8090端口站点目录下有ios目录!否则访问会报错404!     proxy_redirect off ;     proxy_set_header Host $host;     proxy_set_header X-Real-IP $remote_addr;

1)

配置proxy_pass时,当在后面的url加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;

如果没有/,则会把匹配的路径部分也给代理走.

2) nginx配置

[root@localhost ~]# cd /etc/nginx/conf.d/

[root@localhost conf.d]# cat test.conf server { listen 80; server_name localhost; location / { root /var/www/html; index index.html; } }   [root@localhost conf.d]# cat /var/www/html/index.html this is page of test!!!! 3)启动Nginx [root@localhost ~]# service nginx start     //或者使用 systemctl start nginx.service    4)测试访问(103.110.186.23是192.168.1.23机器的外网ip) [root@localhost conf.d]# curl http://192.168.1.23 this is page of test!!!!   5) 为了方便测试,先在另一台机器192.168.1.5上部署一个8090端口的nginx,配置如下: [root@bastion-IDC ~]# cat /usr/local/nginx/conf/vhosts/haha.conf server { listen 8090; server_name localhost; location / { root /var/www/html; index index.html; } } [root@bastion-IDC ~]# cat /var/www/html/index.html this is 192.168.1.5 [root@bastion-IDC ~]# /usr/local/nginx/sbin/nginx -s reload   测试访问(103.110.186.5是192.168.1.5的外网ip): [root@bastion-IDC ~]# curl http://192.168.1.5:8090 this is 192.168.1.5   6)192.168.1.23作为nginx反向代理机器,nginx配置如下:  1)第一种情况: [root@localhost conf.d]# cat test.conf server { listen 80; server_name localhost; location / { root /var/www/html; index index.html; }   location  /proxy/ {           proxy_pass http://192.168.1.5:8090/; } } 访问http://192.168.1.23/proxy/就会被代理到http://192.168.1.5:8090/。p匹配的proxy目录不需要存在根目录/var/www/html里面 注意,终端里如果访问http://192.168.1.23/proxy(即后面不带"/"),则会访问失败!因为proxy_pass配置的url后面加了"/" [root@localhost conf.d]# curl http://192.168.1.23/proxy/ this is 192.168.1.5 [root@localhost conf.d]curl http://192.168.1.23/proxy <head>301 Moved Permanently<</code><code class="bash plain">/title</code><code class="bash plain">><</code><code class="bash plain">/head</code><code class="bash plain">></code> <code class="bash plain"><body bgcolor=</code><code class="bash string">"white"</code><code class="bash plain">></code> <code class="bash plain"><center><h1>301 Moved Permanently<</code><code class="bash plain">/h1</code><code class="bash plain">><</code><code class="bash plain">/center</code><code class="bash plain">></code> <code class="bash plain"><hr><center>nginx</code><code class="bash plain">/1</code><code class="bash plain">.10.3<</code><code class="bash plain">/center</code><code class="bash plain">></code> <code class="bash plain"><</code><code class="bash plain">/body</code><code class="bash plain">></code> <code class="bash plain"><</code><code class="bash plain">/html</code><code class="bash plain">></code> <span data-mce-=""> 页面访问http://103.110.186.23/proxy的时候,会自动加上"/”(同理是由于proxy_pass配置的url后面加了"/"),并反代到http://103.110.186.5:8090的结果</span> <span data-mce-=""><span data-mce-=""></span></span> <p><span data-mce-="">7)第二种情况,proxy_pass配置的url后面不加"/"</span></p> <p><span data-mce-=""><code class="bash plain">[root@localhost conf.d]</code><code class="bash comments"># cat test.conf</code></span></p> <code class="bash plain">server {</code> <code class="bash plain">listen 80;</code> <code class="bash plain">server_name localhost;</code> <code class="bash plain">location / {</code> <code class="bash plain">root </code><code class="bash plain">/var/www/html</code><code class="bash plain">;</code> <code class="bash plain">index index.html;</code> <code class="bash plain">}</code> <code class="bash plain">location  </code><code class="bash plain">/proxy/</code> <code class="bash plain">{</code> <code class="bash spaces">          </code><code class="bash plain">proxy_pass http:</code><code class="bash plain">//192</code><code class="bash plain">.168.1.5:8090;</code> <code class="bash plain">}</code> <code class="bash plain">}</code> <code class="bash plain">[root@localhost conf.d]</code><code class="bash comments"># service nginx restart</code> <code class="bash plain">Redirecting to </code><code class="bash plain">/bin/systemctl</code> <code class="bash plain">restart  nginx.service</code> <code class="bash plain">那么访问http:</code><code class="bash plain">//192</code><code class="bash plain">.168.1.23</code><code class="bash plain">/proxy</code><code class="bash plain">或http:</code><code class="bash plain">//192</code><code class="bash plain">.168.1.23</code><code class="bash plain">/proxy/</code><code class="bash plain">,都会失败!</code> <code class="bash plain">这样配置后,访问http:</code><code class="bash plain">//192</code><code class="bash plain">.168.1.23</code><code class="bash plain">/proxy/</code><code class="bash plain">就会被反向代理到http:</code><code class="bash plain">//192</code><code class="bash plain">.168.1.5:8090</code><code class="bash plain">/proxy/</code> <span data-mce-=""><span data-mce-="">8)第三种情况</span></span> <p> <code class="bash plain">[root@localhost conf.d]</code><code class="bash comments"># cat test.conf</code></p> <code class="bash plain">server {</code> <code class="bash plain">listen 80;</code> <code class="bash plain">server_name localhost;</code> <code class="bash plain">location / {</code> <code class="bash plain">root </code><code class="bash plain">/var/www/html</code><code class="bash plain">;</code> <code class="bash plain">index index.html;</code> <code class="bash plain">}</code>   <code class="bash plain">location  </code><code class="bash plain">/proxy/</code> <code class="bash plain">{</code> <code class="bash spaces">          </code><code class="bash plain">proxy_pass http:</code><code class="bash plain">//192</code><code class="bash plain">.168.1.5:8090</code><code class="bash plain">/haha/</code><code class="bash plain">;</code> <code class="bash plain">}</code> <code class="bash plain">}</code> <code class="bash plain">[root@localhost conf.d]</code><code class="bash comments"># service nginx restart</code> <code class="bash plain">Redirecting to </code><code class="bash plain">/bin/systemctl</code> <code class="bash plain">restart  nginx.service</code> <code class="bash plain">[root@localhost conf.d]</code><code class="bash comments"># curl http://192.168.1.23/proxy/</code> <code class="bash plain">192.168.1.5  haha-index.html</code> 这样配置的话,访问http://103.110.186.23/proxy代理到http://192.168.1.5:8090/haha/ <p><span data-mce-="">9)第四种情况:相对于第三种配置的url不加"/"</span></p> <p><span data-mce-=""><code class="bash plain">[root@localhost conf.d]</code><code class="bash comments"># cat test.conf</code></span></p> <code class="bash plain">server {</code> <code class="bash plain">listen 80;</code> <code class="bash plain">server_name localhost;</code> <code class="bash plain">location / {</code> <code class="bash plain">root </code><code class="bash plain">/var/www/html</code><code class="bash plain">;</code> <code class="bash plain">index index.html;</code> <code class="bash plain">}</code>   <code class="bash plain">location  </code><code class="bash plain">/proxy/</code> <code class="bash plain">{</code> <code class="bash spaces">          </code><code class="bash plain">proxy_pass http:</code><code class="bash plain">//192</code><code class="bash plain">.168.1.5:8090</code><code class="bash plain">/haha</code><code class="bash plain">;</code> <code class="bash plain">}</code> <code class="bash plain">}</code> <code class="bash plain">[root@localhost conf.d]</code><code class="bash comments"># service nginx restart</code> <code class="bash plain">Redirecting to </code><code class="bash plain">/bin/systemctl</code> <code class="bash plain">restart  nginx.service</code> <code class="bash plain">[root@localhost conf.d]</code><code class="bash comments"># curl http://192.168.1.23/proxy/index.html</code> <code class="bash plain">192.168.1.5   hahaindex.html</code> <code class="bash plain">上面配置后,访问http:</code><code class="bash plain">//192</code><code class="bash plain">.168.1.23</code><code class="bash plain">/proxy/index</code><code class="bash plain">.html就会被代理到http:</code><code class="bash plain">//192</code><code class="bash plain">.168.1.5:8090</code><code class="bash plain">/hahaindex</code><code class="bash plain">.html</code> <code class="bash plain">同理,访问http:</code><code class="bash plain">//192</code><code class="bash plain">.168.1.23</code><code class="bash plain">/proxy/test</code><code class="bash plain">.html就会被代理到http:</code><code class="bash plain">//192</code><code class="bash plain">.168.1.5:8090</code><code class="bash plain">/hahatest</code><code class="bash plain">.html</code> <code class="bash plain">[root@localhost conf.d]</code><code class="bash comments"># curl http://192.168.1.23/proxy/index.html</code> <code class="bash plain">192.168.1.5   hahaindex.html</code> <span data-mce-="">注意,这种情况下,不能直接访问http:<code class="bash plain">//192</code><code class="bash plain">.168.1.23</code><code class="bash plain">/proxy/</code><code class="bash plain">,后面就算是默认的index.html文件也要跟上,否则访问失败!</code></span> <p><span data-mce-=""><strong>上面四种方式都是匹配的path路径后面加"/",下面说下path路径后面不带"/"的情况:</strong></span></p> <p><span data-mce-=""><strong>1)第一种情况,proxy_pass后面url带"/":</strong></span></p> <p><span data-mce-=""><strong><code class="bash plain">[root@localhost conf.d]</code><code class="bash comments"># cat test.conf</code></strong></span></p> <code class="bash plain">server {</code> <code class="bash plain">listen 80;</code> <code class="bash plain">server_name localhost;</code> <code class="bash plain">location / {</code> <code class="bash plain">root </code><code class="bash plain">/var/www/html</code><code class="bash plain">;</code> <code class="bash plain">index index.html;</code> <code class="bash plain">}</code> <code class="bash spaces"> </code>  <code class="bash plain">location  </code><code class="bash plain">/proxy</code> <code class="bash plain">{</code> <code class="bash spaces">          </code><code class="bash plain">proxy_pass http:</code><code class="bash plain">//192</code><code class="bash plain">.168.1.5:8090/;</code> <code class="bash plain">}</code> <code class="bash plain">}</code> <code class="bash plain">[root@localhost conf.d]</code><code class="bash comments"># service nginx restart</code> <code class="bash plain">Redirecting to </code><code class="bash plain">/bin/systemctl</code> <code class="bash plain">restart  nginx.service</code> <p>2)第二种情况,proxy_pass后面url不带"/"</p> <p><code class="bash plain">[root@localhost conf.d]</code><code class="bash comments"># cat test.conf</code></p> <code class="bash plain">server {</code> <code class="bash plain">listen 80;</code> <code class="bash plain">server_name localhost;</code> <code class="bash plain">location / {</code> <code class="bash plain">root </code><code class="bash plain">/var/www/html</code><code class="bash plain">;</code> <code class="bash plain">index index.html;</code> <code class="bash plain">}</code> <code class="bash spaces"> </code>  <code class="bash plain">location  </code><code class="bash plain">/proxy</code> <code class="bash plain">{</code> <code class="bash spaces">          </code><code class="bash plain">proxy_pass http:</code><code class="bash plain">//192</code><code class="bash plain">.168.1.5:8090;</code> <code class="bash plain">}</code> <code class="bash plain">}</code> <code class="bash plain">[root@localhost conf.d]</code><code class="bash comments"># service nginx restart</code> <code class="bash plain">Redirecting to </code><code class="bash plain">/bin/systemctl</code> <code class="bash plain">restart  nginx.service</code> <code class="bash plain">[root@localhost conf.d]</code><code class="bash comments">#</code> 这样配置的话,访问http://103.110.186.23/proxy会自动加上"/”(即变成http://103.110.186.23/proxy/),代理到192.168.1.5:8090/proxy/ <p>3)第三种情况</p> <p><code class="bash plain">[root@localhost conf.d]</code><code class="bash comments"># cat test.conf</code></p> <code class="bash plain">server {</code> <code class="bash plain">listen 80;</code> <code class="bash plain">server_name localhost;</code> <code class="bash plain">location / {</code> <code class="bash plain">root </code><code class="bash plain">/var/www/html</code><code class="bash plain">;</code> <code class="bash plain">index index.html;</code> <code class="bash plain">}</code> <code class="bash spaces"> </code>  <code class="bash plain">location  </code><code class="bash plain">/proxy</code> <code class="bash plain">{</code> <code class="bash spaces">          </code><code class="bash plain">proxy_pass http:</code><code class="bash plain">//192</code><code class="bash plain">.168.1.5:8090</code><code class="bash plain">/haha/</code><code class="bash plain">;</code> <code class="bash plain">}</code> <code class="bash plain">}</code> <code class="bash plain">[root@localhost conf.d]</code><code class="bash comments"># service nginx restart</code> <code class="bash plain">Redirecting to </code><code class="bash plain">/bin/systemctl</code> <code class="bash plain">restart  nginx.service</code> 这样配置的话,访问http://103.110.186.23/proxy会自动加上"/”(即变成http://103.110.186.23/proxy/),代理到http://192.168.1.5:8090/haha/ <span data-mce-=""><span data-mce-="">4)第四种情况:相对于第三种配置的url不加"/"</span></span> <p><code class="bash plain">[root@localhost conf.d]</code><code class="bash comments"># cat test.conf</code></p> <code class="bash plain">server {</code> <code class="bash plain">listen 80;</code> <code class="bash plain">server_name localhost;</code> <code class="bash plain">location / {</code> <code class="bash plain">root </code><code class="bash plain">/var/www/html</code><code class="bash plain">;</code> <code class="bash plain">index index.html;</code> <code class="bash plain">}</code> <code class="bash spaces"> </code>  <code class="bash plain">location  </code><code class="bash plain">/proxy</code> <code class="bash plain">{</code> <code class="bash spaces">          </code><code class="bash plain">proxy_pass http:</code><code class="bash plain">//192</code><code class="bash plain">.168.1.5:8090</code><code class="bash plain">/haha</code><code class="bash plain">;</code> <code class="bash plain">}</code> <code class="bash plain">}</code> <code class="bash plain">[root@localhost conf.d]</code><code class="bash comments"># service nginx restart</code> <code class="bash plain">Redirecting to </code><code class="bash plain">/bin/systemctl</code> <code class="bash plain">restart  nginx.service</code> 这样配置的话,访问http://103.110.186.23/proxy,和第三种结果一样,同样被代理到http://192.168.1.5:8090/haha/ </div> <!--conend--> <div class="p-2"></div> <div class="arcinfo my-3 fs-7 text-center"> </div> <div class="p-2"></div> </div> <div class="p-2"></div> <!--xg--> <div class="lbox p-4 shadow-sm rounded-3"> <div class="boxtitle"><h2 class="fs-4">相关</h2></div> <hr> <!----> <!----> </div> <!--xgend--> </div> <div class="col-lg-3 col-12 p-0 ps-lg-2"> <!--box--> <!--boxend--> <!--<div class="p-2"></div>--> <!--box--> <div class="lbox p-4 shadow-sm rounded-3"> <div class="boxtitle pb-2"><h2 class="fs-4"><a href="#">标签</a></h2></div> <div class="clearfix"></div> <ul class="m-0 p-0 fs-7 r-tag"> </ul> <div class="clearfix"></div> </div> <!--box end--> </div> </div> </div> </main> <div class="p-2"></div> <footer> <div class="container-fluid p-0 bg-black"> <div class="container p-0 fs-8"> <p class="text-center m-0 py-2 text-white-50">一品网 <a class="text-white-50" href="https://beian.miit.gov.cn/" target="_blank">冀ICP备14022925号-6</a></p> </div> </div> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?6e3dd49b5f14d985cc4c6bdb9248f52b"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> </footer> <script src="/skin/bootstrap.bundle.js"></script> </body> </html>