docker - 如何在 nginx 代理中添加 api key 身份验证?

标签 docker authentication nginx api-key

有没有办法配置https://hub.docker.com/r/jwilder/nginx-proxy/通过硬编码的 API key 添加基本身份验证?

我只能找到 NGINX Controller 和 NGINX Plus 的示例,令我感到惊讶的是,开源 NGINX 的这种非常常见的用例没有太多示例。

NGINX Plus 的例子在这里:https://www.nginx.com/blog/deploying-nginx-plus-as-an-api-gateway-part-1/

最佳答案

这是我在我的 nginx 上所做的,它可能适用于你

  1. 我在客户端使用“X-APIkey:” header :curl -X POST -H "X-APIkey: my-secret-api-key"https://example.com

  2. 我在 nginx.conf 中有一个定义 X-APIkeys 授权值的映射

  3. 我使用内部位置在需要限制访问的位置使用 map 进行 key 验证。

map $http_x_apikey $api_realm {
    default "";
    "my-secret-api-key" "ipfs_id";
    "this-one-too-is-kinda-secret" "ipfs_cmd";
    "however-this-one-is-well-known" "ipfs_api";
    "password" "ipfs_admin";
}
 # API keys verification
  location = /authorize_apikey {
     internal;
     if ($api_realm = "") {
        return 403; # Forbidden
     }
     if ($http_x_apikey = "") {
        return 401; # Unauthorized
     }
     return 204; # OK
  }
location /api/v0/cmd {
     proxy_pass  http://ipfs-api;
     if ($request_method = 'OPTIONS') {
        add_header Access-Control-Allow-Headers "X-APIkey, Authorization";
     }
     satisfy any;
     auth_request /authorize_apikey;
     limit_except OPTIONS {
        auth_basic "Restricted API ($api_realm)";
        auth_basic_user_file /etc/nginx/htpasswd-api-add;
     }
  }

关于docker - 如何在 nginx 代理中添加 api key 身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61912336/

相关文章:

docker - Docker镜像初始文件系统来自哪里?

python - 使用 supervisord 管理 docker 容器的最佳方法

encryption - Kohana 的 auth 模块中最安全的算法是什么?

java - 验证系统

drupal - 登录 block - drupal

java - 如何将 nginx 连接到我的 java 模块

image - 使用Nginx代理外部图像

docker - 安装 davfs2 卷时无法在 docker 容器中打开 fuse 设备

perl - Starman 的最佳 --max-requests 设置是什么?

node.js - 用于 SPA 应用程序的 .NET Core Docker 镜像