Nginx web 服务器支持if条件表达式,由此来跳转或者使用不同的配置变量。在本文中需要使用 $http_user_agent 变量,它标记了用户浏览器的类别,版本以及操作系统的一些信息
if ( condition ){ do_something } if ( $http_user_agent = "wget" ){ do_something } if ( $http_user_agent ~ MSIE ){ return 403; }
upstream myproxybackend { server 192.168.1.1; server 192.168.1.2; server 192.168.1.3; server 192.168.1.4; } upstream msiebackend { server 192.168.1.6; } upstream mozillabackend { server 192.168.1.5; }
server { access_log logs/access.log; error_log logs/error.log; index index.html; listen 202.54.1.5:80 default; root /usr/local/nginx/html; server_name example.com www.example.com 0.example.com; ## PROXY - Web location / { proxy_pass http://myproxybackend; if ($http_user_agent ~ MSIE ) { proxy_pass http://msiebackend; } if ($http_user_agent ~ Mozilla ) { proxy_pass http://mozillabackend; } proxy_next_upstream error timeout invalid_header http_500 http_502 http_503; # many more... # many more... }
#/usr/local/nginx/sbin/nginx -s reload