nginx - 如何为 nginx 入口中的特定位置添加 limit_req 区域

标签 nginx kubernetes kubernetes-ingress nginx-ingress rate-limiting

我有一个类似于下面的入口

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: staging-ingress-rules-login
  annotations:
    kubernetes.io/ingress.class: 'nginx'
    nginx.ingress.kubernetes.io/proxy-body-size: '0'
spec:
  rules:
  - host: staging.mysite.com
    http:
      paths:
      - path: /
        backend:
          serviceName: login
          servicePort: 80
      - path: /login/info
        backend:
          serviceName: login
          servicePort: 80

nginx.conf 是这样的

server { 
 location / {
   ---------
   ---------
}

location /login/info {
  ---------
  -------
}

} 

我想添加位置/login.info 的速率限制,我尝试了 location-snippet,但它正在/login/info 内创建嵌套位置,并且此 api 的结果给出 404,有什么方法可以做到这一点?

最佳答案

这是一个社区 wiki 答案,请随意编辑和扩展它。

由于我们缺乏有关您的配置的一些详细信息,我将向您解释一般如何处理此问题。

您可以使用以下注释来添加自定义位置 block :

nginx.ingress.kubernetes.io/configuration-snippet: |
  limit_req zone=authentication_ratelimit nodelay;

然后使用 map ,例如:

http-snippets: |
  map $uri $with_limit_req {
    default 0;
    "~*^/authenticate$" 1;
  }
  map $with_limit_req $auth_limit_req_key {
    default '';
    '1'     $binary_remote_addr; # the limit key
  }
  limit_req_zone $auth_limit_req_key zone=authentication_ratelimit:10m rate=1r/s;

Notice that :

Syntax:   limit_req_zone key zone=name:size rate=rate [sync];
Default:  —
Context:  http

Sets parameters for a shared memory zone that will keep states for various keys. In particular, the state stores the current number of excessive requests. The key can contain text, variables, and their combination. Requests with an empty key value are not accounted.

关于nginx - 如何为 nginx 入口中的特定位置添加 limit_req 区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66781995/

相关文章:

opencv - 如何使用我从 nginx 服务器获得的视频数据流?

nginx - 如何在 ubuntu 中使用 nginx 获取域名而不是 ip 地址?

redirect - 如何让 nginx 代理停止重定向到 root?

JAVA 10+ -XX :MaxRAMPercentage flag in Kubernetes container

c# - asp.net core如何根据用户类型实现API限速

Kubernetes 入口和节点故障

docker - NGINX/Docker-Compose/Phoenix

kubernetes - kubectl执行 “groups: cannot find name for group ID 40001”

kubernetes - Istio部署未发生

javascript - 使用 nginx 进行 kubernetes 入口的前端 Web 应用程序中的路径路由