nginx的配置使用相同的URI返回不同的文件,以不同的身份验证的用户 [英] Configure nginx to return different files to different authenticated users with the same URI
问题描述
我使用nginx的为静态文件在嵌入式系统中,没有CGI后端。我有基本的身份验证与多个用户名/密码。我想有一个特定的URI根据当前已验证用户的名称提供不同的内容。
I'm using nginx to serve static files in an embedded system, with no CGI back-end. I have basic-authentication up with multiple username/passwords. I'd like to have a specific URI deliver different content based on the name of the currently authenticated user.
例如,假设一个浏览器请求的URI /index.html。如果浏览器被认证为用户开发者,那么它可以送达文件/index_developer.html。相反,如果浏览器被认证为管理员,它可以被服务的文件/index_administrator.html。
For instance, let's say a browser requested URI /index.html. If the browser was authenticated as user "developer", then it could be served the file /index_developer.html. If instead the browser was authenticated as "administrator" it could be served the file /index_administrator.html.
我只希望在一个小文件集发生这种重定向;大多数将担任原样。我还需要阻止实际直接访问文件,这样没有人可以做一个终端运行系统周围。
I only want this redirection to occur on a small set of files; most will be served as-is. I'd also need to block direct access to the actual files so that nobody could do an end-run around the system.
推荐答案
首先,是可变的 $ REMOTE_USER
。
First, there is variable $remote_user
.
我和结构如下结束:
$ tree
.
├── _auth
│ ├── admin
│ │ ├── f
│ │ │ └── index.html
│ │ ├── hello.html
│ │ └── index.html
│ └── user
│ ├── f
│ │ └── index.html
│ └── index.html
├── f
│ └── x.html
├── hello.html
├── test.html
└── x
└── index.html
和这个nginx的配置:
and this nginx config:
auth_basic "Restricted area";
auth_basic_user_file path/to/passwd/file;
root /path/to/root;
location / {
try_files /_auth/$remote_user$uri
/_auth/$remote_user$uri/index.html
$uri $uri/index.html =404;
}
location /_auth/ {
internal;
}
所以请求 /
将在结束/ _认证/ USER / index.html的
,请求 /test.html
将成为 /test.html
。并要求 /hello.html
将成为 / _ AUTH /管理/ hello.html的
用户管理
和 /hello.html
其他任何用户。
So request to /
will end up in /_auth/USER/index.html
, request to /test.html
will serve /test.html
. And request to /hello.html
will serve /_auth/admin/hello.html
for user admin
and /hello.html
for any other user.
到直接访问/ _ AUTH /..
被禁止内部
指令。
这篇关于nginx的配置使用相同的URI返回不同的文件,以不同的身份验证的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!