Nginx根据用户请求的不同参数返回不同的json值

  用户请求url:http://localhost:8000/getconfig?v=1.03.01,根据参数v=1.03.01或者其他的值返回不同的json值。如果用户请求不带该参数,则返回默认的json值。

  下面是nginx.conf的配置:

server {
        listen       8000;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;

        location ~ ^/getconfig {
			default_type application/json;
			if ( $query_string ~* ^(.*)v=1.03.01$ ){
			return 200 '{"status": "0","message": "OK","body": {"api":"http://116.62.113.124:8080/basestock000/","h5":"http://118.31.69.127:8090/","phone":"4000670019","cr":"Copyright xxxxx"}}';
			}
			return 200 '{"status": "0","message": "OK","body": {"api":"http://116.62.113.124:8080/basestock000/","h5":"http://118.31.69.127:8090/","phone":"4000670019","cr":"Copyright xxxxx"}}';
        }
}

  用户请求http://localhost:8000/getconfig时,应该返回

return 200 '{"status": "0","message": "OK","body": {"api":"http://116.62.113.124:8080/basestock000/","h5":"http://118.31.69.127:8090/","phone":"4000670019","cr":"Copyright xxxxx"}}';

  浏览器模拟请求:http://localhost:8000/getconfig

  

  浏览器请求:http://localhost:8000/getconfig?v=1.03.01时:

 

  可以满足需求,当有多个参数值的时候,本人没有想出来别的更好的办法,本来以为可以使用if else或者if else if的,结果我在测试的时候配置检查都不通过,没办法就采用了下面的办法:

server {
        listen       8000;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;

        location ~ ^/getconfig {
            default_type application/json;
            if ( $query_string ~* ^(.*)v=1.03.01$ ){
            return 200 '{"status": "0","message": "OK","body": {"api":"http://116.62.113.124:8080/basestock000/","h5":"http://118.31.69.127:8090/","phone":"4000670019","cr":"Copyright xxxxx"}}';
            }
            if ( $query_string ~* ^(.*)v=1.03.02$ ){
            return 200 '{"status": "0","message": "OK","body": {"api":"http://116.62.113.124:8080/basestock122/","h5":"http://118.31.69.127:8092/","phone":"40006700191222","cr":"Copyright xxxxx1222"}}';
            }
            return 200 '{"status": "0","message": "OK","body": {"api":"http://116.62.113.124:8080/basestock000/","h5":"http://118.31.69.127:8090/","phone":"4000670019","cr":"Copyright xxxxx"}}';
        }
}

  请求http://localhost:8000/getconfig?v=1.03.02:

  虽然也可以达到目的,但是感觉特别low,希望哪位大神有别的比较高端的解决方法可以指导一下 ^=^!

转载于:https://www.cnblogs.com/jie-fang/p/8554934.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值