5

我安装了 nginx 1.0.8。这是我的问题:我有 2 个文件:file1.jsfile2.js。请求的路径是这样的:

www.mysite.com/files_dir/%user%/file.js

如果请求的标头:“ X-Header ”存在且值为“ OK ”,则响应的内容应为 file1.js,否则为 file2.js。

这些文件位于“ html/files_dir ”中,%user% 是一组目录,代表通过我的服务注册的用户名。

我如何在 nginx 中配置它?只有在 nginx 有可能的情况下,我才对 php、asp 或类似技术不感兴趣。

谢谢

4

2 回答 2

9

map允许您根据另一个变量定义变量的值。map应在http级别声明(即在 之外server):

map $http_x_header $file_suffix {
  default "2";
  OK      "1";
};

然后以下location应该使用您的新变量来解决问题$file_suffix

location ~ ^(/files_dir/.+)\.js$ {
  root html;
  try_files $1$file_suffix.js =404;
}
于 2012-02-07T07:28:41.743 回答
1

你可以很容易地用 nginx 做到这一点。这是示例:

location /files_dir/ {

    set $file = file2.js;
    if ( $http_x_header = OK ) {
        set $file = file1.js;
    }
    rewrite ^(/files_dir/.*)/file.js$ $1/$file last;

}

你可以在这里阅读 NGINX 中的 HTTP 变量,以及这里的 nginx 重写模块

于 2012-02-07T07:08:56.940 回答