使用变量的Nginx proxy_pass和重定向

5

似乎Nginx无法支持当proxy_pass设置为变量时的重定向。有没有什么方法可以实现这个功能,比如通过链式服务器或其他解决方法?为什么不支持这个功能呢?

.conf文件如下:

proxy_pass $var;
proxy_redirect  default;

我收到的错误信息是:
nginx:[emerg]“proxy_redirect default”不能与带有变量的“proxy_pass”指令一起使用。
2个回答

2

您可以通过指定完整的重定向语句来解决这个问题,而不是使用“默认”选项。

根据nginx文档

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/;

因此,要替换“默认”指令,您需要将“proxy_pass”的值用作“proxy_redirect”的第一个参数,并将“location”的值用作第二个参数。


-2

我曾经遇到过同样的问题,而拯救我的是Nginx的解析器模块。

尝试添加以下内容:

    resolver 8.8.8.8;

在 proxy_pass 行之前添加以下代码。这将允许您使用变量。
另外,请查看类似问题的解决方案

这是完全不同的问题,我已经配置了解析器,当然。 - xoopp

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接