一、概述

nginx配置规则还是有点复杂的,在此只总结下本人遇到的一个坑与解决方法,具体原因还不清楚。

二、配置后没有生效的坑

1.首先,要访问的url样例是:

http://10.123.123.123:8080/b/c/getInfo
		 
http://10.123.123.123:8080/a/b/c/getMsg

nginx里配置的规则是:

        location /b/c/ {
          proxy_set_header Host $host;
          proxy_set_header Connection close;
          proxy_pass http://10.124.124.124:8089/api/other_Systems/;
         }

2.此时,访问:

http://10.123.123.123:8080/b/c/getInfo

会按照nginx的配置,转发到:

http://10.124.124.124:8089/api/other_Systems/getInfo

这个是没有问题的。

3.但是,访问:

http://10.123.123.123:8080/a/b/c/getMsg

本来以为也可以转发到:

http://10.124.124.124:8089/api/other_Systems/getMsg

结果并不是这样,访问的还是http://10.123.123.123:8080/a/b/c/getMsg,没有按规则转发。

三、解决方法

1.nginx需要这样配置:

        location /b/c/ {
          proxy_set_header Host $host;
          proxy_set_header Connection close;
          proxy_pass http://10.124.124.124:8089/api/other_Systems/;
         }
         
        location /a/b/c/ {
          proxy_set_header Host $host;
          proxy_set_header Connection close;
          proxy_pass http://10.124.124.124:8089/api/other_Systems/;
         }

这样,才能把:

http://10.123.123.123:8080/a/b/c/getMsg

转发到:

http://10.124.124.124:8089/api/other_Systems/getMsg

四、后记

1.改完nginx配置文件,要找到nginx启动文件,使用命令重启:sudo ./nginx -s reload,这个别忘了。

2.这个转发规则为什么只配置一个不行、配置两个才行,原因还不太清楚,在此先总结下解决方法。

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