dictionary - NGINX 中用户代理的不同文件

标签 dictionary nginx user-agent

如何正确编写nginx服务器的配置?

...
if the client has useragent (A) and it refers to http://somehost.domain/somefile.someextension
       nginx responding a file from the root /file.zip
if the client has useragent (B) and it refers to http://somehost.domain/somefile.someextension
       nginx responding a file from the root /file2.zip
if the client has useragent (C) and it refers to http://somehost.domain/somefile.someextension
       nginx responding 403 error
...

我做了这段代码:

map $http_user_agent $browser {
        "~*Firefox"             "/var/www/test1";
        "~*Wget"                "/var/www/test2";
        "~*SomeUserAgent"       "/var/www/test3";
}

server {
...
root $browser

但是我如何获得传递到任何地址的条件 http://somehost.domain/somefile.someextension

最佳答案

您可以使用 maplocationalias 指令根据 header 的值将特定 URI 映射到多个文件。

例如(所有文件都在同一目录中):

map $http_user_agent $browser {
    default           "nonexistent";
    "~*Firefox"       "file.zip";
    "~*Wget"          "file1.zip";
}

server {
    ...
    location = /somefile.someextension {
        alias /path/to/directory/$browser;

        if (!-f $request_filename) {
            return 403;
        }
    }
}

if block 只需要将 404 响应更改为 403 响应。

参见 this document了解更多。

关于dictionary - NGINX 中用户代理的不同文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47953248/

相关文章:

javascript - GAS : HtmlService. getUserAgent() 使用 V8 时返回 null

java - 将映射值编码到接口(interface) - 声明 Map <String, List<>> 并分配 HashMap<String, ArrayList<>>()

python - 如何修复 "Error during WebSocket handshake: Unexpected response code: 400"错误

Django 和 nginx。我还需要 Apache 吗?

linux - centos 6.7 上的 Nginx 配置

javascript - 当用户代理是curl时,从浏览器返回不同的响应

ios - 如何在 Swift 中设置 UIWebView 的 "User-Agent" header

jsf - 迭代 primefaces 数据表中的 List<Map<String, String>> ?

java - 从java中的Map列表中删除值

带有 glib 的 C 和 dbus 字典