How to configure nginx ingress speed limit
Starting from the business scenario
During the business development process, we have a requirement: the download service provided through filebrowser needs to be speed limited. For example, when users download files through filebrowser, they need to limit the download rate of each user. Extending from this requirement, the download rate for specific users can also be limited.
In order to achieve this business requirement, combined with our current technology stack (k8s nginx ingress), it can be achieved by configuring the corresponding nginx parameters.
What is speed limit?
Speed limit, as the name suggests, is a speed limit.
The rate here can be:
The frequency of a single user accessing resources within a unit time,
can also be The frequency of a single IP accessing resources within a unit of time.
can also be the transmission rate of a specified connection within a unit of time.
Usually, the latter business scenario exists in download speed limit
Why speed limit?
The essence of speed limit is to ensure fairness.
In the case of limited bandwidth resources, try to ensure that each user can be reasonably allocated sufficient bandwidth value. It can also serve more users through speed limiting when bandwidth resources are limited.
In addition, speed limiting can also greatly alleviate the impact of distributed denial-of-service attacks (DDOS).
What are the configurations in the yaml file of nginx ingress?
The speed limit configuration of Nginx ingress can basically be found in the nginx.ingress.kubernetes.io annotation of ingress.
Below, we will interpret the annotations related to speed limit one by one:
##nginx.ingress.kubernetes.io/limit-connections
: single The number of concurrent connections that an IP address can have at the same time. If the number of concurrent connections is exceeded, a 503 error is returned.
nginx.ingress.kubernetes.io/limit-rps
: Limit the number of requests per second for a single IP (limit request per second). If the limit is exceeded, a 503 error is returned. It should be noted that a 503 error does not occur immediately when the value set by the configuration is exceeded. nginx allows the existence of the number of burst requests within a certain time range (number of burst requests = limit-rps * limit-burst-multiplier ). So when will 503 appear? This starts with the current limiting model of nginx. The current limiting model of nginx is a queue (refer to the queue model of the thread pool). The max number of connections for current limiting = the queue processing capability and the length of the queue, that is, max-connections-per-second = limit-rps limit-rps*limit- burst-multiplier.
nginx.ingress.kubernetes.io/limit-rpm
: Same as limit-rps, but limit-rpm has a higher priority than limit-rps, that is When limit-rpm and limit-rps are set at the same time, limit-rpm shall prevail. However, when limit-connections are also set, then limit-connections have the highest priority.
nginx.ingress.kubernetes.io/limit-burst-multiplier
: The coefficient of the burst request size, mainly used to define the queue length of the connection, the default is 5
nginx.ingress.kubernetes.io/limit-rate-after
: The limit-rate is executed after the amount of traffic is exceeded, the unit is KB
nginx.ingress.kubernetes.io/limit-rate
: The rate limit value of a single connection per second, in KB.
nginx.ingress.kubernetes.io/limit-whitelist
: Set an IP whitelist. IPs in the whitelist will not be speed limited and support CIDR. , multiple IPs can be separated by commas.
Note
- When limit-connections, limit-rps, and limit-rpm are set at the same time, the priority Yes limit-connections>limit-rpm>limit-rps
- The prerequisite for limit-rate-after and limit-rate to take effect is
nginx.ingress.kubernetes.io/proxy- buffering: "on"
- The IP mentioned above needs to be distinguished from the SLB or the real user IP to obtain the real user IP
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: annotations: ... nginx.ingress.kubernetes.io/proxy-buffering: on nginx.ingress.kubernetes.io/limit-rate: 10 # 单位是KB name: xxx namespace: yyy spec: ingressClassName: nginx rules: ...
The above is the detailed content of How to configure nginx ingress speed limit. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

ArtGPT
AI image generator for creative art from text prompts.

Stock Market GPT
AI powered investment research for smarter decisions

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

1. The first choice for the Laravel MySQL Vue/React combination in the PHP development question and answer community is the first choice for Laravel MySQL Vue/React combination, due to its maturity in the ecosystem and high development efficiency; 2. High performance requires dependence on cache (Redis), database optimization, CDN and asynchronous queues; 3. Security must be done with input filtering, CSRF protection, HTTPS, password encryption and permission control; 4. Money optional advertising, member subscription, rewards, commissions, knowledge payment and other models, the core is to match community tone and user needs.

