目录
一、Ngnix服务优化与防盗链
1:配置版本号的方法
1.1:修改配置文件
?1.2 修改源码
2、 修改nginx的运行用户和组
2.1 方法一在编译安装时,指定运行用户和组
?2.2 修改配置文件nginx.conf
?编辑?3、修改缓存时间
4、日志分割
5、设置连接超时时间
6、更改工作进程数
?7、配置网页压缩
?8. 配置防盗链
8.1 nginx.conf配置文件
8.2?网页准备
?9、总结
一、Ngnix服务优化与防盗链
1:配置版本号的方法
1.1:修改配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
http {
...
server_tokens off; #添加,关闭版本号
...
[root@localhost ~]# systemctl restart nginx.service //重启服务
[root@localhost ~]# curl -I http://192.168.200.101

?1.2 修改源码
[root@localhost ~]# vim /opt/nginx-1.18.0/src/core/nginx.h
#define NGINX_ VERSION "1.1.1" //修改版本号
#define NGINX_ VER "IIS" NGINX_ VERSION //修改服务器类型
[root@localhost ~]# cd /opt/nginx-1.12.0/
[root@localhost ~]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@localhost ~]# make && make install
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
http {
.... server_tokens on; ##打开版本号
....
[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# curl -I http//192.168.200.101

2、 修改nginx的运行用户和组
2.1 方法一在编译安装时,指定运行用户和组
cd nginx-1.12.2/
[root@localhost ~]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \ //用户 --group=nginx \ //用户组
--with-http_stub_status_module
[root@localhost ~]# ps aux

?2.2 修改配置文件nginx.conf
vim /usr/local/nginx/conf/nginx.conf
user nginx nginx; #取消注释,修改用户为nginx,组为nginx
?#重启服务 ?systemctl restart nginx
#查看是否修改成功。可以看到主进程由root创建,子进程由nginx创建 ?ps aux | grep nginx
?3、修改缓存时间
当Nginx将网页数据返回给客户端后,可设置缓存的时间,以方便在日后进行相同内容的请求时直接返回,避免重复请求,加快了访问速度。
一般针对静态网页设置,对动态网页不设置缓存时间。
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
http {
?...... ?
? ? ? ? ?server {
? ? ? ? ? ?......
? ? ? ? ? ?location / {
? root html; ? index index.html index.htm;
? } ? ?
#加入新的 location,以图片作为缓存对象
? location ~* \.(gif|jpg|jepg|bmp|ico)$ {
? root html;
? expires 1d; #指定缓存时间,1天 ?
? ? ? ? ? } ?
...... ?
? ? ? }
?}
?#重启nginx服务 ?systemctl restart nginx
#访问测试 ?Linux系统中,打开火狐浏览器,访问


?
4、日志分割
操作步骤:
1、创建旧日志存放目录
2、通过mv命令将原有日志移动到日志目录中
3、kill -USR1 < PID> 重新生成日志文件
4、删除30天之前的日志文件
[root@localhost ~]# 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
[root@localhost ~]# chmod +x /opt/fenge.sh
[root@localhost ~]# crontab -e
##设置周期性任务,每天定时做日志分割
0 1 * * * /opt/fenge.sh
5、设置连接超时时间
- HTTP有一个KeepAlive模式,它告诉web服务器在处理完一个请求后保持这个TCP连接的打开状态。若接收到来自同一客户端的其它请求,服务端会利用这个未被关闭的连接,而不需要再建立一个连接。
- KeepAlive 在一段时间内保持打开状态,它们会在这段时间内占用资源。占用过多就会影响性能。
- 在企业网站中,为了避免同一个客户长时间占用连接,造成资源浪费,可设置相应的连接超时参数,实现控制连接访问时间。可以修改配置文件 nginx.conf,设置 keepalive_timeout超时。
vim /usr/local/nginx/conf/nginx.conf
http {
?...... ? ?
? ? ?keepalive_timeout 65 180; ? ? ? //设置连接超时时间 ? ? ? ?
? ? ?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)。

?

?
6、更改工作进程数
在高并发场景,需要启动更多的Nginx进程以保证快速响应,以处理用户的请求,避免造成阻塞
?#1、查看cpu核数
cat /proc/cpuinfo |grep processor|wc -l
或
cat /proc/cpuinfo |grep -c processor
或
cat /proc/cpuinfo | grep -c "physical id"
#2、查看ginx主进程中包含几个工作进程
?ps aux | grep nginx
?#3、编辑配置文件,修改工作进程数
vim /usr/local/nginx/conf/nginx.conf
worker_processes ?2; #修改为与CPU核数相同
worker_cpu_affinity 01 10; #设置每个进程由不同cpu处理,进程数配为4时0001 0010 0100 1000
?#4、重启服务,查看修改后的工作进程
?systemctl restart nginx
ps aux | grep nginx

?

?7、配置网页压缩
nginx的ngx_http_gzip_module压缩模块提供对文件内容压缩的功能
允许nginx服务器将输出内容在发送客户端之前进行压缩,以节约网站带宽,提升用户的访问体验,默认已经安装
可在配置文件中加入相应的压缩功能参数对压缩性能进行优化
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
http {
....
gzip on;
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;#压缩类型,表示哪些网页文档启用压缩功能
......
}
[root@localhost ~]# cd /usr/local/nginx/html //先将game.jpg文件传到/usr/local/nginx/html目录下
[root@localhost ~]# vim index.html
<img src="game.jpg"/> ? ? ? ? ? ? #网页中插入图片
[root@localhost ~]# systemctl restart nginx

?8. 配置防盗链
8.1 nginx.conf配置文件
Web源主机: 192.168.200.100
[root@localhost ~]# cd /usr/local/nginx/html
//将game.jpg、error.png文件传到/usr/local/nginx/html目录下
[root@localhost html]# vim index.html
<html>
<body>
<img src="game.jpg"/>
</body>
</html>
[root@localhost ~]# echo "192.168.200.100 www.benet.com" >> /etc/hosts
[root@localhost ~]# echo "192.168.200.101 www.can.com" >> /etc/hosts
8.2?网页准备
盗链网站主机: 192.168.200.101
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum install -y httpd
[root@localhost ~]# cd /usr/local/nginx/html
[root@localhost ~]# vim index.html
<html>
<body>
<img src="http://www.weq.com/meme.jpg"/>
</body>
</html>
[root@localhost html]# systemctl start httpd
[root@localhost html]# echo "192.168.200.100 www.benet.com" >> /etc/hosts
[root@localhost html]# echo "192.168.200.101 www.can.com" >> /etc/hosts
在盗图网站进行浏览器验证
www.can.com

?9、总结
修改配置文件后,必须重启服务才能生效
防盗链实验中:盗链网站,需要将web主机的域名和IP的映射关系,写入/etc/hosts文件中,否则盗链网站无法展示web主机中的图片。
|