NGINX

时间:2017-12-23 14:20:59

标签: 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

1 个答案:

答案 0 :(得分:1)

您可以使用maplocationalias指令根据标头的值将特定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阻止只需要将404响应更改为403响应。

有关详情,请参阅this document