隐藏版本号
隐藏Nginx版本号,避免安全漏洞泄露
Nginx隐藏版本号的方法
修改配置文件
修改源码法(IIS 微软web服务器)
使用命令获取nginx响应头
curl -I http://192.168.46.40
方法一:修改配置文件方式
vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
server_tokens off; #添加,关闭版本号
......
}
systemctl restart nginx
curl -I http://192.168.46.40
方法二:修改源码文件,重新编译安装
修改配置文件,将 版本信息伪装为IIS
vim /opt/nginx-1.22.0/src/core/nginx.h
#define NGINX_VERSION "1.1.1" #修改版本号
#define NGINX_VER "IIS" NGINX_VERSION #修改服务器类型
重新编译安装
cd /opt/nginx-1.12.0/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module make -j2 && make install
打开显示版本信息
vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
server_tokens on;
......
}
重新启动服务并测试
systemctl restart nginx
curl -I http://192.168.46.40
修改Nginx用户与组
Nginx运行时进程需要有用户与组的支持,以实现对网站文件读取时进行访问控制
Nginx默认使用nobody用户账号与组账号
修改方法:
编译安装是指定用户与组 修改配置文件指定用户与组
vim /usr/local/nginx/conf/nginx.conf
user nginx nginx; ##取消注释,修改用户为nginx,组为nginx
systemctl restart nginx
ps aux | grep nginx ##主进程由root创建,子进程有nginx创建
配置Nginx网页缓存时间
当Nginx将网页数据返回给客户端后,可设置缓存时间,以方便在日后进行相同内容的请求时直接返回,避免重复请求,加快了访问速度一般针对静态网页设置,对动态网页不设置缓存时间
设置方法:
修改配置文件,在http段,或server段,或者location段加入对特定内容的过期参数
vim /usr/local/nginx/conf/nginx.conf
http {
....
server{
.....
location / {
root html;
index index.html index.htm;
}
location ~.(gif|jpg|jepg|png|ico)$ { #~表示匹配正则表达式,加入新的location,以图片作为缓存对象
root html;
expires 1d; ##指定缓存时间:1天
}
}
}
systemctl restart nginx
传入图片并进行网页文件编辑
[root@localhost html]#cd /usr/local/nginx/html
[root@localhost html]#ls
50x.html index.html meme.jpg
[root@localhost html]#vim index.html
[root@localhost html]#systemctl restart nginx.service
查看浏览器是否有效
日志分割
vim /opt/fenge.sh
#!/bin/bash
#Filename: fenge.sh
last_day=$(date -d "-1 day" "+%Y%m%d") ##定义变量获取前一天的时间
logs_path="/var/log/nginx" ##定义用户专门存放日志的目录
pid_path="/usr/local/nginx/logs/nginx.pid" ##定义获取nginx进程pid文件
######main#######
#判断专门存放日志的目录是否存在,如不存在则创建此目录
if [ !-d $logs_path ];then
mkdir -p $logs_path
fi
#移动并重命名日志文件
mv /usr/local/nginx/logs/access.log $logs_path/access.log-$last_day
mv /usr/local/nginx/logs/error.log $logs_path/error.log-$last_day
#重建新日志文件,以便后续日志正常写入
kill -USR1 $(cat $pid_path)
#删除30天之前的旧日志文件,以释放磁盘空间
#find $logs_path -mtime +30 -exec rm -rf {} ;
#find $logs_path -mtime +30 | xargs rm -rf
find $logs_path -mtime +30 -delete
chmod +x /opt/fenge.th
/opt/fenge.th
ls /var/log/nginx
ls /usr/local/nginx/logs/access.log
##设置周期性任务,每天定时做日志分割
crontab -e
0 1 * * * /opt/fenge.sh
扩展:
在linux操作系统中,每个文件都有很多的时间参数,其中有三个比较主要,分别是ctime,atime,mtime
ctime(status time):当修改文件的权限或者属性的时候,就会更新这个时间,ctime并不是createtime,更像是change time,只有当更新文件的属性或者权限的时候才会更新这个时间,但是更改内容的话是不会更新这个时间
atime(accesstime):当使用这个文件的时候就会更新这个时间
mtime(modification time):当修改文件的内容数据的时候,就会更新这个时间,而更改权限或者属性,mtime不会改变,这就是和ctime的区别
连接超时
HTTP有一个KeepAlive模式,它告诉web服务器在处理完一个请求后保持这个TCP连接的打开状态。若接收到来自客户端的其它请求,服务端会利用这个未被关闭的连接,而不需要再建立一个连接
KeepAlive 在一段时间内保持打开状态,它们会在这段时间内占用资源。占用过多就会影响性能
步骤
vim /usr/local/nginx/conf/nginx.conf
http {
.....
keepalive_timeout 60 50;
client_header_timeout 80;
client_body_timeout 80;
.....
}
systemctl restart nginx
keepalive_timeout 指定KeepAlive的超时时间(timeout)。指定每个TCP连接最多可以保持多长时间,服务器将会在这个时间后关闭连接。 Nginx的默认值是65秒,有些浏览器最多只保持 60 秒,所以可以设定为 60 秒。若将它设置为0,就禁止了keepalive 连接。 第二个参数(可选的)指定了在响应头Keep-Alive:timeout=time中的time值。这个头能够让一些浏览器主动关闭连接,这样服务器就不必去关闭连接了。没有这个参数,Nginx 不会发送 Keep-Alive 响应头。
client_header_timeout 客户端向服务端发送一个完整的 request header 的超时时间。如果客户端在指定时间内没有发送一个完整的 request header,Nginx 返回 HTTP 408(Request Timed Out)。
client_body_timeout 指定客户端与服务端建立连接后发送 request body 的超时时间。如果客户端在指定时间内没有发送任何内容,Nginx 返回 HTTP 408(Request Timed Out)
更改进程数
在高并发场景需要启动更多的nginx进程以保证快速响应,以处理用户的请求,避免造成阻塞
cat /proc/cpuinfo | grep -c "physical id" ##查看CPU核数
ps aux | grep nginx ##查看nginx主进程包含几个子进程
vim /usr/local/nginx/conf/nginx.conf
worker_processes 2; ##修改为cpu核数相同或者2倍
worker_cpu_affinity 01 10; ##设置每个进程有不同cpu处理,进程数配为4时0001、0010、0100、1000,1在哪个为位置就是第几个cpu
##将每个worker子进程与特定cpu物理核心绑定,优势在于避免同一个worker子进程在不同的CPU核心上切换,缓存失败,降低性能
systemctl restart nginx
配置网页压缩
nginx的ngx http gzip module压缩模块提供对文件内容压缩的功能
允许nginx服务器将输出内容在发送客户端之前进行压缩,以节约网站带宽,提升用户的访问体验,默认已经安装
可在配置文件中加入相应的压缩功能参数对压缩性能进行优化
vim /usr/local/nginx/conf/nginx.conf
http {
....
gzip on; #取消注释 开启gzip压缩功能
gzip_min_length 1k; #最小压缩文件大小
gzip_buffers 4 16k; #压缩缓冲区,大小为4个16k缓冲区
gzip_http_version 1.1; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
gzip_comp_level 6; #压缩比率
gzip_vary on; #支持前端缓存服务器存储压缩页面
gzip_types text/plain text/javascript application/x-javascript text/css text/xml application/xml application/xml+rss image/jpg image/jpeg image/png image/gif application/x-httpd-php application/javascript application/json; #压缩类型,表示哪些网页文档启用压缩功能
......
}
方便实验 修改网页内容并添加图片(前面实验以添加图片)
cd /usr/local/nginx/html
先将game.jpg文件传到/usr/local/nginx/html目录下
vim index.html
......
<img src="meme.jpg"/> #网页中插入图片
</body>
</html>
systemctl restart nginx
浏览器访问
配置防盗链
vim /usr/local/nginx/conf/nginx.conf
http {
......
server {
......
location ~*.(jpg|gif|swf)$ {
valid_referers *.benet.com benet.com;
if ( $invalid_referer ) {
rewrite ^/ http://www.benet.com/error.png;
#return 403;
}
}
......
}
}
~* .(jpg|gif|swf)$ :这段正则表达式表示匹配不区分大小写,以.jpg 或.gif 或.swf 结尾的文件; valid_referers :设置信任的网站,可以正常使用图片;
none:允许没有http_refer的请求访问资源(根据Referer的定义,它的作用是指示一个请求是从哪里链接过来的,如果直接在浏览器的地址栏中输入一个资源的URL地址,那么这种请求是不会包含Referer字段的),如www.benet.com/meme.jpg
if语句:如果链接的来源域名不在valid_referers所列出的列表中,$invalid_referer为1,则执行后面的操作,即进行重写或返回 403 页面。
网页准备:
Web源主机(192.168.46.40)配置:
cd /usr/local/nginx/html
将meme.jpg、error.png文件传到/usr/local/nginx/html目录下
vim index.html
......
<img src="meme.jpg"/>
</body>
</html>
echo "192.168.46.40 www.weq.com" >> /etc/hosts
echo "192.168.46.50 www.ly.com" >> /etc/hosts
盗链网站主机(192.168.46.50):
cd /usr/local/nginx/html
vim index.html
......
<img src="http://www.weq.com/meme.jpg"/>
</body>
</html>
echo "192.168.46.40 www.weq.com" >> /etc/hosts
echo "192.168.46.50 www.ly.com" >> /etc/hosts
在盗图网站主机上进行浏览器验证
http://www.ly.com
盗链主页网页准备
[root@localhost ~]#systemctl stop firewalld.service
[root@localhost ~]#setenforce 0
[root@localhost ~]#yum install -y httpd
[root@localhost ~]#cd /var/www/html
[root@localhost html]#vim index.html
<html>
<body>
<h1>this is hotlinking web!</h1>
<img src="http://www.weq.com/meme.jpg"/>
</body>
</html>
盗链主机临时添加域名和ip映射关系
[root@localhost html]#vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.46.40 www.weq.com
192.168.46.50 www.ly.com
盗链主机访问网页
修改源主机的nginx主配置文件
vim /usr/local/nginx/conf/nginx.conf
location ~*.(gif|jpg|jepg|swf)$ {
valid_referers none blocked *.weq.com weq.com;
if ( $invalid_referer ) {
rewrite ^/ http://www.weq.com/error.png;
}
}
重启服务,在源主机加入防盗链图片,并添加映射
[root@localhost conf]#vim nginx.conf
[root@localhost conf]#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 conf]#vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.46.40 www.weq.com
192.168.46.50 www.ly.com
[root@localhost conf]#systemctl restart nginx
加入防盗链图片
[root@localhost conf]#cd /usr/local/nginx/html/
[root@localhost html]#ls
50x.html index.html meme.jpg
[root@localhost html]#rz -E
rz waiting to receive.
[root@localhost html]#ls
50x.html error.png index.html meme.jpg