nginx代理传递取决于请求的文件

时间:2015-11-15 02:35:15

标签: nginx

您好我是Nginx的新手,我似乎无法找到任何合适的资源来帮助我。

我的代码

location / {
set $foobar http://test.com;
if ($request_uri = "test") {
    set $foobar http://example.com;
    return 200 $foobar;
    break;
}
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-Proto $scheme;

    proxy_pass $foobar;
    proxy_redirect off;
}

我想要推断的是,如果网址为http://mydomain.ca/test以显示http://example.com,如果不是,则默认为http://test.com

但是,我可能首先得到一些语法错误,因为我得到的只是错误502.

1 个答案:

答案 0 :(得分:0)

我不了解您的配置文件,因此无需使用ifset。以下可能更好:

location / {
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_set_header Host $http_host;
   proxy_set_header X-Forwarded-Proto $scheme;
   proxy_redirect off;

   location /test/ {
      proxy_pass http://test.com;
   }

   proxy_pass http://example.com;
}

但是, 502 Bad Gateway 错误通常意味着您的服务器无法找到远程服务器,可能是由于DNS,路由,防火墙或端口问题。

相关问题