配置Nginx以根据浏览器类型有条件地提供静态内容

时间:2019-06-27 11:29:22

标签: nginx nginx-config

我有两个名为“ esc6”和“ esc5”的静态内容包,可同时支持esc6和esc5浏览器。我应该如何编写nginx.conf文件,以根据$ http_user_agent值有条件地切换位置指令。

Ex伪代码:

如果$ http_user_agent包含“ chrome || firefox || safari”,则为esc6-bundle 其他esc5-bundle

2 个答案:

答案 0 :(得分:1)

您应该使用map指令。有关详细信息,请参见this document

例如:

map $http_user_agent $bundle {
    default         /js/esc5-bundle.js;
    ~*Chrome        /js/esc6-bundle.js;
    ~*Firefox       /js/esc6-bundle.js;
    ~*Safari        /js/esc6-bundle.js;
}

server {
    ...
    location = /js/bundle.js {
        try_files $bundle =404;
    }
    ...
}

答案 1 :(得分:0)

在Nginx配置中尝试

  location / {
      if (($http_user_agent ~ "Chrome") || $http_user_agent ~ "firefox" ) || {
          rewrite ^ esc6-bundle ;
      }
      rewrite ^ esc5-bundle ;
   }

希望有帮助