【问题标题】:How can I add api key authentication in nginx proxy?如何在 nginx 代理中添加 api 密钥认证?
【发布时间】:2020-05-20 11:47:24
【问题描述】:

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

我只能找到 NGINX Controller 和 NGINX Plus 的示例,我有点惊讶地发现,对于开源 NGINX 这个非常常见的用例,没有太多示例。

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

【问题讨论】:

标签: docker authentication nginx api-key


【解决方案1】:

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

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

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

  3. 我使用内部位置在需要限制访问的位置使用地图进行密钥验证。

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;
     }
  }

【讨论】:

  • 我也很惊讶。我一直在寻找类似的东西,这基本上是他们在 API Gateway 示例中所做的。我打算这样做,但我首先必须想办法让它调用 AWS API 网关。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-19
  • 1970-01-01
  • 1970-01-01
  • 2016-06-14
  • 2017-06-15
  • 1970-01-01
相关资源
最近更新 更多