Nginx文件根基于客户端ip?

loo*_*dle 2 nginx

我可以根据客户端 ip 在 nginx 上提供不同的文档根目录吗?基本上我希望使用相同的 url 为我的客户端 IP 提供不同的代码分支。

例如,这就是我期望的配置:

server {
  listen 80;
  server_name some.server.name;

  client_max_body_size 10M;

  gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript application/json;

  root {{ deploy_directory }};

  location /robots.txt {}

  if ($remote_addr ~ "^(<ip>|<ip>|<ip>)$") {
    root <some other root>
  }

  location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    log_not_found off;
    gzip_static on;
    expires     max;
    add_header  Cache-Control public;
    add_header  Last-Modified "";
    add_header  ETag "";
    break;
  }
}
Run Code Online (Sandbox Code Playgroud)

但这没有加载

Ale*_*Ten 5

这应该有效:

root /$root;

set $root default/path;
if ($remote_addr ~ "^(127\.0\.0\.1|10\.20\.30\.40|111\.222\.33\.44)$") {
    set $root another/path;
}
Run Code Online (Sandbox Code Playgroud)

不要忘记在 IP 正则表达式中转义点。