X-Accel-Redirect dropping custom headers

1,346 views
Skip to first unread message

peter....@gmail.com

unread,
Aug 8, 2013, 12:11:50 PM8/8/13
to openre...@googlegroups.com
Hello,

I'm having trouble passing custom headers over X-Accel-Redirect. 

Eg. I set this upstream:

   ...redirect 302 etc.
    X-Accel-Redirect: /internal/stuff
    X-Foo: foobar

And have this in nginx.conf:

  location ~ '/internal/stuff' {
      internal;
      proxy_set_header X-Foo $http_x_foo;
      proxy_pass http://otherupstream;
      break;
  }

...but in otherupstream I don't get an X-Foo header. 

AFAICT the X-Accel Module doesn't pass on custom http headers it receives from upstream -- is that the case? Or am I doing it wrong? 

cheers,
peter.

Matt Gray

unread,
Aug 8, 2013, 3:22:51 PM8/8/13
to openre...@googlegroups.com
Hello Peter,

I think the variable name needs to begin $upstream. 

In your example $upstream_http_x_foo is the x-foo response header from the upstream sending x-accel-redirect.

http://wiki.nginx.org/HttpUpstreamModule#.24upstream_http_.24HEADER


hth,

Matt
--
You received this message because you are subscribed to the Google Groups "openresty-en" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openresty-en...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 


--
Matt Gray

API Developer





--
This email, including attachments, is private and confidential. If you have
received this email in error please notify the sender and delete it from
your system. Emails are not secure and may contain viruses. No liability
can be accepted for viruses that might be transferred by this email or any
attachment. Any unauthorised copying of this message or unauthorised
distribution and publication of the information contained herein are
prohibited. 7digital Limited. Registered office: 69 Wilson Street, London EC2A 2BB. Registered in
England and Wales. Registered No. 04843573.

peter sabaini

unread,
Aug 9, 2013, 5:05:47 AM8/9/13
to openre...@googlegroups.com
Hey Matt,

thanks for the suggestion. I tested that --  I can see other upstream* variables being passed along, but the X-Foo test header I set from the X-Accel-Redirect responder doesn't show... 

Also, I found this thread which seems to suggest that the X-Accel mechanism has been limited on purpose (it's quite old though): https://www.ruby-forum.com/topic/133840 

cheers,
peter.
Matt
To unsubscribe from this group and stop receiving emails from it, send an email to openresty-en+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

Yichun Zhang (agentzh)

unread,
Aug 9, 2013, 2:50:17 PM8/9/13
to openresty-en
Hello!

On Fri, Aug 9, 2013 at 2:05 AM, peter sabaini wrote:
> thanks for the suggestion. I tested that -- I can see other upstream* variables being passed along, but the X-Foo test header I set from the X-Accel-Redirect responder doesn't show...
>

The $upstream_http_HEADER variable should work with X-Accel-Redirect,
but your configuration does not work because you configured another
proxy_pass in location ~ '/internal/stuff', which overrides the
$upstream_http_HEADER variable values generated in your original
location (also with ngx_proxy). To make this work, you need a
"intermediate location" that saves the original value of
$upstream_http_HEADER. Below is a complete example that demonstrates
this and it has been tested on my side:

location = /a1 {
proxy_pass http://127.0.0.1:$server_port/a2;
}

location = /a2 {
more_set_headers 'X-Accel-Redirect: /internal/stuff';
more_set_headers 'X-Foo: foobar';
echo ok;
}

location ~ '/internal/stuff' {
internal;
set $x_foo $upstream_http_x_foo;
rewrite ^ /a3;
}

location = /a3 {
proxy_set_header X-Foo $x_foo;
proxy_pass http://127.0.0.1:$server_port/a4;
}

location = /a4 {
echo "X-Foo: $http_x_foo";
}

And accessing /a1 gives

$ curl localhost:1985/a1
X-Foo: foobar

So we're getting the expected value :) Please note that how we saved
the value of $upstream_http_x_foo into our own user variable $x_foo in
location ~ '/internal/stuff' and later use it in location = /a3.

To conclude, the $upstream_http_HEADER variable is one of those
special built-in Nginx variables that are context-sensitive. Ensure
that you are not using a new Nginx upstream module configuration when
you still need the info in a previous nginx upstream module
configuration.

Best regards,
-agentzh

peter sabaini

unread,
Aug 11, 2013, 1:48:25 AM8/11/13
to openre...@googlegroups.com
Thanks, this did it!
Reply all
Reply to author
Forward
0 new messages