【问题标题】:nginx proxy_pass to dynamic urlnginx proxy_pass 到动态 url
【发布时间】:2018-09-18 17:01:36
【问题描述】:

我有多个后端服务器,我想使用单个 nginx 服务器实例代理所有服务器。每当我添加新的后端服务器时,我都不想更改 nginx.conf。

例如:服务器1:192.168.10.1:8080,服务器2:192.168.10.2:8080等 Nginx 在example.com 上运行。我想通过example.com?ip=192.168.10.1example.com?ip=192.168.10.2等访问Server1

我试过这个配置,但它给出了 500 错误页面。

location / {
   proxy_pass http://$arg_ip:8080;
   proxy_set_header Host      $host;
   proxy_set_header X-Real-IP $remote_addr;
}

我有什么遗漏吗?还有其他方法可以实现吗?

【问题讨论】:

  • 您更改了配置文件但不想更改配置文件?
  • @Rob,更新了我的问题,我的意思是说我不想在添加新实例时更改它
  • 请发布与错误相关的错误日志片段。

标签: nginx reverse-proxy proxypass


【解决方案1】:
server {
    server_name dynamic_host;
    listern 8080;

    #resolver 8.8.8.8;
    #seems you don't need resolver because you use ip address

    location / {
        if ( $arg_address != "" ) {
            proxy_pass $arg_address;

            #proxy_pass $arg_address$uri
            #proxy_pass $arg_address$request_uri
        }

    }
}

三种proxy_pass的区别

  1. $proxy_address
    example.com?address=http://192.168.10.2:8080/ 转到 http://192.168.10.2:8080/

  2. $proxy_address$uri
    example.com/test/path?address=http://192.168.10.2:8080/ 转到
    http://192.168.10.2:8080/test/path

  3. $proxy_address$request_uri
    example.com/test/path?address=http://192.168.10.2:8080/&param=value 转到
    http://192.168.10.2:8080/test/path?address=http://192.168.10.2:8080/&param=value

您可以将参数address 更改为ip,在这种情况下,不要忘记将$arg_address 更改为$arg_ip
参考:
http://nginx.org/en/docs/http/ngx_http_core_module.html#variables

【讨论】:

    猜你喜欢
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    • 2012-06-22
    • 1970-01-01
    • 2019-03-14
    • 1970-01-01
    • 2020-06-09
    相关资源
    最近更新 更多