Nginx rush purchase current limiting configuration implementation analysis

Nginx rush purchase current limiting configuration implementation analysis

Due to business needs, there are often rush purchases, so it is necessary to limit the flow of errors on the load balancing front end. This article also applies to preventing CC.

  limit_req_zone $server_name zone=sname:10m rate=1r/s; #Limit the server to only one successful access per second #limit_req_zone $binary_remote_addr zone=one:3m rate=1r/s; #Limit IP, only one access per second #limit_req_zone $binary_remote_addr $uri zone=two:3m rate=1r/s; #Limit IP and path without parameters,
    #limit_req_zone $binary_remote_addr $request_uri zone=thre:3m rate=1r/s; #Limit IP and path with parameters server {
    listen 80;
    server_name www.abc.com;
    location / {
        include host/proxy.cnf;
        proxy_pass http://backend;
    }
    location /api/createOrder {
        limit_req zone=sname; #No burst, only one normal request limit_req_status 503; #Set the returned status code to 503
        #limit_req zone=sname burst=5 nodelay; #The maximum concurrency is 5, and real-time processing include host/proxy.cnf;
        proxy_pass http://backend;
        error_page 503 =200 /50x.html; #This is very important. You can set the error status code to 503 and return the result as 200
    }
    location = /50x.html {
        if ($http_user_agent ~* "mobile|android|iPhone|iphone|ios|iOS"){
            #default_type application/json;
            return 200 '{"msg": "The event is too popular, please try again later!","data": {},"code": -1}'; #Set the mobile terminal to return an error message display}
        root html; #If it is a PC, return an HTML page}
    }

Key point: Under normal circumstances, if the current limit is set, the status code returned is 503. For the mobile terminal, even if you return JSON data, the client does not recognize it. At this time, you can cleverly set the status code to 200 through error_page 403 =200 /50x.html;

The above only uses the ngx_limit_req_module, and the ngx_limit_conn_module module can also be used.

Reference: https://gist.github.com/simlegate/75b18359316cc33d8e20

Especially if some consulting websites are targeted by crawlers, the server may be killed by the crawlers (this is the case for small websites)
So what can we do? We can use variables to do

#Global configuration limit_req_zone $spider zone=spider:60m rate=200r/m; #Limit the crawler to run 200 times per minute #In a server limit_req zone=spider burst=5 nodelay;
if ($http_user_agent ~* "spider|bot") {
  set $spider $http_user_agent; #Set variables, and limit the speed if you enter here}

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of Nginx current limiting configuration
  • Detailed explanation of Nginx's connection limit configuration for IP addresses in a network segment
  • Practical Record of Nginx Current Limitation and Configuration Management

<<:  How does Vue implement communication between components?

>>:  Cause Analysis and Solution of I/O Error When Deleting MySQL Table

Recommend

Summary of Mysql exists usage

Introduction EXISTS is used to check whether a su...

Vue implements file upload and download functions

This article example shares the specific code of ...

Implementation code for infinite scrolling with n container elements

Scenario How to correctly render lists up to 1000...

Solution to MySQL garbled code problem under Linux

The project interacts with the server, accesses t...

This article teaches you how to play with CSS border

Border Style The border-style property specifies ...

Example of how to increase swap in CentOS7 system

Preface Swap is a special file (or partition) loc...

About the implementation of JavaScript carousel

Today is another very practical case. Just hearin...

How to handle long data when displaying it in html

When displaying long data in HTML, you can cut off...

Detailed explanation of Django+Vue+Docker to build an interface testing platform

1. Two words at the beginning Hello everyone, my ...

Optimized record of using IN data volume in Mysql

The MySQL version number is 5.7.28. Table A has 3...

Analysis of MySQL cumulative aggregation principle and usage examples

This article uses examples to illustrate the prin...

Videojs+swiper realizes Taobao product details carousel

This article shares the specific code of videojs+...

Multiple solutions for cross-domain reasons in web development

Table of contents Cross-domain reasons JSONP Ngin...

Detailed implementation plan of Vue front-end exporting Excel files

Table of contents 1. Technology Selection 2. Tech...

Summary of seven MySQL JOIN types

Before we begin, we create two tables to demonstr...