大网数据广告活动
服务器运维问答

nginx serve from different static directory based on user-agent

· 52CE 我爱测

i have directory structure like this: HTML | |--android/ |--ios/ In short i have 2 directory(ios & android) in HTML/ Both directory contains index.html . In android/index.html there is link to ../ios & In ios/index.html there is …

问题原文

原帖链接 · vishant · CC BY-SA 3.0

i have directory structure like this:

HTML
|
|--android/
|--ios/

In short i have 2 directory(ios & android) in HTML/

Both directory contains index.html . In android/index.html there is link to ../ios & In ios/index.html there is link to ../android for switching user from ios to android & android to ios..

server {
    listen 80 default_server;

    root /home/vishant/HTML;
    index index.html index.htm;

    server_name wishfeed.l;

    location / {
            try_files $uri $uri/ =404;
    }
}

my nginx configuration is like given above..

How to serve directory if user agent is ios / if user agent is android.

社区高分解答

原帖链接 · Richard Smith · CC BY-SA 3.0

The map directive can generate an appropriate document root from the user-agent string down.

For example:

map $http_user_agent $root {
    "~*android" /home/vishant/HTML/android;
    "~iPhone" /home/vishant/HTML/ios;
    default /home/vishant/HTML;
}

You will have to work on the regular expressions yourself, as I do not know if they are right for all cases.

The above map block is placed above the server block within the http context of the configuration file. In this case, it creates a variable called $root which may be used within the server block.


If you need to serve the entire site's content from either subdirectory, use $root to set the global root like this:

server {
    ...
    root $root;

    location / {
        ...
    }
}

If you only want the URI /index.html to be affected, use $root within a location. For example:

location = /index.html {
    root $root;
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-07-09)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。