我想添加一个请求标题,指明请求是来自桌面客户端还是移动客户端。
我试图在服务器块中添加这个
if ($http_user_agent ~* 'mobile|phone') && ($http_user_agent !~* 'ipad') { proxy_set_header X-Device-Category "mobile"; } else { proxy_set_header X-Device-Category "desktop"; }
但是nginx -t响应"if" directive is not allowed here
我可以从各种各样的post中了解到, if在Nginx中不能像你想象的那样工作,但是我不确定用其他方法来处理这个问题。
将这个简单的逻辑移到应用程序中不是一个选项,因为Nginx将需要这个caching键。
你可以通过map指令来代替if。
map $http_user_agent $device_category { default "desktop"; "~*ipad" "desktop"; "~*mobile|phone" "mobile"; }
然后在你的位置块:
proxy_set_header X-Device-Category $device_category;