Coolify logoCoolify

基础认证中间件

对于 标准应用程序基于 Docker Compose 的应用程序/一键服务,配置略有不同。

标准应用程序

traefik.http.middlewares.<random_unique_name>.basicauth.users=test:$2y$12$ci.4U63YX83CwkyUrjqxAucnmi2xXOIlEF6T/KdP9824f1Rf1iyNG
traefik.http.routers.<unique_router_name>.middlewares=<random_unique_name>

在上面的示例中,我们使用 test 作为用户名,test 作为密码。

你很可能已经设置了 traefik.http.middlewares 标签。在这种情况下,你必须将 random_unique_name 中间件追加到现有值中。 例如:

traefik.http.routers.<unique_router_name>.middlewares=gzip,<random_unique_name>

注意:<random_unique_name><unique_router_name> 是占位符。当你将它们添加到自己的标签部分时,需要替换它们。 <random_unique_name> 是中间件的唯一名称,需要你自己定义。<unique_router_name> 是 Coolify 已经为你生成的路由器的唯一名称。

Nginx 简单 Web 容器示例

假设你有一个由 Coolify 生成的 nginx 简单 Web 容器,其 Dockerfile 如下:

FROM nginx:alpine
COPY . /usr/share/nginx/html

Coolify 生成的 容器标签 如下所示:

traefik.enable=true
traefik.http.middlewares.gzip.compress=true
traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https
traefik.http.routers.http-0-wc04wo4ow4scokgsw8wow4s8.entryPoints=http
traefik.http.routers.http-0-wc04wo4ow4scokgsw8wow4s8.middlewares=redirect-to-https
traefik.http.routers.http-0-wc04wo4ow4scokgsw8wow4s8.rule=Host(`nginxsite.mysite.com`) && PathPrefix(`/`)
traefik.http.routers.http-0-wc04wo4ow4scokgsw8wow4s8.service=http-0-wc04wo4ow4scokgsw8wow4s8
traefik.http.routers.https-0-wc04wo4ow4scokgsw8wow4s8.entryPoints=https
traefik.http.routers.https-0-wc04wo4ow4scokgsw8wow4s8.middlewares=gzip
traefik.http.routers.https-0-wc04wo4ow4scokgsw8wow4s8.rule=Host(`nginxsite.mysite.com`) && PathPrefix(`/`)
traefik.http.routers.https-0-wc04wo4ow4scokgsw8wow4s8.service=https-0-wc04wo4ow4scokgsw8wow4s8
traefik.http.routers.https-0-wc04wo4ow4scokgsw8wow4s8.tls.certresolver=letsencrypt
traefik.http.routers.https-0-wc04wo4ow4scokgsw8wow4s8.tls=true
traefik.http.services.http-0-wc04wo4ow4scokgsw8wow4s8.loadbalancer.server.port=80
traefik.http.services.https-0-wc04wo4ow4scokgsw8wow4s8.loadbalancer.server.port=80

如果你想要为该服务添加基础认证,假设你想将认证中间件命名为 mybasicauth,你可以在 第一行 traefik.enable=true 下方添加以下标签:

traefik.http.middlewares.mybasicauth.basicauth.users=test:$2y$12$ci.4U63YX83CwkyUrjqxAucnmi2xXOIlEF6T/KdP9824f1Rf1iyNG

请注意,mybasicauth 已替换了 <random_unique_name> 占位符。换句话说,你将自定义的认证中间件命名为 mybasicauth

然后,你需要将该中间件添加到路由器标签中。由于已经设置了一个或多个中间件,你需要将新中间件追加到现有值中。

例如,你会将当前行

traefik.http.routers.https-0-wc04wo4ow4scokgsw8wow4s8.middlewares=gzip

更新为:

traefik.http.routers.https-0-wc04wo4ow4scokgsw8wow4s8.middlewares=gzip,mybasicauth

请注意,在这种情况下,<unique_router_name> 已被替换为 https-0-wc04wo4ow4scokgsw8wow4s8,这是 Coolify 已经为你生成的路由器的唯一名称。

你的 nginx 简单 Web 容器现已受到基础认证的保护。

Docker Compose 与服务

要将 basicauth 中间件添加到你的服务中,你需要在 docker-compose.yml 文件。:

services:
  nginx-simple-web-container::
    image: 'nginx:alpine'
    ports:
      - '8080:80'
    labels:
      - 'traefik.http.middlewares.<random_unique_name>.basicauth.users=test:$2y$12$ci.4U63YX83CwkyUrjqxAucnmi2xXOIlEF6T/KdP9824f1Rf1iyNG'

你应该将占位符 <random_unique_name> 替换为中间件的唯一名称。例如,你可以将其命名为 mybasicauth,然后 用 mybasicauth 替换占位符。该标签将如下所示:

labels:
  - 'traefik.http.middlewares.mybasicauth.basicauth.users=test:$2y$12$ci.4U63YX83CwkyUrjqxAucnmi2xXOIlEF6T/KdP9824f1Rf1iyNG'

现在,我们已将 basicauth 中间件添加到 nginx-simple-web-container 服务中。

你的 nginx 简单 Web 容器现已受到基础认证的保护,用户名为 test,密码为 test。

注意:在应用基础认证标签时,必须转义特殊字符(如 $、@ 和 ,)以避免解析错误。 例如,如果使用双引号,请将标签值用引号括起来,并在特殊字符前使用反斜杠 ()。

如何生成用户名/密码?

你需要在 basicauth.users 标签中设置用户名和密码。

你可以使用 htpasswd 命令生成:

htpasswd -nbB test test

这将为用户 test(密码为 test)生成密码哈希值。 随后可将 test 替换为你想要的用户名和密码。然后将生成的哈希值替换到上面的 basicauth.users 标签中。

注意:htpasswd 命令在大多数 Linux 发行版中均可用。它是 Debian/Ubuntu 系统中 apache2-utils 软件包的一部分, 可在此处查阅 此处

On this page