Linux----------nginx性能优化

企业web架构优化方案 {此篇借鉴 小马哥 Maximilian Kalbfell}

案例一:
作用:限制网站来源IP访问
最佳应用场景:内部使用网站或者暴露外网网站有恶意攻击IP出现
location / {
root html/blog;
index index.php index.html index.htm;
deny 10.0.0.1; ####deny允许,deny一定要加一个IP,不然会有死循环危害。
allow all; ###allow禁止,all所有
}

案例二: <错误代码页面优化>
作用:当业务页面故障,无法正常处理请求时,会返回一些不太友好的默认错误页面。(如404,500,5002等)。可以把错误页面优雅的显示
最佳应用场景:电商活动促销,直播高峰期等期间,可把网站做好备案方式
server {
listen 80;
server_name www.douyu.com;
location / {
root html/www;
index index.html index.htm;
error_page 400 403 404 405 408 410 411 412 413 414 415 http://bak.douyu.com; #当出现404等错误时,会跳转到指定的URL http://bak.douyu.com页面显示给用户,这个URL一般是企业另外的可用地址(备用亦可)
access_log /usr/local/nginx/logs/bbs_access.log commonlog;
}
}

案例三:
作用:限制web入口处流量和并发数,防止网站崩溃
最佳应用场景:对外暴露的网站,比如电商且促销活动时(双11.双12等),黑客攻击(DDOS,CC),防止不被瞬间高并发打垮。

limit_conn_zone方式(对客户端次数限制,单一IP作用)
http{
limit_conn_zone $binary_remote_addr zone=one:10m;
server
{ ......
limit_conn one 10; ###客户端的并发连接数只能是10个,反之返回503(可对整个service生效,也可对location中对单个location生效)
...... } }

limit_req_zone方式(对客户端速率限制“漏桶”方法,单一IP作用)
http{
limit_req_zone $binary_remote_addr zone=req_one:10m rate=1r/s; ###rate=1r/s的意思是每个地址每秒只能请求一次
server
{ ......
limit_req zone=req_one burst=120; ###令牌桶burst=120块令牌,并且每秒钟只新增1块令牌,120块令牌发完后,多出来的请求就会返回503.。
...... } }

ngx_http_upstream_module(对后端限流)
upstream xxxx{
server 127.0.0.1:8080 max_conns=10; ###后端处理并发数为10,(实际并发数与10可能有较小的差距,但微不足道)
server 127.0.0.1:8081 max_conns=10;
}

附加:
ab工具安装步骤
yum install apr-util yum-utils -y ###安装依赖
yum -y install httpd-tools ###安装ab命令
./ab -c 50 -n 1000 http://127.0.0.1/index.html ###-c 50 每秒并发50 -n 1000 一共发送1000个请求

猜你喜欢

转载自www.cnblogs.com/wangchengshi/p/10966774.html
今日推荐