5

It seems that Nginx can't support redirects when proxy_pass is set to variable. Is there a way to achieve this functionality e.g. somehow chain servers or other workaround? What is the reason why this is not supported?

.conf is:

proxy_pass $var;
proxy_redirect  default;

Error message I get is:

nginx: [emerg] "proxy_redirect default" cannot be used with "proxy_pass" directive with variables

j0k
  • 22,600
  • 28
  • 79
  • 90
xoopp
  • 91
  • 1
  • 4

2 Answers2

2

You can work around this problem by specifying the full redirect statement instead of using the "default" option.

From the nginx documentation:

The default replacement specified by the default parameter uses the parameters of the location and proxy_pass directives. Hence, the two configurations below are equivalent:

location /one/ {
    proxy_pass     http://upstream:port/two/;
    proxy_redirect default;
location /one/ {
    proxy_pass     http://upstream:port/two/;
    proxy_redirect http://upstream:port/two/ /one/;

So to replace the "default" directive, you use the value of "proxy_pass" as the first argument for "proxy_redirect", and use the value of "location" for the second.

Tobias Cohen
  • 19,893
  • 7
  • 54
  • 51
-2

I once had the same issue and what saved me was Nginx's resolver module

try to add:

    resolver 8.8.8.8;

before the proxy_pass line. It should allow you to work with variables.

Also have a look at a similar question.

Community
  • 1
  • 1
Erez Rabih
  • 15,562
  • 3
  • 47
  • 64