nginx的proxy_pass路径转发规则浅析(末尾/问题)

2019-09-09 09:38:34

参考链接 nginx的proxy_pass路径转发规则浅析(末尾/问题) 另外,比如  /api/good/list  与 /api/list  怎么区分


答案:     

     location ~ ^/api/.*/.*{

                        proxy_pass http://127.0.0.1:8080;

                                }

                location / {

                        try_files $uri $uri/ /index.php?$query_string;

                }


一 location匹配路径末尾没有 /

此时proxy_pass后面的路径必须拼接location的路径:

1

2

3

4

5

6

7

8

location /sta

{

   proxy_redirect off;

   proxy_set_header        Host $host;

   proxy_set_header        X-Real-IP $remote_addr;

   proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

   proxy_pass http://192.168.1.31/sta;

}


  • 外面访问:http://192.168.1.30/sta/sta1.html

  • 相当于访问:http://192.168.1.31/sta/sta1.html

注:这里也可以写成:“proxy_pass http://192.168.1.31/sta/;”。当然,不推荐使用上面这种写法

二 location匹配路径末尾有 /

此时proxy_pass后面的路径需要分为以下四种情况讨论:

(1)proxy_pass后面的路径只有域名且最后没有 /:


1

2

3

4

5

6

7

8

location /sta/

{

   proxy_redirect off;

   proxy_set_header        Host $host;

   proxy_set_header        X-Real-IP $remote_addr;

   proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

   proxy_pass http://192.168.1.31;

}


  • 外面访问:http://192.168.1.30/sta/sta1.html

  • 相当于访问:http://192.168.1.31/sta/sta1.html

(2),


1

2

3

4

5

6

7

8

location /sta/

{

   proxy_redirect off;

   proxy_set_header        Host $host;

   proxy_set_header        X-Real-IP $remote_addr;

   proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

   proxy_pass http://192.168.1.31/;

}


  • 外面访问:http://192.168.1.30/sta/sta1.html

  • 相当于访问:http://192.168.1.31/sta1.html

(3)proxy_pass后面的路径还有其他路径但是最后没有 /:

1

2

3

4

5

6

7

8

location /sta/

{

   proxy_redirect off;

   proxy_set_header        Host $host;

   proxy_set_header        X-Real-IP $remote_addr;

   proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

   proxy_pass http://192.168.1.31/abc;

}


  • 外面访问:http://192.168.1.30/sta/sta1.html

  • 相当于访问:http://192.168.1.31/abcsta1.html

(4)proxy_pass后面的路径还有其他路径同时最后有 /:


1

2

3

4

5

6

7

8

location /sta/

{

   proxy_redirect off;

   proxy_set_header        Host $host;

   proxy_set_header        X-Real-IP $remote_addr;

   proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

   proxy_pass http://192.168.1.31/abc/;

}


  • 外面访问:http://192.168.1.30/sta/sta1.html

  • 相当于访问:http://192.168.1.31/abc/sta1.html

附:在nginx上面配置APK文件下载路径:

1

2

3

4

5

6

7

8

9

location ^~ /h5/appdownload/

{

      proxy_redirect off;

      proxy_set_header        Host $host;

      proxy_set_header        X-Real-IP $remote_addr;

      proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

      proxy_pass http://192.168.1.31/;

      proxy_set_header   Cookie $http_cookie;

}


  • 外面访问:http://test.com/h5/appdownload/Demo_1.0.0.apk

  • 相当于访问:http://192.168.1.31/Demo_1.0.0.apk

每次更新apk文件,只需要上传新的apk文件到192.168.1.31服务器,然后再更新对外的下载地址为http://test.com/h5/appdownload/newName.apk即可,并不需要更改nginx的任何配置


  • 2018-11-28 09:55:53

    android radiogroup样式(设置切换背景与文字颜色)

    RadioButton(单选按钮)在Android开发中应用的非常广泛,比如一些选择项的时候,会用到单选按钮。它是一种单选框双状态的按钮,可以选择或不选择。在RadioButton没有被选中时,用户能够按下或点击来选中它。

  • 2018-12-01 00:27:12

    批量kill mysql processlist进程

    如果大批量的操作能够通过一系列的select语句产生,那么理论上就能对这些结果批量处理。 但是mysql并没用提供eval这样的对结果集进行分析操作的功能。所以只能现将select结果保存到临时文件中,然后再执行临时文件中的指令。

  • 2018-12-02 10:54:14

    HTTP长连接、短连接究竟是什么?

    HTTP的长连接和短连接本质上是TCP长连接和短连接。HTTP属于应用层协议,在传输层使用TCP协议,在网络层使用IP协议。 IP协议主要解决网络路由和寻址问题,TCP协议主要解决如何在IP层之上可靠地传递数据包,使得网络上接收端收到发送端所发出的所有包,并且顺序与发送顺序一致。TCP协议是可靠的、面向连接的。

  • 2018-12-04 15:30:01

    如何在Mac OS X上安装 Ruby运行环境

    ​ 对于新入门的开发者,如何安装 Ruby和Ruby Gems 的运行环境可能会是个问题,本页主要介绍如何用一条靠谱的路子快速安装 Ruby 开发环境。 此安装方法同样适用于产品环境!

  • 2018-12-04 15:31:15

    iOS--Pod install && Pod update

    许多人在最初接触CocoaPods时认为pod install只是在第一次为项目设置CocoaPods时使用,之后都应该使用pod update.看起来是这样,但也不是(But that's not the case at all.)。 这篇文章的目的就是教你啥时候用pod install,啥时候用pod update

  • 2018-12-04 15:33:19

    CocoaPods安装和使用教程

    当你开发iOS应用时,会经常使用到很多第三方开源类库,比如JSONKit,AFNetWorking等等。可能某个类库又用到其他类库,所以要使用它,必须得另外下载其他类库,而其他类库又用到其他类库,“子子孙孙无穷尽也”,这也许是比较特殊的情况。总之小编的意思就是,手动一个个去下载所需类库十分麻烦。另外一种常见情况是,你项目中用到的类库有更新,你必须得重新下载新版本,重新加入到项目中,十分麻烦。如果能有什么工具能解决这些恼人的问题,那将“善莫大焉”。所以,你需要 CocoaPods。