nginx-基于请求的标头的响应 [英] nginx - response based on the requested header

查看:143
本文介绍了nginx-基于请求的标头的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have nginx 1.0.8 installed. here is my problem: I have 2 files : file1.js and file2.js. the requested path is something like this:

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

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

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

If the requested header : "X-Header" exists and has the value "OK" then the responded content should be file1.js else file2.js.

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

The files are situated in "html/files_dir" and %user% is a set of directories that represents the usernames registered through my service.

如何在nginx中配置它?只有对nginx可行,我对php,asp或类似技术不感兴趣.

How do I configure this in nginx? I'm not interested in php, asp or similar technologies only if it's possible with nginx.

谢谢

推荐答案

map可让您基于另一个变量定义变量的值. map应该在http级别(即在server之外)声明:

map lets you define a variable's value based on another variable. map should be declared at http level (i.e. outside of server):

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

然后,以下location应该使用新变量$file_suffix

Then the following location should do the trick using your new variable $file_suffix

location ~ ^(/files_dir/.+)\.js$ {
  root html;
  try_files $1$file_suffix.js =404;
}

这篇关于nginx-基于请求的标头的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