Nginx优化与防盗链(隐藏版本号、配置网页缓存时间、日志切割、Nginx 深入优化、配置网页压缩、配置防盗链等等)

一、 Nginx 服务优化

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

1.1 隐藏版本号

和apache一样,在生产环境中,需要隐藏 Nginx 的版本号,以避免泄漏 Nginx 的版本,使攻击者不能针对特定版本进行攻击。

手工编译安装nginx

之前对于手工编译安装nginx的步骤已经讲解过很多次了,所以这里就不过多讲解。

[root@localhost ~]# cd /opt
[root@localhost opt]# ls
nginx-1.12.2.tar.gz  rh
[root@localhost opt]# iptables -F
[root@localhost opt]# setenforce 0
[root@localhost opt]# tar zvxf nginx-1.12.2.tar.gz
[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx
[root@localhost nginx-1.12.2]# yum install gcc gcc-c++ pcre pcre-devel zlib-devel -y
[root@localhost nginx-1.12.2]# ./configure \
   --prefix=/usr/local/nginx \
   --user=nginx \
   --group=nginx \
   --with-http_stub_status_module 
[root@localhost nginx-1.12.2]# make && make install
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin

[root@localhost nginx-1.12.2]# vim /etc/init.d/nginx
#!/bin/bash
#chkconfig:-  99 20
#description:Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
 start)
  $PROG
 ;;
stop)
   kill -s QUIT $(cat $PIDF)
 ;;
restart)
 $0 stop
 $0 start
 ;;
reload)
 kill -s HUP $(cat $PIDF)
 ;;
 *)
   echo "Usage:$0{start|stop|restart|reload}"
   exit 1
esac
exit 0
[root@localhost nginx-1.12.2]# chmod +x /etc/init.d/nginx
[root@localhost nginx-1.12.2]# chkconfig --add /etc/init.d/nginx
[root@localhost nginx-1.12.2]# service nginx start

在win10 虚拟机中验证服务

在这里插入图片描述

[root@localhost nginx-1.12.2]# curl -I http://14.0.0.27
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Mon, 10 Aug 2020 13:56:06 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 10 Aug 2020 13:48:12 GMT
Connection: keep-alive
ETag: "5f31501c-264"
Accept-Ranges: bytes

在这里插入图片描述
隐藏版本号的方法有两种

  • 第一种是修改Nginx的主配置文件
  • 第二种是修改Nginx源码文件,指定不显示版本号

修改Nginx的主配置文件

