【问题标题】:Nginx reverse proxy - Ask for basic auth credentials and pass them on to the target APINginx 反向代理 - 请求基本身份验证凭据并将它们传递给目标 API
【发布时间】:2020-01-09 19:25:29
【问题描述】:

我在 Haskell 中编写了一个 REST API,提供要在浏览器中查看的 HTML,目前我正在尝试使用 Nginx 的反向代理来托管它。 但是,我的后端需要 Nginx 服务器不提供的基本身份验证凭据。

如何配置反向代理,以便在通过浏览器发出 GET 请求时要求提供凭据,但不验证它们并将它们传递到后端? 我已经尝试了关于 stackoverflow、reddit 等的大约六种建议,但还没有找到可行的解决方案。

这是我当前的配置:

server {
    listen 80;
    location / {
        proxy_pass http://127.0.0.1:8000/;

        auth_basic "user-realm";
        proxy_set_header X-Forwarded-User $http_authorization;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
        proxy_pass_header Accept;
        proxy_pass_header Server;
        proxy_http_version 1.1;
        proxy_set_header Authorization $http_authorization;
        #proxy_pass_header Authorization;
        proxy_set_header ns_server-ui yes;
    }
}

我尝试或阅读的一些文章如下:

所有或大部分似乎都集中在如何让 Nginx 接管授权,但我希望它只传递在浏览器中输入的凭据。

【问题讨论】:

    标签: nginx reverse-proxy basic-authentication


    【解决方案1】:

    经过几个小时的尝试,我发现我的配置甚至没有被使用,因为由于某种原因它总是与默认配置匹配。
    我不知道如何解决这个问题,但是当我更改它时,我让代理按预期工作

    location / {
                    # First attempt to serve request as file, then
                    # as directory, then fall back to displaying a 404.
                    try_files $uri $uri/ =404;
            }
    

    到这里

    location / {
                    # First attempt to serve request as file, then
                    # as directory, then fall back to displaying a 404.
                    proxy_pass http://localhost:8000;
            }
    

    Nginx 然后会自动询问凭据并将其传递给后端。 (所以最后我的配置根本没有被使用,因此没有任何东西重新路由到后端)


    希望这可以为将来节省一些搜索时间:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-02
      • 2015-10-23
      • 2013-11-13
      • 2016-03-12
      • 1970-01-01
      • 1970-01-01
      • 2011-06-28
      相关资源
      最近更新 更多