nginx中限流模块ngx_http_limit_req_module里的limit_req_zone指令和limit_zone限流指令以及服务器运维人员基本的服务器优化命令及相关参数意义

一、nginx中限流模块ngx_http_limit_req_module里的limit_req_zone指令和limit_zone限流指令

1. ngx_http_limit_req_module里的limit_req_zone指令

    通常我们会在程序里进行防盗刷,实际这些可以在nginx层就解决,nginx里有一个模板:ngx_http_limit_req_module模块,其中有一个指令limit_req_zone可以进行限流访问,以防止同一用户(以IP统计,所以CDN转发过来的要注意要把客户端IP转发过来)恶意攻击刷爆服务器。ngx_http_limit_req_module模块是nginx默认安装的,所以只要直接配置即可 publish:November 15, 2018 -Thursday。

#http模块limit_req_zone配置使用格式, 同时还需要配置server
#$binary_remote_addr是$remote_addr(客户端IP)的二进制格式,固定占用4个字节。下面的配置表明:限流功能区域名为one,占用空间10m,请求频率不能超过每秒50次。
limit_req_zone $binary_remote_addr zone=req_one:10m rate=50r/s;
#这个功能同样可以用来针对网络爬虫,用limit模块对网络爬虫进行限流。
#limit_req_zone $anti_spider zone=anti_spider:10m rate=10r/s;

#server模块
location / {
    #缓存区队列burst=10个,nodelay不延期,即每秒最多可处理rate+burst个.同时处理rate个。
    #limit_req zone=anti_spider burst=10 nodelay;
    limit_req zone=one burst=5 nodelay;
    if ($http_user_agent ~* "spider|Googlebot") {
        set $anti_spider $http_user_agent;
    }
}

2. http模块配置参数解释:

$binary_remote_addr :表示通过remote_addr这个标识来做限制,“binary_”的目的是缩写内存占用量,是限制同一客户端ip地址。
zone=one:10m:表示生成一个大小为10M,名字为one的内存区域,用来存储访问的频次信息
rate=50r/s:表示允许相同标识的客户端的访问频次,这里限制的是每秒50次,即每秒只处理50个请求,还可以有30r/m的,即限制每2秒访问一次/每分钟30次。

3. server模块配置参数解释:

zone=one :设置使用哪个配置区域来做限制,与上面limit_req_zone 里的name对应

burst=5:这个配置的意思是设置一个大小为5的缓冲区当有大量请求过来时,超过了访问频次限制的请求可以先放到这个缓冲区内等待,但是这个等待区里的位置只有5个,超过的请求会直接报503的错误然后返回。

nodelay:如果设置,会在瞬时提供处理(burst + rate)个请求的能力,请求超过(burst + rate)的时候就会直接返回503,永远不存在请求需要等待的情况。(这里的rate的单位是:r/s)如果没有设置,则所有请求会依次等待排队。

limit_req zone=req_zone;严格依照在limti_req_zone中配置的rate来处理请求,超过rate处理能力范围的,直接drop,表现为对收到的请求无延时

limit_req zone=req_zone burst=5;依照在limti_req_zone中配置的rate来处理请求,同时设置了一个大小为5的缓冲队列,在缓冲队列中的请求会等待慢慢处理,超过了burst缓冲队列长度和rate处理能力的请求被直接丢弃,表现为对收到的请求有延时。

limit_req zone=req_zone burst=5 nodelay;依照在limti_req_zone中配置的rate来处理请求,同时设置了一个大小为5的缓冲队列,当请求到来时,会爆发出一个峰值处理能力,对于峰值处理数量之外的请求,直接丢弃,在完成峰值请求之后,缓冲队列不能再放入请求。如果rate=10r/s,且这段时间内没有请求再到来,则每6 s 缓冲队列就能回复一个缓冲请求的能力,直到回复到能缓冲5个请求位置。

        能理解就行,具体的测试数据可查看链接:https://blog.csdn.net/hellow__world/article/details/78658041

        nginx上限制连接的模块里有两个指令,一个是上面的limie_req_zone,另一个是limit_zone,两个都可以限制连接,但有些不同。nginx官网上的解释:

limit_req_zone:Limit frequency of connections from a client. 
This module allows you to limit the number of requests for a given session, or as a special case, with one address.Restriction done using leaky bucket. 
limit_zone:Limit simultaneous connections from a client. 
This module makes it possible to limit the number of simultaneous connections for the assigned session or as a special case, from one address. 

    可以这样简单理解,lit_req_zone的功能是通过令牌桶原理来限制用户的连接频率,(限制连接的次数),而limit_zone功能是限制一个客户端的并发连接数。(限制连接的个数)前面是限制连接频率,后面是限制并发连接数数。能理解就行,具体的测试可查看链接:https://www.centos.bz/2017/11/nginx-%E7%9A%84%E9%99%90%E5%88%B6%E8%BF%9E%E6%8E%A5%E6%A8%A1%E5%9D%97limit_zone%E4%B8%8Elimit_req_zone/

