Nginx x-accel 重定向命名位置 uri

aci*_*fix 2 python nginx falcon x-accel-redirect

我使用 nginx x-accel-redirect 作为外部资源的身份验证前端。

在我的 python 代码中,我将执行以下操作:

/获取资源/

def view(self, req, resp): 
    name = get_name(req.user.id) # authenticates request.
    resp.set_header('X-Accel-Redirect', '/resource/%s/' %name ) 
Run Code Online (Sandbox Code Playgroud)

这也将转发 HTTP 方法,直到 nginx 1.10。从 nginx 1.10 开始,所有 x-accel-redirects 都作为 GET 方法转发。

从这个线程:https : //forum.nginx.org/read.php?2,271372,271380#msg-271380

我知道转发 HTTP 方法的正确方法是使用命名位置。我无法找到有关如何完成此操作的文档。我尝试了以下方法:

def view(self, req, resp): 
    name = get_name(req.user.id) 
    resp.set_header('X-Accel-Redirect', '@resource' ) 
Run Code Online (Sandbox Code Playgroud)

但这会重定向到“@resource /”。

我想重定向到“@resource /name”。

我也在 nginx 论坛上问过这个问题:https ://forum.nginx.org/read.php ?2,271448

但还没有回应。

编辑:

发布 nginx 配置

location /getresource {
   proxy_pass http://127.0.0.1:8000;
}

location /resource {
    internal;
    proxy_pass http://127.0.0.1:8888;
}

location @resource {
    internal;
    proxy_pass http://127.0.0.1:8888;
}
Run Code Online (Sandbox Code Playgroud)

aci*_*fix 6

由于这里没有人回答,我想发布 nginx 论坛的答案以供完成。

https://forum.nginx.org/read.php?2,271448,271549#msg-271549

你好,

在这里你做什么。由于您不能使用 X-Accel-Redirect 来设置不同的位置,您应该使用位置设置其他标头,并在 nginx 配置中执行以下操作:

location @resources {
    set $stored_real_location $upstream_http_x_real_location;
    proxy_pass http://resources-backend$stored_real_location;
}
Run Code Online (Sandbox Code Playgroud)

在上面的示例中,Python 代码应设置以下标头:

X-Accel-Redirect: @resources
X-Real-Location: /some/other/path...
Run Code Online (Sandbox Code Playgroud)