1、使用nginx内置变量
nginx配置
server {
listen 80;
server_name testa.cc www.testa.cc;
location ~* /http/ {
add_header Content-Type "text/plain; charset=utf-8";
set $cusheader $http_cusheader;
return 200 "自定义请求头cusheader:$cusheader\n";
}
}
测试
[root@rocky9 ~]# curl -H "cusheader:666" http://www.testa.cc/http/
自定义请求头cusheader:666
[root@rocky9 ~]#
1.2、获取参数
nginx配置
server {
listen 80;
server_name testa.cc www.testa.cc;
root /data/html/testa;
index index.html;
location ~* /args/ {
add_header Content-Type "text/plain; charset=utf-8";
set $cusarg $arg_cusarg;
return 200 "获取到的nginx参数cusarg:$cusarg\n";
}
}
测试
[root@rocky9 ~]# curl http://www.testa.cc/args/?cusarg=666
获取到的nginx参数cusarg:666
[root@rocky9 ~]#
2、使用lua脚本
2.1、获取自定义请求头
server {
listen 80;
server_name testa.cc www.testa.cc;
root /data/html/testa;
index index.html;
location ~* /http2/ {
access_by_lua_block {
local ngx = require "ngx"
local cus_header = ngx.var.http_cusheader2
ngx.say('cus_header:', cus_header)
}
}
}
测试
[root@rocky9 ~]# curl -H "cusheader2:666" http://www.testa.cc/http2/
cus_header:666
[root@rocky9 ~]#
2.2、获取参数
nginx配置
server {
listen 80;
server_name testa.cc www.testa.cc;
root /data/html/testa;
index index.html;
location ~* /args2/ {
access_by_lua_block {
local ngx = require "ngx"
local cus_arg2 = ngx.var.arg_cusarg2
ngx.say('cus_arg2:', cus_arg2)
}
}
}
测试
[root@rocky9 ~]# curl http://www.testa.cc/args2/?cusarg2=666
cus_arg2:666
[root@rocky9 ~]#