二、 服务器运维人员基本的服务器优化命令及相关参数意义

    以下是一般公司运维部门进行服务器初始化后的服务器优化集合,认为比较有用,留着备用吧 publish:November 21, 2018 -Wednesday:

#限定root用户不能通过ssh的登录方式
sudo sed -i 's/\#PermitRootLogin yes/PermitRootLogin no/g' /etc/ssh/sshd_config
#停用密码认证的登入方式,只允许密钥认证
sudo sed -i 's/\#PubkeyAuthentication/PubkeyAuthentication/g' /etc/ssh/sshd_config
#可以使用user/.ssh/key文件来验证
sudo sed -i 's/\#AuthorizedKeysFile/AuthorizedKeysFile/g' /etc/ssh/sshd_config
#不要这种GSSAPI认证方式,浪费时间
sudo sed -i 's/^GSSAPIAuthentication yes/GSSAPIAuthentication no/' /etc/ssh/sshd_config
#root用户将被拒绝登录
echo "DenyUsers root" >> /etc/ssh/sshd_config

#set ulimits
#限制用户进程的数量对于linux系统的稳定性是非常重要的
sed -i '$i*               soft    nofile          65535\n*               hard    nofile          65535' /etc/security/limits.conf
#/etc/security/limits.conf 配置文件可限制文件打开数,系统进程等资源,在该文件配置中写的最大用户进程数是受/etc/security/limits.d/90-nproc.conf配置上限影响的。
#一般系统普通用户进程数满了都是修改/etc/security/limits.d/90-nproc.conf文件。
[ondev@B72 ~]$ cat /etc/security/limits.d/90-nproc.conf
# Default limit for number of user's processes to prevent
# accidental fork bombs.
# See rhbz #432903 for reasoning.
*          soft    nproc     65535
root       soft    nproc     unlimited
#设置的就是其它用户和root用户的默认最多进程数
[ondev@B72 ~]$ sed -i 's#1024#65535#' /etc/security/limits.d/90-nproc.conf

#其它服务器优化参数
#set sysctl
cat > /etc/sysctl.conf <<EOF
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1 
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_wmem = 4096 16384 4194304
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 262144
net.core.somaxconn = 262144 
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 262144 
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1 
net.ipv4.tcp_mem = 94500000 915000000 927000000  
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 1200 
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog =  32768
net.core.somaxconn = 32768
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_timestamps = 1
net.ipv4.tcp_tw_recycle = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
#net.ipv4.tcp_tw_len = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800
#net.ipv4.tcp_fin_timeout = 30
#net.ipv4.tcp_keepalive_time = 120
net.ipv4.ip_local_port_range = 1024  65535
net.ipv6.conf.all.disable_ipv6=1
net.ipv6.conf.default.disable_ipv6=1
EOF

三、附存 MySQL中可操作的权限列表及意义 

附表:在MySQL中的操作权限:

ALTER	        #Allows use of ALTER TABLE.
ALTER ROUTINE	#Alters or drops stored routines(存储过程/存储程序).
CREATE	        #Allows use of CREATE TABLE.
CREATE ROUTINE	#Creates stored routines.
CREATE TEMPORARY TABLE	#Allows use of CREATE TEMPORARY TABLE.
CREATE USER	    #Allows use of CREATE USER, DROP USER, RENAME USER, and REVOKE ALL PRIVILEGES.
CREATE VIEW	    #Allows use of CREATE VIEW.
DELETE	        #Allows use of DELETE.
DROP	        #Allows use of DROP TABLE.
EXECUTE	        #Allows the user to run stored routines.
FILE	        #Allows use of SELECT... INTO OUTFILE and LOAD DATA INFILE.
INDEX	        #Allows use of CREATE INDEX and DROP INDEX.
INSERT	        #Allows use of INSERT.
LOCK TABLES	    #Allows use of LOCK TABLES on tables for which the user also has SELECT privileges.
PROCESS	        #Allows use of SHOW FULL PROCESSLIST.
RELOAD	        #Allows use of FLUSH.
REPLICATION	    #Allows the user to ask where slave or master
CLIENT	        #servers are.
REPLICATION SLAVE	#Needed for replication slaves.
SELECT	        #Allows use of SELECT.
SHOW DATABASES	#Allows use of SHOW DATABASES.
SHOW VIEW	    #Allows use of SHOW CREATE VIEW.
SHUTDOWN	    #Allows use of mysqladmin shutdown.
SUPER	        #Allows use of CHANGE MASTER, KILL, PURGE MASTER LOGS, and SET GLOBAL SQL statements. Allows mysqladmin debug command. Allows one extra connection to be made if maximum connections are reached.
UPDATE	        #Allows use of UPDATE.
USAGE	        #Allows connection without any specific privileges.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林戈的IT生涯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值