• Nginx配置转发


     需求:

    同一域名+不同后缀转发到

    不同服务器ip+端口的webapi接口(198.122.133.4:800,199.122.133.5:800,197.122.133.6:800)

    说明:

    nginx location proxy_pass 后面的url 加与不加/的区别
    在nginx中配置proxy_pass时,当在后面的url加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;如果没有/,则会把匹配的路径部分也给代理走。

     

    首先是location进行的是模糊匹配

    1)没有“/”时,location /abc/def可以匹配/abc/defghi请求,也可以匹配/abc/def/ghi等
    2)而有“/”时,location /abc/def/不能匹配/abc/defghi请求,只能匹配/abc/def/anything这样的请求


     
     server {
            listen       80;
            server_name  www.baidu.com;
            location /test1/{  
                proxy_pass http://198.122.133.4:800/;   #转发请求的地址 
            } 
            location /test2/{  
                proxy_pass http://199.122.133.5:800/;   #转发请求的地址 
            } 
            location /test3/{  
                proxy_pass http://196.122.133.6:800/;   #转发请求的地址 
            } 

    访问 

    www.baidu.com/test1/ 调用的是 http://198.122.133.4:800/

    注意 四种情况
    第一种
    location  /test/ {
    
        proxy_pass http://127.0.0.1:5000/;
    
    }

    结论:会被代理到http://127.0.0.1:5000/这个url

     第二种(相对于第一种,最后少一个 /)

    location  /test/ {
         proxy_pass http://127.0.0.1:5000; 
    }

    结论:会被代理到http://127.0.0.1:5000/test/api这个url

    第三种
    location  /test/ {
    
    proxy_pass http://127.0.0.1:5000/test/;
    
    }

    结论:会被代理到http://127.0.0.1:5000/test/api这个url。

    第四种(相对于第三种,最后少一个 / ):
    location  /test/ {
    
         proxy_pass http://127.0.0.1:5000/test;
    
    }

    结论:会被代理到http://127.0.0.1:5000/testapi这个url

    最终配置

    location /sys/{                                             //转发路径
                proxy_set_header Host $http_host;               
                proxy_set_header X-Real-Ip $remote_addr;
                proxy_set_header REMOTE-HOST $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://192.168.2.45:8080/;           //后端地址
    
            }

    参考 http://events.jianshu.io/p/c9c29515b706

  • 相关阅读:
    基于小脚丫DDS 调频 调幅 调相 切换波形 AD5601输出模拟波形
    spi 10方式编写
    VGA colorbar显示
    hostname
    让CentOS能用yum自动安装rar和unrar
    Centos系统使用代理上网时 yum的代理设置
    【转】uvm 与 system verilog的理解
    跟我一起学习VIM
    gVim 配置方案 采用Vundle管理插件
    在 Ubuntu 16.04 中安装谷歌 Chrome 浏览器
  • 原文地址:https://www.cnblogs.com/qingjiawen/p/16136127.html
Copyright © 2020-2023  润新知