目录
一、nginx优化
安装nginx
https://blog.csdn.net/weixin_45842014/article/details/109740721
1. 运行用户
创建用户
[root@localhost ~]# useradd tom
修改配置
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
user tom;
[root@localhost ~]# /usr/local/nginx/sbin/nginx
[root@localhost ~]# ps -ef |grep nginx
root 68105 1 0 16:01 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
tom 68106 68105 0 16:01 ? 00:00:00 nginx: worker process
root 68267 46055 0 16:01 pts/0 00:00:00 grep --color=auto nginx
2. worker进程数量优化
这个数量一般和cpu核心数保持一致
获取cpu核心数
[root@localhost ~]# grep -c 'processor' /proc/cpuinfo
1
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes 1;
3. worker处理最大连接数优化
events {
worker_connections 1024;
}
4. 隐藏版本号
查看nginx版本号
[root@localhost ~]# curl -sI 192.168.234.130 | grep -i server
Server: nginx/1.18.0
在http模块中添加: server_tokens off;即可关闭版本号显示
http{
server_tokens off;
}
修改完毕后重新加载nginx并访问测试
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost ~]# curl -sI 192.168.234.130 | grep -i server
Server: nginx
5. 伪装版本号
需要更改源码
[root@localhost ~]# vim nginx-1.18.0/src/core/nginx.h #修改源码文件
#define NGINX_VERSION "2.0" #版本改为2.0
#define NGINX_VER "home/" NGINX_VERSION #名字改为home
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s stop #停止nginx
[root@localhost ~]# cd nginx-1.18.0
[root@localhost nginx-1.18.0]# ./configure && make && make install #从新编译安装即可
需要重启nginx,并且显示版本号,才可以看到修改的版本号
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
访问测试
[root@localhost ~]# curl -sI 192.168.234.130 | grep -i server
Server: home/2.0
二、Nginx防DDos优化和配置
1.脚本防御
使用ab进行压力测试,并通过脚本获取访问日志中访问量前3的IP地址
[root@localhost ~]# ab -c 20 -n 2000 http://192.168.234.130/ #模拟20人发起2000次访问
查询目前nginx日志中访问次数排行前3的IP地址
[root@localhost ~]# awk '{print $1 }' /usr/local/nginx/logs/access.log | sort | uniq -c | sort -nr | head -n 3
2010 192.168.234.130
174 192.168.234.1
将IP地址取出后使用iptables封锁即可!
可以结合iptables 编写ddos防御脚本
#!/bin/bash
ip=($(awk '{print $1 }' /usr/local/nginx/logs/access.log | sort | uniq -c | awk '$1>50{print $2}'))
for i in ${ip[@]}
do
iptables -L -n | grep DROP | grep -q $i
if [ $? -ne 0 ];then
iptables -A INPUT -s $i -j DROP
fi
done
2.通过Nginx配置限制用户的访问速率
限制用户请求的频率,每秒最多访问多少次
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf #修改nginx配置文件
limit_req_zone $binary_remote_addr zone=1804A:10m rate=8r/s; #每秒访问8次
server {
location / {
root html;
index index.html index.htm;
limit_req zone=1804A;
}
}
我们使用limit_req_zon和limit_req来实现的访问速率控制!
limit_req_zone用来定义策略,limit_req引用策略
$binary_remote_addr是用户的IP地址
zone=1804A:10m 定义一个名为1804A的区域,可以理解为策略名称,区域大小为10mb,区域用来存放用户的状态!
rate=8r/s 意思是用户每秒只能访问8次,rate=8r/m 每分钟访问8次,根据需要配置即可
在大量用户访问nginx的时候,会产生很多状态为time wait的进程!这是因为大量的用户请求在断开后
产生的进程!我们可以修改内核参数,让系统快速回收此类进程!以节省资源来处理用户的请求!
nginx快速回收time wait 进程
[root@localhost ~]# vim /etc/sysctl.conf
net.ipv4.tcp_tw_recycle=1
[root@localhost ~]# sysctl -p
net.ipv4.tcp_tw_recycle = 1
3.Nginx配置文件
#启动子进程程序默认用户
#user nobody;
#一个主进程和多个工作进程。工作进程是单进程多线程的,且不需要特殊授权即可运行;>这里定义是工作进程数量
worker_processes 1;
#全局错误日志的位置及日志格式
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#PID位置
#pid logs/nginx.pid;
events {
#每个工作进程最大的并发数 进程数*线程数=并发数
worker_connections 1024;
}
#http服务器设置
http {
#设定mime类型,类型由mime.type文件定义
include mime.types;
default_type application/octet-stream;
#日志格式
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#$remote_addr与$http_x_forwarded_for用以记录客户端IP地址
#$remote_user:用来记录客户端用户名称
#$time_local:用来记录访问时间与时区
#$request:用来记录请求的URL与HTTP协议
#$status :用来记录请求状态,成功是200
#$body_bytes_sent:记录发送给客户端文件主体内容大小
#$http_referer:用来记录从哪个页面链接访问过来的
#$http_user_agent:记录客户浏览器的相关信息
#全局访问日志路径
#access_log logs/access.log main;
#sendfile指令指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,>对于普通应用,必须设为on
sendfile on;
#此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用
#tcp_nopush on;
#长连接超时时间
#keepalive_timeout 0;
keepalive_timeout 65;
#开启压缩
#gzip on;
#配置虚拟主机
server {
#虚拟主机使用的端口
listen 80;
#虚拟主机域名
server_name localhost;
#寻主机支持的字符集
#charset koi8-r;
#虚拟主机的访问日志路径
#access_log logs/host.access.log main;
#定义web根路径
location / {
#根目录路径
root html;
#索引页
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
#根据错误码 返回对应的页面
error_page 500 502 503 504 /50x.html;
#定义页面路径
location = /50x.html {
root html;
}
#定义反向代理服务器 数据服务器是lamp模型
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
#定义PHP为本机服务的模型
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#拒绝apache DR目录及子目录下的.htaccess文件访问
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
#https的配置方案
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}