简体   繁体   English

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

[英]Nginx x-accel redirect named location uri

I was using nginx x-accel-redirect as an authentication frontend for an external resource. 我使用nginx x-accel-redirect作为外部资源的身份验证前端。

In my python code I would do the following: 在我的python代码中,我将执行以下操作:

/getresource/ / getresource /

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

This would forward the HTTP method as well until nginx 1.10. 这也将转发HTTP方法,直到nginx 1.10。 Since nginx 1.10 all x-accel-redirects are forwarded as GET methods. 从nginx 1.10开始,所有x-accel-redirects都作为GET方法转发。

From this thread: https://forum.nginx.org/read.php?2,271372,271380#msg-271380 从此线程: https : //forum.nginx.org/read.php?2,271372,271380#msg-271380

I understand that the correct way to forward the HTTP method is to use named location. 我了解转发HTTP方法的正确方法是使用命名位置。 I am unable to find documentation on how this should be done. 我找不到有关应如何执行操作的文档。 I tried the following: 我尝试了以下方法:

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

but this redirects to "@resource /". 但这会重定向到“ @resource /”。

I would like to redirect to "@resource /name". 我想重定向到“ @resource / name”。

I also have asked this question on nginx forums: https://forum.nginx.org/read.php?2,271448 我也在nginx论坛上问过这个问题: https ://forum.nginx.org/read.php ?2,271448

but no response yet. 但没有回应。

Edit: 编辑:

Posting configs for nginx 发布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;
}

Since noone answered here, I would like to post the answer from the nginx forums for completion. 由于此处没有人回答,因此我想在nginx论坛上发布答案以完成任务。

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

Hi, 你好

Here what you do. 在这里你做什么。 As you can not use X-Accel-Redirect to set different location, you should set other header with location and in nginx config do something like this: 由于您不能使用X-Accel-Redirect设置不同的位置,因此应设置具有位置的其他标头,并在nginx config中执行以下操作:

location @resources {
    set $stored_real_location $upstream_http_x_real_location;
    proxy_pass http://resources-backend$stored_real_location;
}

In example above Python code should set the following headers: 在上面的示例中,Python代码应设置以下标头:

X-Accel-Redirect: @resources
X-Real-Location: /some/other/path...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM