当前位置: 首页 > news >正文

docker nginx-lua发送post json 请求

环境准备 dockerfile

from fabiocicerchia/nginx-lua:1.25.3-ubuntu22.04
run apt-get -qq update && apt-get -qq install luarocks 
run luarocks install lua-cjson
run luarocks install lua-iconv
run luarocks install lua-resty-http

后台代理服务准备(python)

from flask import Flask, jsonify, request
import jsonapp = Flask(__name__)
app.config['JSON_AS_ASCII'] = False@app.route('/test', methods=['POST', 'GET','OPTIONS'])
def hello():request_data = request.get_json()print("Request data:", json.dumps(request_data, ensure_ascii=False, indent=4))data = {'message': '你好'}return jsonify(data)if __name__ == '__main__':app.run(host='0.0.0.0', port=8080)

代码准备

nginx.conf

user  nginx;
worker_processes  auto;error_log  /dev/stdout;
#error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;events {worker_connections  1024;
}http {include       /etc/nginx/mime.types;default_type  application/octet-stream;lua_package_path "/path/to/lua/?.lua;/another/path/?.lua;;";log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';log_format my escape=json ''
'{ "timestamp": "$time_iso8601", '
'"remote_addr": "$remote_addr",'
'"remote_port":"$remote_port",'
'"costime": "$request_time",'
'"status": "$status",'
'"request_method":"$request_method",'
'"request_uri":"$request_uri",'
'"request_body":"$request_body",'
'"response_body":"$resp_body",'
'"agent": "$http_user_agent" }'
'';#access_log  off;access_log  /dev/stdout my;#access_log  /var/log/nginx/access.log  main;sendfile        on;#tcp_nopush     on;keepalive_timeout  65s;gzip  on;#include /etc/nginx/conf.d/*.conf;server {listen       20000;server_name  localhost;#记录nginx请求返回值lua_need_request_body on;set $resp_body "";body_filter_by_lua 'local resp_body = string.sub(ngx.arg[1], 1, 1000)ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_bodyif ngx.arg[2] thenngx.var.resp_body = ngx.ctx.bufferedend';#access_log  /var/log/nginx/host.access.log  main;# 该请求测试引入其他位置的lua脚本,access_by_lua_file可以设置多个# send_track.lua是发送post json 请求的代码location /test/ {     access_by_lua_file /etc/nginx/workspace/send_track.lua;proxy_pass http://192.168.2.5:8080/;}#error_page  404              /404.html;# redirect server error pages to the static page /50x.html#error_page   500 502 503 504  /50x.html;location = /50x.html {root   /usr/share/nginx/html;}}}

send_track.lua

local http = require("resty.http")
local httpc = http.new()local res, err = httpc:request_uri("http://192.168.2.5:8080/test", {method = "POST",body = '{"key": "这是一个lua请求发送测试"}',headers = {["Content-Type"] = "application/json",["Authorization"] = "Bearer your_token"}
})if not res thenngx.log(ngx.ERR, err)endngx.log(ngx.ERR, res.body)
http://www.lryc.cn/news/338323.html

相关文章:

  • 阿里面试总结 一
  • 多线程(49)定义无锁、阻塞、非阻塞和无等待算法
  • (一)ffmpeg 入门基础知识
  • 【软件测试】个人博客系统测试
  • 20240410解决OK3588-C的核心板刷机之后无法启动的问题
  • 仅需三步就能成为大语言模型Prompt Engineer提示词工程大神
  • RuleEngine规则引擎底层改造AviatorScript 之公式规则
  • Vue项目(H5)与微信小程序来回跳转
  • 设计模式-单一职责原则
  • vue和nunjucks的变量插值的形式{{}}冲突
  • 多语言婚恋交友APP开发流程一览
  • RUM 最佳实践-交互延迟的探索与发现
  • spring boot 集成 flyway依赖 做数据库迁移,让部署没烦恼
  • TCP协议--传输机制
  • 句柄ros::NodeHandle nh(“~“)与nh对launch文件参数配置(param)的影响
  • C++_List的学习
  • centos 7.9 nginx本地化安装,把镜像改成阿里云
  • JVM规范中的运行时数据区
  • Stable Diffusion文生图技术详解:从零基础到掌握CLIP模型、Unet训练和采样器迭代
  • SDK-0.7.8-Release-实体管理 - ApiHug-Release
  • 3. DAX 时间函数-- DATE 日期--一生二,二生三,三生万物
  • c 解数独(通用方法,适用于9×9 数独)
  • 一篇文章深入学习Java的AQS(AbstractQueuedSynchronizer)
  • Linux sed
  • 【MySQL】MySQL在Centos 7环境安装
  • 【CSS】一篇文章讲清楚screen、window和html元素的位置:top、left、width、height
  • 铸造大型基础平板的结构应该怎样设计
  • 医院预约系统微信小程序APP前后端
  • springboot数字化智慧城市管理系统源码
  • 【鸿蒙开发】第二十一章 Media媒体服务(一)