nginx 利用 rewrite 屏蔽IE浏览器
前端兼容性问题尤其是 IE 浏览器常让人头疼,通常做法是给 IE 用户提示升级浏览器或展示简单静态页面,这就需要运维配置 nginx rewrite 规则。核心配置是在 server 块中用 if ( $http_user_agent ~* "MSIE [6-9].[0-9]") 匹配 IE 6 到 9 版本的浏览器标识,命中后通过 rewrite /ie.html break 返回指定静态页面。其中 $http_user_agent 是客户端浏览器标识(可在访问日志中查看),~* 表示正则匹配且不区分大小写,break 用于停止执行当前这一轮 ngx_http_rewrite_module 指令集。验证是否生效可用 IETester 模拟任意版本的 IE 来测试。
文章最后还整理了一份 nginx 全局变量速查,包括 $args、$content_length、$content_type、$document_root、$host、$http_user_agent、$http_cookie、$limit_rate、$request_method、$remote_addr、$request_filename、$query_string、$scheme、$server_protocol、$server_name、$request_uri、$uri 等,方便在编写 rewrite 及其他规则时参考取用。