zoukankan      html  css  js  c++  java
  • 多级缓存-OpenResty获取请求参数

    OpenResty提供了各种API用来获取不同类型的请求参数:

     

    在查询商品信息的请求中,通过路径占位符的方式,传递了商品id到后台:

     需求:在OpenResty中接收这个请求,并获取路径中的id信息,拼接到结果的json字符串中返回

    nginx.conf配置:

    #user  nobody;
    worker_processes  1;
    error_log  logs/error.log;
    
    events {
        worker_connections  1024;
    }
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        
        #加载lua 模块
        lua_package_path "D:/dev/openresty-1.19.9.1/lualib/?.lua;;";  
        #加载c模块     
        lua_package_cpath "D:/dev/openresty-1.19.9.1/lualib/?.so;;"; 
    
        server {
            listen       80;
            server_name  localhost;
            
            location ~ /api/item/(d+) {
                # 响应类型,这里返回json
                default_type application/json;
                # 响应数据由 lua/item.lua这个文件来决定
                content_by_lua_file lua/item.lua;
            }
            location / {
                root   html;
                index  index.html index.htm;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
    }

     

    编写item.lua文件:

    --获取路径参数
    local id=ngx.var[1]
    
    
    --返回结果
    ngx.say('{"id":'..id..',"name":"SALSA AIR}')

     

    请求接口
    http://localhost/api/item/1001
    {"id":1001,"name":"SALSA AIR}

    http://localhost/api/item/1002
    {"id":1002,"name":"SALSA AIR}

    注意id值随着请求参数变化。

  • 相关阅读:
    fckeditor上传问题的解决
    重装系统
    JQuery中文日期控件
    Log4Net使用心得
    not过滤的几种写法
    三级日期选择
    Log4Net本地正常,发布到服务器却不能工作
    工具栏设置大按钮
    Log4Net与Log2Console配合时中文问题的解决
    服务器不能发邮件
  • 原文地址:https://www.cnblogs.com/linjiqin/p/15430371.html
Copyright © 2011-2022 走看看