pythonnginxfalconx-accel-redirect

Nginx x-accel redirect named location uri


I was using nginx x-accel-redirect as an authentication frontend for an external resource.

In my python code I would do the following:

/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. Since nginx 1.10 all x-accel-redirects are forwarded as GET methods.

From this thread: 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. 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 /".

I would like to redirect to "@resource /name".

I also have asked this question on nginx forums: https://forum.nginx.org/read.php?2,271448

but no response yet.

Edit:

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

Solution

  • Since noone answered here, I would like to post the answer from the nginx forums for completion.

    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:

    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:

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