• Nginx优化与防盗链



    前言

    在企业信息化应用环境中,服务器的安全性和响应速度需要根据实际情况进行响应参数配置,以达到最优的用户体验。
    默认的 Nginx 安装参数只能提供最基本的服务,还需要调如网页缓存时间、连接超时、网页压缩等响应参数,才能发挥出服务器的最大作用。

    一、Nginx服务优化

    1.隐藏版本号

    为防止攻击者针对特定版本进行攻击,我们需要隐藏Nginx的版本号

    查看方法

    CentOS中使用命令curl

    curl -I 192.168.28.100 查看的是头部信息
    在这里插入图片描述

    浏览器查看

    浏览器 → 开发者工具(F12) → 选择network → 刷新页面(Ctrl +r) → 选择请求 → 选择 headlers → 查看版本
    在这里插入图片描述

    1.1 修改配置文件

    [root@yzq conf]# vim nginx.conf
    
    
    • 1
    • 2

    在这里插入图片描述
    重启服务查看,版本号不显示
    在这里插入图片描述

    1.2 修改源码

    修改版本号和服务器类型

    [root@yzq nginx-1.15.9]# vim /opt/nginx-1.15.9/src/core/nginx.h
    
    
    • 1
    • 2

    在这里插入图片描述
    修改后重新编译安装

    [root@yzq nginx-1.15.9]# cd /opt/nginx-1.15.9/
    [root@yzq nginx-1.15.9]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install 
    
    
    • 1
    • 2
    • 3

    重新开启版本信息

    [root@yzq local]# vim /usr/local/nginx/conf/nginx.conf
    
    • 1

    在这里插入图片描述
    重启服务并查看版本,版本号更改
    在这里插入图片描述

    2.修改用户和组

    2.1 编译时指定用户和组

    在编译时可以指定用户和组;父进程由root创建,子进程由nginx创建

    [root@yzq nginx-1.15.9]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
    [root@yzq nginx-1.15.9]# ps aux | grep nginx | grep -v grep
    root      11230  0.0  0.0  20556   624 ?        Ss   17:46   0:00 nginx: master process /usr/local/nginx/sbin/nginx
    nginx     11231  0.0  0.0  23092  1640 ?        S    17:46   0:00 nginx: worker process
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2.2 修改配置文件(编译时未指定用户与组)

    如果没有安装前创建用户,则在此服务中默认使用的是nobody
    默认是nginx默认使用nobody用户账号与组账号

    [root@yzq nginx-1.15.9]# vim /usr/local/nginx/conf/nginx.conf
    
    
    • 1
    • 2

    在这里插入图片描述

    在这里插入图片描述

    3.配置网页缓存时间

    为加快访问速度,对静态网页设置缓存时间

    3.1 以图片作为缓存对象,修改主配置文件,指定缓存时间

    在这里插入图片描述

    3.2 重启nginx,查看缓存时间

    [root@yzq html]# curl -I 192.168.28.100/renwu.jpg
    HTTP/1.1 200 OK
    Server: yy/666
    Date: Mon, 29 Aug 2022 11:24:28 GMT
    Content-Type: image/jpeg
    Content-Length: 177298
    Last-Modified: Mon, 29 Aug 2022 11:03:40 GMT
    Connection: keep-alive
    ETag: "630c9d0c-2b492"
    Expires: Tue, 30 Aug 2022 11:24:28 GMT
    Cache-Control: max-age=86400  ##缓存时间
    Accept-Ranges: bytes
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    也可网页查看
    在这里插入图片描述

    添加照片到网页

    需将照片放到html下

    [root@yzq html]# vim index.html 
    [root@yzq html]# systemctl restart nginx
    
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    4.日志切割

    太大的日志文件不利于分析排查,因此需要定期进行日志文件的切割

    4.1 编写脚本

    [root@yzq html]# vim /opt/fenge.sh
    [root@yzq html]# cat /opt/fenge.sh 
    #!/bin/bash
    
    d=$(date -d "-1 day" "+%Y%m%d")      #显示前一天的时间
    logs_path="/var/log/nginx"           #日志路径      
    pid_path=`cat /usr/local/nginx/logs/nginx.pid`   #pid路径
    
    [ -d $logs_path ] || mkdir -p $logs_path      #不存在即创建日志文件目录
    
    #移动并重命名日志文件
    mv /usr/local/nginx/logs/access.log ${logs_path}/yzq.com-access.log-{$d}
    
    #平滑重建日志文件
    kill -USR1 $pid_path
    #删除30天前的日志文件                   
    find $logs_path -mtime +30 -exec rm -rf {} \;
    #find $logs_path -mtime +30 |xargs rm -rf
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    4.2 执行脚本,查看日志是否被切割

    [root@yzq nginx]# sh /opt/fenge.sh 
    [root@yzq nginx]# ls
    test.com-access.log-{20220828}
    
    
    • 1
    • 2
    • 3
    • 4
    [root@yzq nginx]# ls /var/log/nginx/
    test.com-access.log-{20220828}   #查看前一天日志
    [root@yzq nginx]# date -s 20220829
    2022年 08月 29日 星期一 00:00:00 CST
    [root@yzq nginx]# sh /opt/fenge.sh 
    [root@yzq nginx]# ls /var/log/nginx/
    test.com-access.log-{20220828}
    [root@yzq nginx]# date 
    2022年 08月 29日 星期一 00:00:19 CST
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    4.3 设置定时任务

    每天凌晨1点执行脚本

    [root@yzq nginx]# crontab -l
    0 1 * * * sh /opt/fenge.sh
    
    
    • 1
    • 2
    • 3

    5.设置连接超时

    HTTP有一个KeepAlive模式,它告诉web服务器在处理完一个请求后保持这个TCP连接的打开状态。若接收到来自客户端的其它请求,服务端会利用这个未被关闭的连接,而不需要再建立一个连接。
    ​KeepAlive 在一段时间内保持打开状态,它们会在这段时间内占用资源。占用过多就会影响性能。

    5.1 修改配置文件,设置超时时间

    [root@yzq nginx]# vim /usr/local/nginx/conf/nginx.conf
    
    
    • 1
    • 2

    在这里插入图片描述

    5.2 查看效果(请求头和请求体在特殊情况下才能显示效果,此处不演示)

    在这里插入图片描述

    在这里插入图片描述
    ​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优化深入

    1.更改进程数

    高并发时可以修改为cpu核数的两倍

    [root@yzq nginx]# cat /proc/cpuinfo | grep -c 'physical id'
    2   ##查看支持最大核心数
    
    
    • 1
    • 2
    • 3

    只有两个进程,一个主进程包含一个子进程

    [root@yzq nginx]# ps aux | grep nginx | grep -v grep
    root      13109  0.0  0.0  20560   632 ?        Ss   00:24   0:00 nginx: master process /usr/local/nginx/sbin/nginx
    nginx     13110  0.0  0.0  23096  1648 ?        S    00:24   0:00 nginx: worker process
    [root@yzq nginx]# vim /usr/local/nginx/conf/nginx.conf
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3## 1.2 重启服务,查看效果

    [root@yzq nginx]# nginx -t
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: [warn] the number of "worker_processes" is not equal to the number of "worker_cpu_affinity" masks, using last mask for remaining worker processes
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
    [root@yzq nginx]# systemctl restart nginx.service 
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    设置每个进程由不同cpu处理,进程数配2 4 6 8分别为0001 0010 0100 1000
    在这里插入图片描述
    共五个进程,一个master,四个worker

    [root@yzq nginx]# ps aux | grep nginx | grep -v grep
    root      13294  0.0  0.0  20560   628 ?        Ss   00:38   0:00 nginx: master process /usr/local/nginx/sbin/nginx
    nginx     13295  0.0  0.0  23096  1400 ?        S    00:38   0:00 nginx: worker process
    nginx     13296  0.0  0.0  23096  1400 ?        S    00:38   0:00 nginx: worker process
    nginx     13297  0.0  0.0  23096  1400 ?        S    00:38   0:00 nginx: worker process
    nginx     13298  0.0  0.0  23096  1400 ?        S    00:38   0:00 nginx: worker process
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2.配置网页压缩

    为节约网站的带宽,将网页内容在输出时进行压缩

    2.1 修改配置文件

    [root@yzq nginx]# vim /usr/local/nginx/conf/nginx.conf
    
    
    • 1
    • 2

    在这里插入图片描述

    2.2 重启服务,查看效果

    [root@yzq nginx]# nginx -t  #检查配置文件是否出错
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is 
    nginx: [warn] the number of "worker_processes" is not equal to the number
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is succes
    [root@yzq nginx]# systemctl restart nginx.service 
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    3.配置防盗链

    3.1 实现盗链功能

    源主机:192.168.28.100 www.yzq.com
    盗链端:192.168.28.10  aaa.com
    测试端:win10  192.168.28.169
    
    
    • 1
    • 2
    • 3
    • 4

    服务端源主机准备图片
    在这里插入图片描述
    源主机与盗链端,盗链端起始无图片
    在这里插入图片描述

    3.1.1盗链端配置
    [root@yzq opt]#cat /etc/hosts
    127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
    192.168.28.10 www.yzq.com
    [root@yzq nginx]#cd /usr/local/nginx/html/
    [root@yzq html]#vim index.html 
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

    3.1.2 查看效果

    直接盗图过来,不需要自己上传
    在这里插入图片描述

    3.2 防盗链

    3.2.1 修改源主机配置文件
    [root@yzq html]# vim /usr/local/nginx/conf/nginx.conf
    
    
    • 1
    • 2

    在这里插入图片描述
    ​~* .(jpg|gif|jepg|bmp|ico)$ :这段正则表达式表示匹配不区分大小写,以.jpg 或.gif 或.swf
    结尾的文件;
    ​valid_referers :设置信任的网站,可以正常使用图片;​
    ​后面的网址或者域名 :referer 中包含相关字符串的网址;
    if语句:如果链接的来源域名不在valid_referers所列出的列表中,$invalid_referer为1,则执行后面的操作

    3.2.2 查看效果

    在这里插入图片描述

    4.FPM参数优化

    首先安装带FPM模块的PHP环境,保证PHP正常运行
    FPM进程有两种启动方式,由pm参数指定,分别是static和dynamic

    pm.max_children static模式下空闲进程数上限,大于下面的值
    pm.start_servers 动态方式下默认开启的进程数,在最小和最大之间
    pm.min_spare_server 动态方式下最少空闲进程数
    pm.max_spare_server 动态方式下最大空闲进程数

    [root@yzq etc]#cd /usr/local/php/etc/
    [root@yzq etc]#ls
    pear.conf  php-fpm.conf  php-fpm.conf.default  php-fpm.d
    [root@yzq etc]#cp php-fpm.conf.default php-fpm.conf
    cp:是否覆盖"php-fpm.conf"? y
    [root@yzq etc]#vim php-fpm.conf
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

    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  #动态方式下最大空闲进程数
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    [root@yzq etc]#/usr/local/php/sbin/php-fpm  -c /usr/local/php/lib/php.ini  #启动php-fpm,不可用于重启
    [root@yzq etc]#kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`  #执行第一个命令后,就可以使用下面这条命令查看pid号重启php-fpm
    [root@yzq etc]#netstat -anpt | grep 9000
    tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      13967/php-fpm: mast 
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    总结

    默认的Nginx安装参数只能提供最基本的服务,还需要调整相应的参数,才能发挥出服务器的最大作用

  • 相关阅读:
    创建第一个Servlet程序“hello world“(创建流程+页面出错情况)
    FineReport制作任务日历
    云栖大会,一场边缘云计算的「超前瞻」之约
    svg学习 路由跳转方式以及传(获取)参 路由获取参数 懒加载
    Java线程定时器
    RabbitMQ相关概念
    C语言----------最后一个单词的大小
    Python内置函数
    SpringBoot笔记:SpringBoot集成MyBatis实战
    matlab根轨迹绘制
  • 原文地址:https://blog.csdn.net/weixin_71429790/article/details/126587437