nginx根据UA决定网站根目录


nginx的配置是很强大的,有很多变量可供使用。比如,有个场景:全静态的展示网站,根据用户的UA不同,显示不同版本的内容(假设网站没做响应式设计)。就可以根据变量$http_user_agent判断是否是Mobile用户。从而选择不同的网站根目录就可以了。

举例

网站目录结构如下

|—ua 网站根目录
||—mobile
|||—index.html 移动版网站 内容:hello mobile
||—index.html PC版网站 内容:hello world

nginx配置如下

location / {
if ($http_user_agent ~ Mobile){
root /path/to/mobile-web-root;
}
try_files $uri $uri/;
}

演示:http://ua.test.bootsphp.com

可尝试通过PC、手机、平板去访问,看输出内容。


发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注