网站地图 - XML地图 - 设为首页 - 加入收藏
您的当前位置:主页 > 国内 > 正文

无极

Nginx limit_req 限流:几行配置挡住大部分CC攻击_我的网站

happy together

A |     

     一键部署OpenClaw        服务器CPU莫名飙高,带宽跑满,看日志全是同一个IP一秒打来几十个请求——这不是业务火了,是被CC了。    CABO SAN LUCAS, Mexico -- Residents of Mexico’s Los Cabos resorts rushed to prepare as Hurricane Norma headed toward the southern tip of Mexico’s Baja California Peninsula for an expected landfall Saturday, while in the Atlantic, Tammy grew into a hurricane and threatened to batter the islands of the Lesser Antilles. Businesses in Cabo San Lucas nailed up sheets of plywood over their windows and government personnel hung up banners warning people not to try to cross gullies and stream beds, after Hurricane Norma regained strength and once again became a major storm Friday.Baja California Sur Gov. Victor Manuel Castro urged people to stay at home.“Nobody should leave their house after six, seven in the evening," Castro said. "Nobody should go out.”In the Atlantic, the U.S. National Hurricane Center said Hurricane Tammy had winds of 80 mph (130 kph), and hurricane warnings were issued for the islands of Guadeloupe, Antigua, Barbuda, Montserrat, and St. Kitts and Nevis.The center said Norma had 120 mph (195 kph) maximum sustained winds and was centered about 145 miles (235 kilometers) south of Cabo San Lucas. The Category 3 storm was moving north-northwest at 9 mph (15 kph), and the center said its outer bands of rain were already reaching the Baja peninsula.Hotels in Los Cabos, which are largely frequented by foreign tourists, remained about three-quarters full and there was no major move by visitors to leave, Baja California Sur state tourism secretary Maribel Collins said.With rain already falling in Los Cabos, some flights in and out were canceled Friday, there was no way out anyway. Airports will be closed Saturday, according to the local civil defense office.The local hotel association estimated there were about 40,000 tourists still in Cabo San Lucas and San Jose del Cabo on Friday.A couple from San Diego walked through the largely deserted streets of Cabo San Lucas on Friday. Because their sports fishing tournament had been postponed until next week, they had little choice but to stay. The local port was closed to navigation as a precaution. At the marina in Cabo San Lucas, José Ceseña was hauling out of the water the boat he usually uses to ferry tourists around on tours. With the port closed and a hurricane coming, he said it wasn’t worth risking his craft.Homero Blanco, the state commander of the National Guard, said beaches at the resort had been ordered closed and Guard troops were sent to clear people from the seashore.“This morning there were a few people on the beach when we cleared it,” Blanco said. “We invited them to leave.” The federal government posted 500 marines to the resort to help with storm preparations, and municipal officials said as many as 39 emergency shelters could be opened if needed.A hurricane warning was issued for the southern tip of the Baja California Peninsula, and the forecast track would take a weakened Norma toward the mainland of Mexico’s western Pacific coast as a tropical storm.Norma was expected to weaken somewhat as it neared land, but not as much as originally forecast. In the Atlantic, Hurricane Tammy was about 55 miles (85 kilometers) east of Martinique and 135 miles (220 kilometers) southeast of the Caribbean island of Guadeloupe and was moving west-northwest at 9 mph (15 kph).Tammy was expected to remain at hurricane strength and even strengthen slightly as it moved toward the Lesser Antilles through Saturday passing by Guadeloupe, Antigua and Barbuda. Both Martinique and Guadeloupe are French overseas departments.The hurricane center said in a report that “heavy rainfall and flooding (are) likely over much of the Lesser Antilles.”Hurricane warnings were issued for the islands of Guadeloupe, Antigua, Barbuda, Montserrat, and St. Kitts, Nevis, Anguilla, St. Maarten, St. Martin and St. Barthelemy.___Follow AP’s climate coverage at: https://apnews.com/hub/climate-and-environment。扛CC的第一道闸门不在代码里,在Nginx的限流模块里,配置成本几乎为零。    limit_req的原理是漏桶:你设定一个速率,超速的请求直接返回503或延迟排队。先用limit_req_zone定义一个计数区,再在location里挂上limit_req,两步完事。    

# nginx.conf 的 http 块里定义限流区
    # 以客户端IP为key,10MB内存可存约16万个IP状态    # rate=10r/s 表示平均每秒最多放行10个请求    limit_req_zone $binary_remote_addr zone=req_per_ip:10m rate=10r/s;    # server 或 location 块里应用    server {    location / {    limit_req zone=req_per_ip burst=20 nodelay;    proxy_pass http://127.0.0.1:8080;    }    }    burst=20是缓冲队列长度,配合nodelay表示突发20个请求立即处理、不排队,超出就直接拒。

B | 想对登录、搜索这类昂贵接口管得更严,单独再定义一个rate=2r/s的zone挂上去就行,互不影响。

C |     

# 对登录接口单独限流:每秒2个,突发5个
limit_req_zone $binary_remote_addr zone=login_limit:10m rate=2r/s;

location /login.php {
limit_req zone=login_limit burst=5 nodelay;
# 被限流的请求返回429而不是默认503
limit_req_status 429;
fastcgi_pass 127.0.0.1:9000;
}
    验证很简单:reload之后用ab压一下,ab -n 200 -c 20 https://你的域名/,看返回里有没有429或503;同时tail -f日志确认正常用户没被误伤。

D | 限流阈值别拍脑袋定,先统计自己站点的正常峰值,设成峰值的两倍左右再逐步收紧。限流是防守,如果卡死自己人就本末倒置了。    数据来源:nginx.org官方文档 ngx_http_limit_req_module 模块说明

        
    

申请创业报道,分享创业好点子。点击此处,共同探讨创业新机遇!。

E |

Current article:http://yat.cuozubeishenjingli.cfd/list_yw2n/2079d9t.html

Published on:00:44:45


注:凡本网注明来源非本站的作品,均转载自其它媒体,并不代表本网赞同其观点和对其真实性负责。
本站致力于帮助文章传播,希望能够建立合作关系。
若有任何不适的联系以下方式我们将会在24小时内删除。联系方式:
Copyright © 2018 我的网站 版权所有