请教如何通过反向代理请求路径时加入查询参数?呢? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
daimaosix
V2EX    NGINX

请教如何通过反向代理请求路径时加入查询参数?呢?

  •  
  •   daimaosix 2021-02-22 16:10:35 +08:00 2504 次点击
    这是一个创建于 1706 天前的主题,其中的信息可能已经有所发展或是发生改变。

    业务需要通过 Nginx 反向代理 Get 请求后端接口,但发现反向代理服务器访问路径中无法添加查询参数?

    原配置

     location /net/comm/ { proxy_pass http://localhost:9000; proxy_set_header Host localhost; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } 

    这样可以通过访问 http://a.com/net/comm/127.0.0.1 获取数据

    但需要将请求路径调整为 http://a.com/net/comm/?ip=127.0.0.1 获取数据时,调整了反代规则:

     location /net/comm/?ip= { proxy_pass http://localhost:9000; proxy_set_header Host localhost; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } 

    此时,反向代理服务器会报 404 错误。接着又进行了调整:

     location ^~ /net/comm/ { proxy_pass http://localhost:9000; proxy_set_header Host localhost; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } 

    此时,接口会返回 404 错误。

    麻烦 V 友给看看,需要怎么调整才可以实现带查询参数访问呢?将请求路径 http://a.com/net/comm/127.0.0.1 调整为 http://a.com/net/comm/?ip=127.0.0.1

    16 条回复    2021-02-22 17:56:40 +08:00
    maocat
        1
    maocat  
       2021-02-22 16:14:16 +08:00
    $query_string
    daimaosix
        2
    daimaosix  
    OP
       2021-02-22 16:15:27 +08:00
    @maocat 是这样的吗老哥
    location ^~ /net/comm/$query_string {
    proxy_pass http://localhost:9000;
    proxy_set_header Host localhost;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    ksmagi
        3
    ksmagi  
       2021-02-22 16:16:24 +08:00
    daimaosix
        4
    daimaosix  
    OP
       2021-02-22 16:17:04 +08:00
    @maocat 下午用$query_string 试了一下还是不行。。可能是我放的位置不对吧,测试了好几次都没通过。。
    oxromantic
        5
    oxromantic  
       2021-02-22 16:17:35 +08:00
    问句题外话, 既然 set_header 管用, 为了快速上线, 为啥不用 header 传参方式呢?
    ksmagi
        6
    ksmagi  
       2021-02-22 16:18:44 +08:00
    oxromantic
        7
    oxromantic  
       2021-02-22 16:23:07 +08:00
    @ksmagi 你这个只是客户端跳转行为吧, 不是反向代理
    daimaosix
        8
    daimaosix  
    OP
       2021-02-22 16:23:10 +08:00
    @oxromantic 主要是没用 header 做过。。。
    oxromantic
        9
    oxromantic  
       2021-02-22 16:25:50 +08:00
    @daimaosix 你的例子里都有 3 个设 header 的方式, 例子还不够么...
    daimaosix
        10
    daimaosix  
    OP
       2021-02-22 16:31:12 +08:00
    @oxromantic 这个我搜索了一下好像不能给 url 传参吧
    ksmagi
        11
    ksmagi  
       2021-02-22 16:38:42 +08:00   1
    @oxromantic rewrite 不是 redirect/permanent 的时候不是客户端重定向。
    解决方案如下:
    location = /test/ {
    proxy_pass http://127.0.0.1:8000;
    }
    location /test/ {
    rewrite ^/test/(.*)$ /test/?q=$1 last;
    }
    daimaosix
        12
    daimaosix  
    OP
       2021-02-22 16:49:56 +08:00
    @ksmagi 谢谢啊老哥,我刚才试了一下,接口还是返回 404 错误
    ksmagi
        13
    ksmagi  
       2021-02-22 16:55:07 +08:00
    @daimaosix 盲猜 location 匹配顺序上的问题 https://stackoverflow.com/a/45129826
    刚刚 at 错 oxromantic 老哥不好意思
    daimaosix
        14
    daimaosix  
    OP
       2021-02-22 17:02:50 +08:00
    @ksmagi 我又试了一遍,调整了一下,接口还是 404.。。
    daimaosix
        15
    daimaosix  
    OP
       2021-02-22 17:05:06 +08:00
    @ksmagi 是因为 proxy_pass 的不对嘛,我刚才检查了一下应该是 proxy_pass http://127.0.0.1:8000/ip/
    justseemore
        16
    justseemore  
       2021-02-22 17:56:40 +08:00
    location ^~ /net/comm/(.*) {
    proxy_pass http://127.0.0.1:8000/ip/?ip=$1&$query_string
    }
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1311 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 23ms UTC 16:55 PVG 00:55 LAX 09:55 JFK 12:55
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86