配置 Nginx 以根据浏览器类型有条件地提供静态内容 [英] Configure Nginx to serve static content conditionally based on the browser type

查看:50
本文介绍了配置 Nginx 以根据浏览器类型有条件地提供静态内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个名为esc6"和esc5"的静态内容包来为支持 esc6 和 esc5 的浏览器提供服务.我应该如何编写 nginx.conf 文件以根据 $http_user_agent 值有条件地切换位置指令.

I have two static content bundles named "esc6" and "esc5" to serve both esc6 and esc5 supportive browsers. How should I write the nginx.conf file to conditionally switch the location directive based on the $http_user_agent value.

Ex 伪代码:

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

if $http_user_agent contains "chrome || firefox || safari" then esc6-bundle else esc5-bundle

推荐答案

您应该使用 map 指令.有关详细信息,请参阅本文档.

You should use a map directive. See this document for details.

例如:

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;
    }
    ...
}

这篇关于配置 Nginx 以根据浏览器类型有条件地提供静态内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