【问题标题】:Nginx Too Many Redirects when using variable in proxy pass, works fine when passing value in directlyNginx Too Many Redirects 在代理传递中使用变量时,直接传递值时工作正常
【发布时间】:2022-01-19 02:54:23
【问题描述】:

在代理传递中使用变量时,Nginx 配置导致重定向过多。这是尝试使用 NGINX 反向代理私有子网中的资源。反向代理在代理传递中直接使用 DNS 记录时工作正常,但在传递变量时会导致过多的重定向。

NGINX 配置:不工作

server {
    listen 443 ssl;

    access_log /var/log/nginx/reverse-access.log;
    error_log /var/log/nginx/reverse-error.log;

    server_name $host;
    rewrite ^/$ https://$host/_dashboards redirect;

    ssl_certificate           /etc/nginx/cert.crt;
    ssl_certificate_key       /etc/nginx/cert.key;

    ssl_session_cache  builtin:1000  shared:SSL:10m;
    ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
    ssl_prefer_server_ciphers on;

    set $domain_endpoint "${elasticsearch_endpoint}";
    set $cognito_endpoint "${cognito_host}";

    location /_dashboards {
        # Forward requests to Dashboards
        proxy_pass https://$domain_endpoint/_dashboards;

        # Handle redirects to Cognito
        proxy_redirect https://$cognito_endpoint https://$host;

        # Update cookie domain and path
        proxy_cookie_domain $domain_endpoint $host;
        proxy_cookie_path / /_dashboards/;

        # Response buffer settings
        proxy_buffer_size 128k;
        proxy_buffers 4 256k;
        proxy_busy_buffers_size 256k;
    }

    location ~ \/(log|sign|fav|forgot|change|saml|oauth2) {
        # Forward requests to Cognito
        proxy_pass https://$cognito_endpoint;
    

        # Handle redirects to Dashboards
        proxy_redirect https://$domain_endpoint https://$host;

        # Update cookie domain
        proxy_cookie_domain $cognito_endpoint $host;
    }
}

工作配置的唯一区别。是不是第一个location /_dashboards下的proxy_pass是这样直接给DNS记录的

location /_dashboards {
    # Forward requests to Dashboards
    proxy_pass https://vpc-aws-blah-blah-blah.com/_dashboards;

在浏览器中查看网络流量时。请求似乎是相同的。它使用 url 参数中的 redirect_uri 向登录端点发出初始 POST 请求。

不同的是,在初始 POST 之后,工作版本发出一个 GET 请求,而非工作版本发出重复的 GET 请求

【问题讨论】:

  • 什么是${elasticsearch_endpoint}${cognito_host}?这些变量是从哪里来的?我也不明白你想用server_name $host; 实现什么。
  • 这些变量是从 Terraform 输入的,我可以通过 SSH 连接到机器上并查看它们是否存在。它们看起来与我将变量直接输入到 nginx 配置中没有什么不同。老实说,我不知道我在用服务器名称做什么,或者我已经将 github.com/aws-samples/opensearch-in-vpc/blob/main/…aws.amazon.com/premiumsupport/knowledge-center/… 的一些部分放在一起
  • 你不能在 nginx 配置中使用环境变量,除非你使用 OpenResty 之类的东西(如果你是,here 是解决方案)。 this ServerFault 线程中描述的一些解决方法。
  • 试试:proxy_pass https://$domain_endpoint;
  • 删除 _dashboards 有效。请将其发布为答案,我会接受。知道为什么会这样吗?它是否卡在仪表板循环中? @RichardSmith

标签: nginx


【解决方案1】:

来自the documentation

如果使用 URI 指定 proxy_pass 指令,那么当 请求被传递给服务器,规范化请求 URI 的一部分 匹配位置被指令中指定的 URI 替换

您在locationproxy_pass 中都有/_dashboards,因此替换不会改变。因此,您可以从 proxy_pass 语句中删除可选的 URI 部分。

另外,在同一个文档中:

在 proxy_pass ... 中使用变量时,如果在 指令,它按原样传递给服务器,替换原来的 请求 URI。

因此,当您开始使用变量时,如果原始请求是 /_dashboards/foo,则上游服务器将只会收到 /_dashboards

最简单的解决方案是从proxy_pass 语句中删除/_dashboards

【讨论】:

    猜你喜欢
    • 2016-10-20
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-05
    • 2018-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多