When Nginx experiences a "Toomyopenfiles" error, it is usually because the system or process has reached the file descriptor limit. Solutions include: 1. Increase the soft and hard limits of Linux system, set the relevant parameters of nginx or run users in /etc/security/limits.conf; 2. Adjust the worker_connections value of Nginx to adapt to expected traffic and ensure the overloaded configuration; 3. Increase the upper limit of system-level file descriptors fs.file-max, edit /etc/sysctl.conf and apply changes; 4. Optimize log and resource usage, and reduce unnecessary file handle usage, such as using open_l

The core role of Homebrew in the construction of Mac environment is to simplify software installation and management. 1. Homebrew automatically handles dependencies and encapsulates complex compilation and installation processes into simple commands; 2. Provides a unified software package ecosystem to ensure the standardization of software installation location and configuration; 3. Integrates service management functions, and can easily start and stop services through brewservices; 4. Convenient software upgrade and maintenance, and improves system security and functionality.

To solve the problem of inconsistency between PHP environment and production, the core is to use Kubernetes' containerization and orchestration capabilities to achieve environmental consistency. The specific steps are as follows: 1. Build a unified Docker image, including all PHP versions, extensions, dependencies and web server configurations to ensure that the same image is used in development and production; 2. Use Kubernetes' ConfigMap and Secret to manage non-sensitive and sensitive configurations, and achieve flexible switching of different environment configurations through volume mounts or environment variable injection; 3. Ensure application behavior consistency through unified Kubernetes deployment definition files (such as Deployment and Service) and include in version control; 4.

In NGINX configuration, the @ symbols within the location block are used to define named locations. These are internally used endpoints and cannot be matched directly by the client request. They are usually called via the error_page, try_files, or rewrite directives. 1. The naming location starts with @. For example, location@notfound will not respond to direct requests, but trigger from other configuration parts; 2. It is often used for custom error handling, internal routing and backend agent backing; 3. For example, combined with try_files, forwarding to @backend when static files do not exist; 4. Notes include: not directly accessed, avoiding naming conflicts, and using descriptive names. Named locations can include

1. The mainstream frameworks of PHP e-commerce backend include Laravel (fast development, strong ecology), Symfony (enterprise-level, stable structure), Yii (excellent performance, suitable for standardized modules); 2. The technology stack needs to be equipped with MySQL Redis cache, RabbitMQ/Kafka message queue, Nginx PHP-FPM, and front-end separation is considered; 3. High concurrency architecture should be layered and modular, database read and write separation/distributed database, accelerated with cache and CDN, asynchronous processing of tasks, sharing of load balancing and Session, gradually microservice, and establish a monitoring and alarm system; 4. Multiple monetization paths include product price difference or platform commission, site advertising, SaaS subscription, customized development and plug-in market, API connection

After modifying the Nginx configuration, you should first test the syntax and then reload the service. 1. Use nginx-t to check the configuration file syntax. If the prompt "syntaxisok" and "testissuccessful" are prompted, it means that it is correct; if there is an error, the specific problem line will be displayed. 2. If the configuration file permissions are high, you need to use sudonginx-t to execute. 3. Confirm that the test is actually loaded. You can specify the path through nginx-t-c/path/to/your/nginx.conf, or view the configuration file used by the main process through ps-ef|grepnginx. 4. After the test is passed, execute sudonginx-sreload overload service to make the new configuration take effect

To configure the PHP environment to support MongoDB, the core step is to install and enable the PHP driver of MongoDB to enable the PHP application to communicate with the MongoDB database. 1. Install MongoDBPHP driver, it is recommended to use PECL to install. If there is no PECL, you need to first install the PHP development package and related compilation tools; 2. Edit the php.ini file and add extension=mongodb.so (or .dll) to enable the extension; 3. Restart the web server or PHP-FPM service to make the configuration take effect; 4. Verify whether the extension is loaded successfully through phpinfo() or php-m. Frequently asked questions include missing PECL commands, compilation errors, php.ini
