Nginx优化与防盗链

一、Nginx优化

1.查看版本号

1.本地查看
在这里插入图片描述
2.浏览器查看
在这里插入图片描述

2.隐藏版本号

1.修改配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
http {
 19     include       mime.types;
 20     default_type  application/octet-stream;
 21     server_tokens off;            #关闭版本号
[root@localhost ~]# systemctl restart nginx.service

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.修改用户和组

更改配置文件
若没有安装前创建用户,则在此服务中默认使用的时nobody

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
修改前:(默认是Nginx默认使用nobody用户账号与组账号)
#user  nobody;
worker_processes  1;
修改后:
user  nginx nginx;                                 #修改用户为nginx,组为nginx
worker_processes  1;
[root@localhost ~]# systemctl restart nginx.service
[root@localhost ~]# ps -aux | grep nginx                 #主进程由root创建,子进程由nginx创建
root       3210  0.0  0.0  20500   628 ?        Ss   14:54   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      3211  0.0  0.0  24228  2464 ?        S    14:54   0:00 nginx: worker process
root       3233  0.0  0.0 112676   984 pts/0    S+   14:56   0:00 grep --color=auto nginx

在这里插入图片描述

4.设置缓存时间

当Nginx将网页数据返回给客户端后,可设置缓存的时间,以方便在日后进行相同内容的请求时直接返回,避免重复请求,加快了访问速度。一般针对静态网页设置,对动态网页不设置缓存时间。
1.修改配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
        location / {
            root   html;
            index  index.html index.htm;
        }
#插入下面的命令:
        location ~ \.(gif|jpg|jpeg|png|bmp|ico)$ {      #加入location,以图片作为缓存对象
            root html;
            expires 1d;     #指定缓存时间为1,一天半为1d12h
        }
[root@localhost ~]# 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 ~]# systemctl restart nginx.service

在这里插入图片描述

2.上传识别图片,修改站点文件

[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# ls
50x.html  index.html
[root@localhost html]# rz -E           #插入图片
rz waiting to receive.
[root@localhost html]# ls
50x.html  index.html  jingxi.jpg
[root@localhost html]# vim index.html
<h1>Welcome to nginx!</h1>
<img src="jingxi.jpg"/>                #插入图片命令

在这里插入图片描述
在这里插入图片描述

5.日志分割

1.插入脚本

[root@localhost ~]# vim /opt/fenge.sh
#!/bin/bash
#Filename:fenge.sh
d=$(date -d "-1 day" "+%Y%m%d")               #显示前一天的时间
logs_path="/var/log/nginx"
pid_path=" /usr/local/nginx/logs/nginx.pid"
[ -d $logs_path ] || mkdir -p $logs_path      #创建日志文件目录
mv /usr/local/nginx/logs/access.log ${logs_path}/test.com-access.log-$d     #移动并重命名日志文件
kill -HUP $(cat $pid_path)                    #重建日志文件
find $logs_path -mtime +30 | xargs rm -rf     #删除30天前的日志文件

2.测试脚本

[root@localhost ~]# chmod +x /opt/fenge.sh 
[root@localhost ~]# crontab -e         #添加计划任务
0 1 * * * /root/fenge.sh
[root@localhost ~]# systemctl restart nginx.service 
[root@localhost ~]# netstat -natp | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      3940/nginx: master
[root@localhost ~]# sh -x /opt/fenge.sh
[root@localhost ~]# ls /var/log/nginx/
test.com-access.log-20211019
[root@localhost ~]# date -s 20211019
20211019日 星期二 00:00:00 CST

6.连接超时

在企业网站中,为了避免同一个客户长时间占用连接,造成资源浪费,可以设置相应的超时参数,实现对连接访问时间的控制
HTTP 有一个 KeepAlive 模式,它告诉 web 服务器在处理完一个请求后保持这个 TCP 连接的打开状态
若接收到来自客户端的其它请求,服务端会利用这个未被关闭的连接,而不需要再建立一个连接
KeepAlive 在一段时间内保持打开状态,它们会在这段时间内占用资源,而占用过多就会影响性能

Keepalive_timeout:设置连接保持超时时间

Client_header_timeout:指定等待客户端发送请求头的超时时间

Client_body_timeout:设置请求体读超时时间
修改配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
    #keepalive_timeout  0;
keepalive_timeout  65;
client_header_timeout 80;     #等待客户端发送请求头的超时时间,超时会发送408错误
client_body_timeout 80;       #等待客户端发送请求体的超时时间
[root@localhost ~]# 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 ~]# systemctl restart nginx.service

在这里插入图片描述

7.更改进程数

在高并发环境中,需要启动更多的 nginx 进程以保证快速响应,用以处理用户的请求,避免造成阻塞
默认情况下,nginx 的多个进程可能更多的跑在一颗 CPU 上,可以分配不同的进程给不同的 CPU 处理,以充分利用 cpu 性能
worker_processes 最多开启 8 个,8 个以上性能就不会再提升了,而且稳定性会变低

1.查看CPU核心数

[root@localhost ~]# cat /proc/cpuinfo | grep -c "physical"
8

2.查看主进程包含的子进程

[root@localhost ~]# ps aux | grep nginx
root      42764  0.0  0.0  20496   612 ?        Ss   00:21   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     42765  0.0  0.0  23024  1640 ?        S    00:21   0:00 nginx: worker process
root      42913  0.0  0.0 112676   984 pts/1    S+   00:33   0:00 grep --color=auto nginx

在这里插入图片描述

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes  2;           #修改与CPU核数相同或2( cgroup )
worker_cpu_affinity 01 10;     #设置每个进程由不同cpu处理,进程数配为2时,为0001 0010 0100 1000
PS###worker_processes最多开启8个,8个以上性能就不会再提升了,而且稳定性会变的更低,因此8个进程够用了
###22进程:
worker_processes 2;
worker_cpu_affinity 01 10;

###44进程:
worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000

###88进程:
worker_processes 8;
worker_cpu_affinity 0001 0010 0100 1000 10000 100000 1000000 10000000
[root@localhost ~]# 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 ~]# systemctl restart nginx.service

8.配置网页压缩

Nginx 的 ngx_http_gzip_module 压缩模块提供了对文件内容压缩的功能,允许 Nginx 服务器将输出内容发送到客户端之前进行压缩,以节约网站的带宽,提升用户的访问体验
默认 Nginx 已经安装该模块,只需在配置文件中加入相应的压缩功能参数即可对压缩性能进行优化

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
   gzip on;                    #取消注释,开启gzip压缩功能
   gzip_min_length 1k;         #最小压缩文件大小
   gzip_buffers 4 16k;         #压缩缓冲区,大小为416k缓冲区
   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 ~]# 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 ~]# systemctl restart nginx.service 
[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# ls
50x.html  index.html
[root@localhost html]# rz -E                    #插入图片进行测试
rz waiting to receive.
[root@localhost html]# vim index.html
<h1>Welcome to nginx!</h1>
	<img src="qia.jpg"/>		                #插入此行
[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.service

在这里插入图片描述

二、盗链与防盗链

在企业网站中,一般都要配置防盗链功能,以避免网站内容被非法盗用,造成经济损失,也避免不必要的带宽浪费。

1.盗链配置

1.服务端配置
服务端IP:192.168.9.135

[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# ls
50x.html  index.html  qia.jpg
[root@localhost html]# vim index.html 
<h1>Welcome to nginx!</h1>
<img src="qia.jpg"/>
[root@localhost html]# vim /etc/hosts
192.168.9.135 www.qia.com

在这里插入图片描述
2.盗链端配置
盗链端IP:192.168.9.131
盗链的目标是135的内容

[root@localhost ~]# systemctl stop firewalld && systemctl disable firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# ntpdate ntp1.aliyun.com   #同步时间
#安装 httpd 服务
[root@localhost ~]# yum -y install httpd      
[root@localhost ~]# systemctl start httpd && systemctl enable httpd
#配置临时 DNS 映射
[root@localhost ~]# echo "192.168.9.135 www.qia.com" >>/etc/hosts
[root@localhost ~]# echo "192.168.9.131 www.daolian.com" >>/etc/hosts
[root@localhost ~]# cat > /var/www/html/index.html <<EOF
> <html><body><h1>this is a "盗链" test</h1>
> <img src= "http://www.qia.com/qia.jpg"/>
> </body></html>
> EOF
[root@localhost ~]# systemctl restart httpd

在这里插入图片描述

2.防盗链

上传 daolian.jpg 图片到 /usr/local/nginx/html 目录下
[root@nginx ~]#vim /usr/local/nginx/conf/nginx.conf
    server {
    ......
        location ~* \.(jpeg|gif|jpg|swf)$ {
#匹配不区分大小写,以 .jpeg/.gif/.jpg/.swf 结尾的文件
        valid_referers none blocked *.qia.com qia.com;      
#设置信任来源可以正常访问资源
        if ( $invalid_referer ) {
        rewrite ^/ http://www.qia.com/daolian.png;
        #return 403;
#如果链接的来源域名不在 valid_referers 列表中,则 rewrite 跳转页面或者返回 403 页面
       }
      }
     }
[root@nginx ~]#nginx -t
[root@nginx ~]#systemctl restart nginx

#valid referers:设置信任的网站,即能引用相应图片的网站。
#none:浏览器中Referer为空的情况,就是直接在浏览器访问图片。
#blocked:referer不为空的情况,但是值被代理或防火墙删除了,这些值不以http://或者https://开头。
后面的网址或者域名:referer中包含相关字符串的网址。

在这里插入图片描述

三、FPM 参数优化

nginx 的 PHP 解析功能实现如果是交由 FPM 处理的,为了提高 PHP 的处理速度,可对 FPM 模块进行参数的调整。

首先安装带 FPM 模块的 PHP 环境,保证 PHP 可以正常运行
FPM 进程有两种启动方式,由 pm 参数指定,分别是 static 和 dynamic,前者将产生固定数据的 fpm 进程,后者将以动态的方式产生 fpm 进程
static 方式可以使用 pm.max_children 指定启动的进程数量。dynamic 方式的参数则要根据服务器的内存与服务负载进行调整,参数下所示
pm.max_children——指定启动的进程的最大的数量
pm.start_servers——动态方式下初始的 ftpm 进程数量
pm.min_spare_servers——动态方式下最小的 fpm 空闲进程数
pm.max_spare_servers——动态方式下最大的 fpm 空闲进程数

cd /usr/ local/php/etc/
cp php-fpm.conf.default php-fpm.conf
vim php-fpm.conf
pid = run/php-fpm.pid
vim /usr/local/php/etc/php-fpm.d/ www .conf
#96行
pm = dynamic                  #fpm进程启动方式,动态的
#107行
pm.max_children=20            #fpm进程启动的最大进程数
#112行
pm.start_servers = 5          #动态方式下启动时默认开启的进程数,在最小和最大之间
#117行
pm.min_spare_servers = 2      #动态方式下最小空闲进程数
#122行
pm. max_spare_servers = 8     #动态方式下最大空闲进程数
#启动php-fpm,不可用于重启
/usr/local/php/sbin/php-fpm -c /usr/local/php/lib/php.ini
#执行第一个命令后,就可以使用下面这条命令查看pid号重启php-fpm
kill -USR2 `cat /usr/ local/php/var/run/php-fpm.pid`
netstat -anpt l grep 9000
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值