nginx - 基于请求标头的响应
我安装了 nginx 1.0.8。 这是我的问题: 我有 2 个文件:file1.js 和 file2.js。请求的路径是这样的:
www.mysite.com/files_dir/%user%/file.js
如果请求的标头:“X-Header”存在并且值为“OK”,则响应内容应该是 file1.js,否则是 file2.js。
这些文件位于“html/files_dir”中,%user% 是一组目录,代表通过我的服务注册的用户名。
我如何在 nginx 中配置这个?我对 php、asp 或类似技术不感兴趣,除非它可以用 nginx 实现。
谢谢
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
If the requested header : "X-Header" exists and has the value "OK" then the responded content should be file1.js else file2.js.
The files are situated in "html/files_dir" and %user% is a set of directories that represents the usernames registered through my service.
How do I configure this in nginx? I'm not interested in php, asp or similar technologies only if it's possible with nginx.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
map
允许您根据另一个变量定义一个变量的值。map
应该在http
级别声明(即在server
之外):那么以下
location
应该可以解决问题使用新变量$file_suffix
map
lets you define a variable's value based on another variable.map
should be declared athttp
level (i.e. outside ofserver
):Then the following
location
should do the trick using your new variable$file_suffix
您可以使用 nginx 非常轻松地完成此操作。这是示例:
您可以在 此处 阅读有关 NGINX 中的 HTTP 变量的信息,以及有关 nginx 重写模块的信息 < a href="http://nginx.org/en/docs/http/ngx_http_rewrite_module.html" rel="nofollow">此处
You could do this with nginx very easily. This is example:
You could read about HTTP variables in NGINX here , and about nginx rewrite module here