Docker登录未将基本身份验证标头传递给nginx

时间:2019-04-04 12:44:04

标签: nginx docker-registry

我需要在注册人和网络之间放置一个NGINX代理。这是因为我需要使用auth_request控制身份验证。 auth_request背后的程序可以在不同的数据库中搜索用户。

需要登录的用户可以位于不同的数据库中。程序可以找到用户时给出200答复。这已经可以用于我需要以相同方式保护的其他设置。

## Set a variable to help us decide if we need to add the
## 'Docker-Distribution-Api-Version' header.
## The registry always sets this header.
## In the case of nginx performing auth, the header is unset
## since nginx is auth-ing before proxying.
map $upstream_http_docker_distribution_api_version $docker_distribution_api_version {
    '' 'registry/2.0';
}

server {
    include /etc/nginx/conf.d/certs/server.io/ssl.conf;

    listen *:5000 ssl;
    server_name  docker.server.io;

    access_log  /etc/nginx/conf.d/logs/docker.server.io.access.log  main;

    # disable any limits to avoid HTTP 413 for large image uploads
    client_max_body_size 0;

    # required to avoid HTTP 411: see Issue #1486 (https://github.com/moby/moby/issues/1486)
    chunked_transfer_encoding on;

    location /v2/ {
        # Do not allow connections from docker 1.5 and earlier
        # docker pre-1.6.0 did not properly set the user agent on ping, catch "Go *" user agents
        if ($http_user_agent ~ "^(docker\/1\.(3|4|5(?!\.[0-9]-dev))|Go ).*$" ) {
            return 404;
        }

        # To add basic authentication to v2.
        auth_request /tb_int_auth;
        auth_request_set $user $upstream_http_x_forwarded_user;

        ## If $docker_distribution_api_version is empty, the header is not added.
        ## See the map directive above where this variable is defined.
        add_header 'Docker-Distribution-Api-Version' $docker_distribution_api_version always;


        proxy_pass https://10.0.100.161:31500;
        proxy_ssl_verify off;
        proxy_set_header  Host              $http_host;   # required for docker client's sake
        proxy_set_header  X-Real-IP         $remote_addr; # pass on real client's IP
        proxy_set_header  X-Forwarded-For   $proxy_add_x_forwarded_for;
        proxy_set_header  X-Forwarded-Proto $scheme;
        proxy_read_timeout                  900;
    }

    location = /tb_int_auth {
        internal;

        proxy_pass   https://internal_server/auth.php;
        proxy_ssl_verify    off;

        proxy_pass_request_body off;
        proxy_set_header Content-Length "";
        proxy_set_header X-Original-URI     $request_uri;
        proxy_set_header X-Real-IP          $remote_addr;
        proxy_set_header X-Client-IP        $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
        proxy_set_header Host               $http_host;
    }

    error_page   500 502 503 504  /50x.html;

    location = /50x.html {
        root   /usr/local/openresty/nginx/html;
    }

}

在请求期间收到的标头

"headers": {
      "x-forwarded-proto": "https",
      "QueryString": "",
      "Method": "GET",
      "x-forwarded-for": "127.0.0.1",
      "Uri": "auth.php",
      "x-real-ip": "127.0.0.1",
      "RemoteUser": "",
      "host": "127.0.0.1",
      "PathInfo": "auth.php",
      "connection": "close",
      "x-forwarded-by": "127.0.0.1:443",
      "front-end-https": "on",
      "Principal": "",
      "accept-encoding": "gzip",
      "user-agent": "docker/18.09.0 go/go1.10.4 git-commit/4d60db4 kernel/3.10.0-862.14.4.el7.x86_64 os/linux arch/amd64 UpstreamClient(Docker-Client/18.09.0 \\(linux\\))"
    }

在其他应用程序中,我收到身份验证标头。

0 个答案:

没有答案