[root@localhost nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf

在这里插入图片描述

[root@localhost nginx-1.12.2]# service  nginx stop
[root@localhost nginx-1.12.2]# service  nginx start
[root@localhost nginx-1.12.2]# curl -I http://14.0.0.27
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 10 Aug 2020 14:03:46 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 10 Aug 2020 13:48:12 GMT
Connection: keep-alive
ETag: "5f31501c-264"
Accept-Ranges: bytes

在这里插入图片描述

修改Nginx源码文件

[root@localhost ~]# cd /opt
[root@localhost opt]# ls
nginx-1.12.2.tar.gz  rh
[root@localhost opt]# iptables -F
[root@localhost opt]# setenforce 0
[root@localhost opt]# tar zvxf nginx-1.12.2.tar.gz
[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx
[root@localhost nginx-1.12.2]# yum install gcc gcc-c++ pcre pcre-devel zlib-devel -y
[root@localhost nginx-1.12.2]# cd src/core/
[root@localhost core]# vim nginx.h
#define NGINX_VERSION      "1.1.7"

在这里插入图片描述
然后执行编译安装过程,并开启

[root@localhost core]# cd /opt/nginx-1.12.2/
[root@localhost nginx-1.12.2]# ./configure \
   --prefix=/usr/local/nginx \
   --user=nginx \
   --group=nginx \
   --with-http_stub_status_module 
[root@localhost nginx-1.12.2]# make && make install
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin
[root@localhost nginx-1.12.2]# nginx
[root@localhost nginx-1.12.2]# curl -I http://14.0.0.14
HTTP/1.1 200 OK
Server: nginx/1.1.7
Date: Mon, 10 Aug 2020 14:30:48 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 10 Aug 2020 14:29:15 GMT
Connection: keep-alive
ETag: "5f3159bb-264"
Accept-Ranges: bytes

在这里插入图片描述

1.2 修改用户与组

[root@localhost nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf 
user nginx nginx;

在这里插入图片描述

重启服务
[root@localhost nginx-1.12.2]# service nginx stop
[root@localhost nginx-1.12.2]# service nginx start
[root@localhost nginx-1.12.2]# ps aux | grep  nginx

在这里插入图片描述
也可以在编译过程中直接指定用户与组。

1.3 缓存时间

[root@localhost nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf 
location ~\.(gif|jpg|jpeg|png|ico)$ {
            root   html;
            expires 1d;
        }

在这里插入图片描述

[root@localhost nginx-1.12.2]# cd  /usr/local/nginx/html
[root@localhost html]# vim index.html 
<img src="why.png"/>
并将图片拷贝到当前目录
[root@localhost html]# ls
50x.html  index.html  why.png

在这里插入图片描述

重启服务
[root@localhost html]# service nginx stop
[root@localhost html]# service nginx start

在win10虚拟机中验证
在这里插入图片描述
在这里插入图片描述

1.4 日志分割

制作日志分割脚本

[root@localhost opt]# vim 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)
#删除30天前的日志
find $logs_path -mtime +30 | xargs rm -rf
[root@localhost opt]# ./fenge.sh 
需要启动服务
[root@localhost opt]# ls /var/log/nginx/
test.com-access.log-200810

在这里插入图片描述
如果想要定期每天都进行日志分割,我们可以通过周期性任务计划crontab来定期执行。

1.5 更改运行进程数

在高并发环境中,需要启动更多的 Nginx 进程以保证快速响应,用以处理用户的请求, 避免造成阻塞。

使用 ps aux 命令查看 Nginx 运行进程的个数。从命令执行结果可以看出 master process 是 Nginx 的主进程,开启了 1 个;worker process 是子进程,子进程也是开启了1个,与配置文件中对应。

多的那一个进程是grep的进程
[root@localhost opt]# ps aux | grep nginx
root      21098  0.0  0.0  20620  1472 ?        Ss   09:37   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     21124  0.0  0.0  23124  1456 ?        S    09:38   0:00 nginx: worker process
root      22163  0.0  0.0 112724   988 pts/0    S+   09:51   0:00 grep --color=auto nginx
[root@localhost opt]# cat /proc/cpuinfo | grep -c "physical"
8  ##查看到最大支持的核心数是8
[root@localhost opt]# vim /usr/local/nginx/conf/nginx.conf
worker_processes  8;

在这里插入图片描述

进程数就变成了8
[root@localhost opt]# ps aux | grep nginx
root      22242  0.0  0.0  20544   672 ?        Ss   09:52   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     22243  0.0  0.0  23072  1388 ?        S    09:52   0:00 nginx: worker process
nginx     22244  0.0  0.0  23072  1388 ?        S    09:52   0:00 nginx: worker process
nginx     22245  0.0  0.0  23072  1388 ?        S    09:52   0:00 nginx: worker process
nginx     22246  0.0  0.0  23072  1388 ?        S    09:52   0:00 nginx: worker process
nginx     22247  0.0  0.0  23072  1388 ?        S    09:52   0:00 nginx: worker process
nginx     22248  0.0  0.0  23072  1388 ?        S    09:52   0:00 nginx: worker process
nginx     22249  0.0  0.0  23072  1388 ?        S    09:52   0:00 nginx: worker process
nginx     22250  0.0  0.0  23072  1388 ?        S    09:52   0:00 nginx: worker process
root      22252  0.0  0.0 112724   984 pts/0    S+   09:52   0:00 grep --color=auto nginx

1.6 设置连接超时

[root@localhost opt]# vim /usr/local/nginx/conf/nginx.conf
要写在http的大括号中
    keepalive_timeout  65;
    client_header_timeout 60;
    client_body_timeout 60;

在这里插入图片描述

1.7 网页压缩

[root@localhost opt]# vim /usr/local/nginx/conf/nginx.conf

在这里插入图片描述

    gzip  on;      ##开启gzip压缩功能
    gzip_min_length 1k;   ##大于1K开始压缩
    gzip_buffers 4 16k;   ##缓存空间大小
    gzip_http_version 1.1;   ##压缩版本
    gzip_comp_level 6;       ##压缩比率,最小为1,速度最快,最大为9,压缩比最高
    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]\.";   ##微软IE浏览器1-6版本禁用
    gzip_vary on;      ##支持vary header,让前端的缓存服务器经过gzip压缩的页面
[root@localhost opt]# service nginx stop
[root@localhost opt]# service nginx start

在win10虚拟机中进行访问测试,访问的同时开启抓包
在这里插入图片描述
在这里插入图片描述

1.8 防盗链

我们用14.0.0.14这一台虚拟机安装httpd网站作为盗链网站。

盗链网站

[root@localhost opt]# yum install httpd -y
[root@localhost opt]# vim /etc/httpd/conf/httpd.conf

在这里插入图片描述

[root@localhost opt]# vim /var/www/html/index.html
<h1>this is a text web</h1>
<img src="http://www.abc.com/why.png"/>

14.0.0.27官网

14.0.0.27官网
[root@localhost opt]# yum install -y bind 
[root@localhost opt]# vim /etc/named.conf 

在这里插入图片描述

[root@localhost opt]# vim /etc/named.rfc1912.zones 

在这里插入图片描述

[root@localhost opt]# cp -p /var/named/named.localhost /var/named/abc.com.zone
[root@localhost opt]# vim /var/named/abc.com.zone 

在这里插入图片描述

[root@localhost opt]# systemctl start named

在win10虚拟机中访问,将win10中的DNS设为14.0.0.27
在这里插入图片描述

设置防盗链

14.0.0.27中nginx配置文件

[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
location ~*\.(gif|jepg|png)$ {
            valid_referers none blocked *.abc.com abc.com;
            if ($invalid_referer) {
               rewrite ^/ http://www.abc.com/error.jpg;
            }
        }
[root@localhost opt]# cd /usr/local/nginx/html/
将防止盗链的图片加进去
[root@localhost html]# ls
50x.html  error.jpg  index.html  why.png
[root@localhost html]# service nginx stop
[root@localhost html]# service nginx start

在这里插入图片描述
在win10虚拟机中进行访问
在这里插入图片描述

9 fpm参数优化

在php配置文件php-fpm.conf中
pid = run/php-fpm.pid
pm = dynamic
pm.max_children=20   ##static模式下空闲进程数上限,大于下面的值
pm.start_server=5   ##动态方式下默认开启的进程数,在最小和最大之间
pm.min_spare_servers=2   ##动态方式下最少空闲进程数
pm.max_spare_servers=8   ##动态方式下最大空闲进程数

猜你喜欢

转载自blog.csdn.net/kimowinter/article/details/107920991