通过Nginx(OpenResty)修改UserAgent

虚幻大学 xuhss 176℃ 0评论

? 优质资源分享 ?

学习路线指引(点击解锁) 知识定位 人群定位
? Python实战微信订餐小程序 ? 进阶级 本课程是python flask+微信小程序的完美结合,从项目搭建到腾讯云部署上线,打造一个全栈订餐系统。
?Python量化交易实战? 入门级 手把手带你打造一个易扩展、更安全、效率更高的量化交易系统

通过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"

转载请注明:xuhss » 通过Nginx(OpenResty)修改UserAgent

喜欢 (0)

您必须 登录 才能发表评论!