如何使用 nginx 代理到需要身份验证的主机?

bwi*_*zzy 55 nginx proxy

如何设置一个 nginx proxy_pass 指令,该指令还将包含发送到代理主机的 HTTP 基本身份验证信息?

这是我需要代理到的 URL 示例:

http://username:password@192.168.0.5/export?uuid=1234567890
Run Code Online (Sandbox Code Playgroud)

最终目标是允许一台服务器在不暴露代理服务器的 URI 的情况下,允许另一台服务器(我们代理的服务器)显示文件。通过遵循此处找到的 Nginx 配置,我现在可以正确运行 90%:

http://kovyrin.net/2010/07/24/nginx-fu-x-accel-redirect-remote/

我只需要添加 HTTP Basic 身份验证以发送到代理服务器

小智 66

不久前我写了一篇关于这个的文章。在此处查看详细信息:

http://shairosenfeld.blogspot.com/2011/03/authorization-header-in-nginx-for.html

例如:

 location / {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://6.6.6.6:80;
    proxy_set_header Authorization "Basic a2luZzppc25ha2Vk";
 }
Run Code Online (Sandbox Code Playgroud)

"a2luZzppc25ha2Vk" 是 "king:isnaked" base64 编码,因此适用于

http://king:isnaked@6.6.6.6

请随时查看博客文章以获取更多详细信息。

  • 您的解决方案不够灵活。动态编码用户名:密码可能非常有用。首先,nginx 必须从 URL 解析 username:password,其次,nginx 必须对这些数据进行编码并设置在适当的标头中。我不想对编码的凭据进行硬编码。 (7认同)
  • 链接坏了 (2认同)

bwi*_*zzy 26

我用 alvosu 的回答得到了这个,但我必须在 base64 字符串的引用中输入“Basic”这个词,所以它看起来像这样:

proxy_set_header Authorization "Basic dGVzdHN0cmluZw==";
Run Code Online (Sandbox Code Playgroud)

  • 您知道如何使用 nginx 即时对用户名:密码进行编码吗?硬编码凭据不灵活,因为我想使用他在 URL 中指定的凭据对用户进行身份验证。 (2认同)

alv*_*osu 5

proxy_set_header Authorization "USER_AND_PASS"
Run Code Online (Sandbox Code Playgroud)

哪里USER_AND_PASS = base64(user:pass)

  • 缺少关键字“基本”。 (5认同)

小智 5

删除 nginx 转发的授权标头proxy_set_header Authorization "";

我将 nginx 配置为进行基本身份验证,但Authorization标头在指令中传递proxy_pass,并且接收端无法处理令牌。

# Basic Auth
auth_basic "Private Stuff";
auth_basic_user_file /etc/nginx/.htpasswd;

location /server {
    proxy_pass http://172.31.31.140:9090;
    proxy_set_header Authorization "";
}
Run Code Online (Sandbox Code Playgroud)

(具体到我的情况,返回了这个错误Reason: No AuthenticationProvider found for org.springframework.security.authentication.UsernamePasswordAuthenticationToken