Nginx优化
隐藏版本号
修改配置文件
在生产环境中,需要隐藏版本号,以避免泄露Nginx版本,通过版本知道该版本的缺陷,并进行攻击
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
[root@localhost ~]# curl -I http://192.168.20.11 #查看版本号
HTTP/1.1 200 OK
Server: nginx //版本号已经隐藏
Date: Wed, 23 Jun 2021 04:21:28 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 23
Last-Modified: Wed, 23 Jun 2021 03:22:59 GMT
Connection: keep-alive
ETag: "60d2a913-17"
Accept-Ranges: bytes
修改源码
[root@localhost ~]# vim /opt/nginx-1.15.9/src/core/nginx.h //进入放源码的位置,基本所有配置文件看到src就是源码的位置
[root@localhost ~]# cd /opt/nginx-1.15.9/ //重新编译安装
[root@localhost nginx-1.15.9]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@localhost nginx-1.15.9]# make && make install
[root@localhost nginx-1.15.9]# vim /usr/local/nginx/conf/nginx.conf
[root@localhost nginx-1.15.9]# systemctl restart nginx.service
[root@localhost nginx-1.15.9]# curl -I http://192.168.20.11
HTTP/1.1 200 OK
Server: Apache/1.1.1 //可以看到已经给攻击者看到了你想给他们看到的版本和服务
Date: Thu, 24 Jun 2021 15:39:12 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Thu, 24 Jun 2021 15:15:31 GMT
Connection: keep-alive
ETag: "60d4a193-264"
Accept-Ranges: bytes
网页中也一样
设置用户和组
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
[root@localhost ~]# ps aux | grep nginx
root 81432 0.0 0.0 20544 628 ? Ss 12:27 0:00 nginx: master process /usr/localnginx/sbin/nginx
nginx 81433 0.0 0.0 22992 1396 ? S 12:27 0:00 nginx: worker process
root 81435 0.0 0.0 112724 980 pts/1 S+ 12:27 0:00 grep --color=auto nginx
设置缓存时间
设置缓存时间,避免重复请求,加速访问速度(一般用于静态网页)
[root@localhost nginx-1.15.9]# vim /usr/local/nginx/conf/nginx.conf
[root@localhost nginx-1.15.9]# cd /usr/local/nginx/html //到网页文件目录下
[root@localhost html]# vim index.html
[root@localhost html]# systemctl restart nginx
[root@localhost html]# curl -I http://192.168.20.11/jerry.jpg
HTTP/1.1 200 OK
Server: Apache/1.1.1
Date: Thu, 24 Jun 2021 16:28:13 GMT
Content-Type: image/jpeg
Content-Length: 20722
Last-Modified: Thu, 27 May 2021 02:16:15 GMT
Connection: keep-alive
ETag: "60af00ef-50f2"
Expires: Fri, 25 Jun 2021 16:28:13 GMT
Cache-Control: max-age=86400 //缓存天数为一天,这里以秒为单位
Accept-Ranges: bytes
日志分割
#!/bin/bash
#Filename:rzfg.sh
d=$(date -d "-1 day" "+%Y%m%d") #显示一天前的时间;+:表示连接符
logs_path="/var/log/nginx" ##日志存放的位置
pid_path="/usr/local/nginx/logs/nginx.pid" ##nginx的进程文件存放位置
[ -d $logs_path ] || mkdir -p $logs_path ##判断是否目录,不就创建目录
mv /usr/local/nginx/logs/access.log ${logs_path}/test.com-access.log-$d ##移动访问日志到log_path,并命名test.com;脚本执行后创建日志进行命令(时间戳的方式来标记唯一性)
kill -s HUP $(cat $pid_path) ##给nginx发送信号进行reload,从而生成新的access日志
find $logs_path -mtime +30 | xargs rm -rf ##删除30天之前的日志
[root@localhost ~]# ./rzfg.sh
[root@localhost ~]# ls /var/log/nginx/
test.com-access.log-20210624
[root@localhost ~]# date -s 20210621
2021年 06月 21日 星期一 00:00:00 CST
[root@localhost ~]# ls /var/log/nginx/
test.com-access.log-20210620 test.com-access.log-20210624
crontab -e ##周期任务定时执行
连接超时
避免为同一个客户端长时间占用连接,控制连接访问时间
[root@localhost nginx]# vim /usr/local/nginx/conf/nginx.conf
更改进程数
在高并发时,需要启动跟多的Nginx进程保证响应,避免造成阻塞
[root@localhost nginx]# cat /proc/cpuinfo | grep -c "physical"
8
[root@localhost nginx]# ps aux | grep nginx
root 81551 0.0 0.0 20596 1500 ? Ss 6月20 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 81762 0.0 0.0 23108 1424 ? S 00:00 0:00 nginx: worker process
root 81976 0.0 0.0 112724 984 pts/1 S+ 00:18 0:00 grep --color=auto nginx
[root@localhost nginx]# vim /usr/local/nginx/conf/nginx.conf
- 4为cpu核数
- 设置每个进程由不同cpu处理,两个人干活肯定不会有四个人干活快
[root@localhost nginx]# systemctl restart nginx
[root@localhost html]# ps aux | grep nginx
root 82384 0.0 0.0 20556 624 ? Ss 00:43 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 82385 0.0 0.0 23092 1396 ? S 00:43 0:00 nginx: worker process
nginx 82386 0.0 0.0 23092 1396 ? S 00:43 0:00 nginx: worker process
nginx 82387 0.0 0.0 23092 1396 ? S 00:43 0:00 nginx: worker process
nginx 82388 0.0 0.0 23092 1396 ? S 00:43 0:00 nginx: worker process
root 82391 0.0 0.0 112724 980 pts/1 S+ 00:43 0:00 grep --color=auto nginx
网页压缩功能
[root@localhost nginx]# vim /usr/local/nginx/conf/nginx.conf
gzip on; #开启gzip压缩功能
gzip_min_length 1k; #压缩阈值
gzip_buffers 4 16k; #buffer 大小为4个16k缓冲区大小
gzip_http_version 1.1; #压缩版本
gzip_comp_level 6; #压缩比率,最小为1,处理速度快,传输速度慢,9最大压缩比,处理速度慢,传输速度快(建议5-6)
gzip_types text/plain application/x-javascript text/css image/jpg image/jpeg image/png image/gif application/xml text/javascript application/x-httpd-php application/javascript application/json;
gzip_disable "MSIE [1-6]\."; #配置禁用gzip条件,支持正则,表示ie6以下不启用gzip
gzip_vary on; #支持前端缓存服务器存储压缩页面
[root@localhost html]# nginx -t //检测
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost html]# systemctl restart nginx //重新开启服务
fpm优化
nginx的PHP解析功能如果是给FPM处理的,那么为了提高处理速度,可以根据服务器饿内存和服务负载对模块进行参数的调整
[root@localhost php-fpm.d]# cd /usr/local/php/etc/
[root@localhost etc]# vim php-fpm.conf
pid = run/php-fpm.pid
pm = dynamic
pm.max_children=20 #static模式下空闲进程数上限,大于下面的值
pm.start_servers = 5 #动态方式下默认开启的进程数,在最小和最大之间
pm.min_spare_servers = 2 #动态方式下最少空闲进程数
pm.max_spare_servers = 8 #动态方式下最大空闲进程数
盗链
-
虚拟机win10
-
服务端
-
盗链端
以下为验证部分
在盗链端添加映射
[root@localhost ~]# vim /etc/hosts
在服务端添加映射
[root@localhost ~]# vim /etc/hosts
win10端添加映射
第一次修改需要修改安全里面所有权限
win10访问服务端
在盗链端配置
[root@localhost html]# vim index.html
用win10访问盗链端也可以访问
防盗链
在服务端
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
[root@www html]# ls
50x.html error.png index.html jerry.jpg
[root@www html]# systemctl restart nginx
再访问www.daolian.com,需要清理缓存,发现成功
总结
-
安全第一
-
定时任务模板
{minute} {hour} {day-of-month} {month} {day-of-week} {full-path-to-shell-script}
o minute: 区间为 0 – 59
o hour: 区间为0 – 23
o day-of-month: 区间为0 – 31
o month: 区间为1 – 12. 1 是1月. 12是12月.
o Day-of-week: 区间为0 – 7. 周日可以是0或7.