如何正确编写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 ?
最佳答案
您可以使用 map
、location
和 alias
指令根据 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/