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

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

  下面是nginx.conf的配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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,希望哪位大神有别的比较高端的解决方法可以指导一下 ^=^!


__EOF__

本文作者StaryJie
本文链接https://www.cnblogs.com/jie-fang/p/8554934.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   StaryJie  阅读(1160)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示