通过OpenResty修改UserAgent,非常简单,Demo里做了多次反向代理是为了日志输出显示效果。实际应用中不必这么麻烦。
浏览器访问如下地址即可
http://127.0.0.1:10090/
话不多说,直接上配置
upstream webapi_mydemo1{
server 127.0.0.1:10092;
}
upstream webapi_mydemo2{
server 127.0.0.1:10093;
}
server {
listen 10093;
location / {
content_by_lua_block{
ngx.print(“HelloWorld”)
}
}
}
server {
listen 10092;
set $newagentheader $http_user_agent;
location / {
access_by_lua_block{
ngx.var.newagentheader =”MyDemo Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko“
}
proxy_pass http://webapi_mydemo2;
proxy_set_header User-Agent $newagentheader;
}
}
server {
listen 10090;
set $newagentheader $http_user_agent;
location / {
proxy_pass http://webapi_mydemo1;
proxy_set_header User-Agent $newagentheader;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
以下访问日志
127.0.0.1 – – [23/Jul/2022:10:37:19 +0800] fwf[127.0.0.1] tip[-] – – – – 0.000 – – webapi_mydemo2 GET / HTTP/1.0 “200” 10 “-” “zh-CN” “MyDemo Agent Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko”
127.0.0.1 – – [23/Jul/2022:10:37:19 +0800] fwf[127.0.0.1] tip[-] 0.000 0.001 127.0.0.1:10093 0.001 0.001 10 164 webapi_mydemo1 GET / HTTP/1.0 “200” 10 “-” “zh-CN” “Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko”
127.0.0.1 – – [23/Jul/2022:10:37:19 +0800] fwf[-] tip[-] 0.000 0.001 127.0.0.1:10092 0.001 0.001 10 164 127.0.0.1:10090 GET / HTTP/1.1 “200” 10 “-” “zh-CN” “Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko”