Nginx 模拟服务端返回客户端接口响应
场景:
我们服务器一般经常会有一些接口是需要对接第三方服务器,向第三方服务器发送请求,第三方响应后返回结果,往我们在测试过程中没有第三方的测试桩,无法得知消息接口是否发送成功,或根据不同的返回码,我们自己的业务会做出何种处理流程,所以需要一个测试桩来模拟第三方响应
工具:
Nginx
方法:
1、搭建好nginx服务,搭建方法不再详述,各项目都有在使用nginx
2、修改nginx的配置信息,在原有配置的基础上,增加一个测试使用的配置,样例见附件
test_upstreams.conf
## 1
server {
listen 8080;
server_name *.minitor.tencent-cloud.net;
access_log logs/$remote_addr-HttpCurveReportRpc.log main;
default_type "application/json;charset=UTF-8";
location /access_v1.http_service/HttpCurveReportRpc {
content_by_lua_block
{
-- ngx.exit(ngx.HTTP_BAD_REQUEST)
ngx.say('{"pkg_seq":"0","retcode":"0","retmsg":"成功","lastagenttime":"0","lastcollectortime":"0"}')
}
lua_need_request_body on;
}
}
## 2
server {
listen 80;
default_type "application/json;charset=UTF-8";
location /cgi-bin/cdn_monitor_report/out_monitor_report.cgi {
access_log logs/$remote_addr-out_monitor_report.log main;
content_by_lua_block
{
ngx.say('{"errno":"0","error":"verygood"}')
}
lua_need_request_body on;
}
## 3
location /interface/settlement_report {
access_log logs/$remote_addr-settlement_report.log main;
content_by_lua_block
{
ngx.say('{"ret":"0","msg":""}')
}
lua_need_request_body on;
}
}
## 4
server {
listen 8081;
default_type "application/json;charset=UTF-8";
location /report/monitor-info {
access_log logs/$remote_addr-monitor-info.log main;
content_by_lua_block
{
ngx.say('{"code":"10000","message":"10000","success":"true","respTime":"1656038527545"}')
}
lua_need_request_body on;
}
location /report/alarm-info {
access_log logs/$remote_addr-alarm-info.log main;
content_by_lua_block
{
ngx.say('{"code":"10000","message":"10000","success":"true","respTime":"1656038527545"}')
}
lua_need_request_body on;
}
}
## 5
server {
listen 8082;
default_type "application/json;charset=UTF-8";
location /omsnew/alarmlistsyn {
access_log logs/$remote_addr-alarmlistsyn.log main;
content_by_lua_block
{
ngx.say('{"taskid":"12345678","resultCode":"2","resultDesc":"success1111"}')
}
lua_need_request_body on;
}
}
vim /home/nginx/conf/nginx.conf
# worker_process :recommend to set the same number of the CPU cores
user root root;
worker_processes 1;
error_log logs/error.log notice;
pid sbin/nginx.pid;
#pcre_jit on;
events {
#Modified according to the situation
accept_mutex_delay 5ms;
worker_connections 65530;
}
http {
include mime.types;
## MIME types
default_type application/octet-stream;
## Size Limits
client_body_buffer_size 1m;
client_header_buffer_size 16K;
client_max_body_size 1m;
client_body_in_single_buffer on;
proxy_buffers 16 64k;
#large_client_header_buffers 1 1k;
## Timeouts
keepalive_timeout 65;
#keepalive_disable msie6;
sendfile on;
## TCP options
#tcp_nodelay on;
#tcp_nopush on;
## Compression
#gzip on;
#gzip_min_length 1024;
#gzip_types text/css text/javascript application/xml text/json;
#gzip_disable "MSIE [1-6]\.";
## Log Format
#log_format main 'request:$request|request_body:$request_body|time_is:$time_iso8601|remote_addr:$remote_addr|remote_user:$remote_user|response_time:$upstream_response_time|request_time:$request_time|browser_type:$http_user_agent|response_body:$resp_body';
#log_format access
#'time=$time_local || remote_addr=$remote_addr || upstream_addr=$upstream_addr || upstream_status=$upstream_status || upstream_response_time=$upstream_response_time || Cache=$upstream_cache_status || body_bytes=$body_bytes_sent || request=$request || status=$status ';
#access_log logs/access.log access;
#log_format main '$remote_addr - [$time_local] '
# '[$request_body]';
#log_format main escape=json '$time_iso8601 -- $remote_addr -- request_message: $request_body';
log_format main escape=json '$fmt_localtime request_message: $request_body';
#proxy_cache_path cache levels=2:2 keys_zone=mycache:300m inactive=12h max_size=30g;
#proxy_buffers 16 64k;
#proxy_set_header X-Real-IP $remote_addr;
map $host $fmt_localtime {
default '';
}
log_by_lua_block {
ngx.var.fmt_localtime = ngx.localtime();
}
server {
listen 8199;
server_name 127.0.0.1;
proxy_set_header Host $http_host;
location / {
root html;
index index.html index.htm;
}
# location /status
# {
# access_log off;
# iag_status on;
# iag_status_local on;
# }
# location /iag
# {
# root html;
# }
#################### config ####################
#clean cache by hand
#para is the name of cache zone
# location = /querycache {
# proxy_cache_query mycache;
# }
#location = /delcache {
# proxy_cache_delete mycache;
# }
# check the status of iag:the active numbers and so on
location = /iag_stat
{
iag_stat on;
}
# error pages
error_page 500 502 503 504 /50x.html;
location = /50x.html
{
root html;
}
#################### service ####################
}
include /home/nginx/conf/test_upstreams.conf;
}
在原 http框架中添加:
include /home/nginx/conf/test_upstreams.conf;
3、添加以上红框目录下文件
以接口为例,编辑配置文件 test_upstreams.conf
4、在客户端上添加域名对应的ip信息
由于是模拟客户端(也就是我们业务的服务器环境)向第三方域名发送请求,所以在hosts文件中添加域名与ip对应关系
该文介绍了如何利用Nginx搭建测试桩,模拟第三方服务器的接口响应,以便在没有实际第三方测试环境时,测试自己的业务流程。通过修改Nginx配置文件,使用`content_by_lua_block`指令返回预设的JSON响应,包括不同状态和消息,确保接口交互的正确性。
1519

被折叠的 条评论
为什么被折叠?



