Nginx优化与防盗链

目录

引言

一、隐藏版本号

1、查看版本号

2、Nginx隐藏版本号的方式

二、修改用户和组

1、更改配置文件

2、重启服务,查看服务状态

三、设置缓存时间

1、修改配置文件

2、重启服务

3、上传识别图片,修改站点文件

4、验证

四、日志分割

1、插入脚本

2、测试脚本

五、连接超时

1、超时参数

2、修改配置文件

六、更改进程数

1、查看核心数

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

七、网页压缩

1、修改配置文件

2、重启并验证

八、盗链与防盗链

1、盗链配置

2、防盗链配置

九、FPM模块参数优化

总结


引言

服务器的优化至关重要,对于安全性和响应速度需要进行相关的参数配置,以达到最优的用户体验,还需调整缓存时间、连接超时、网页压缩,才能发挥服务器的最大用处。

一、隐藏版本号

1、查看版本号

(1)本地查看(查看头部信息)

 

(2)浏览器查看

谷歌浏览器:更多工具→开发者工具→Network→刷新页面→点击ip地址→Headers→查看到版本

2、Nginx隐藏版本号的方式

(1)修改配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
http {
    include       mime.types;
    default_type  application/octet-stream;
    server_tokens off;     #添加命令,指的是关闭版本号

[root@localhost ~]# systemctl restart nginx.service 

 验证:

 (2)修改源码

[root@localhost ~]# vim /opt/nginx-1.12.2/src/core/nginx.h
修改前:
#define NGINX_VERSION      "1.12.2"
#define NGINX_VER          "nginx/" NGINX_VERSION
修改后:
#define NGINX_VERSION      "8.8.8"
#define NGINX_VER          "xxx/" NGINX_VERSION

[root@localhost ~]# 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]# systemctl restart nginx.service

二、修改用户和组

1、更改配置文件

若没有安装前创建用户,则在此服务中默认使用的时nobody

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
修改前:(默认是Nginx默认使用nobody用户账号与组账号)
#user  nobody;
worker_processes  1;
修改后:
user  nginx nginx;
worker_processes  1;

2、重启服务,查看服务状态

[root@localhost ~]# systemctl restart nginx.service 

三、设置缓存时间

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

1、修改配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
        location / {
            root   html;
            index  index.html index.htm;
        }
#插入下面的命令:
        location ~ \.(gif|jpg|jepg|png|bmp|ico)$ {      #加入location,以图片作为缓存对象
            root html;
            expires 1d;     #指定缓存时间,1天
        }

2、重启服务

[root@localhost ~]# nginx -t
[root@localhost ~]# systemctl restart nginx.service

3、上传识别图片,修改站点文件

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

4、验证

四、日志分割

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      83547/nginx: master 
[root@localhost ~]# sh -x /opt/fenge.sh
[root@localhost ~]# ls /var/log/nginx/
test.com-access.log-20211007
[root@localhost opt]# date -s 20211007
2021年 10月 07日 星期四 00:00:00 CST

五、连接超时

为避免同一客户端长时间占用连接,造成资源浪费,可设置相应的连接超时参数,实现控制连接访问时间

1、超时参数

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

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

Client_body_timeout:设置请求体读超时时间

2、修改配置文件

[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

六、更改进程数

1、查看核心数

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

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

[root@localhost ~]# ps aux | grep nginx
root      83547  0.0  0.0  20620  1464 ?        Ss   10月06   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     83719  0.0  0.0  23124  1452 ?        S    10月06   0:00 nginx: worker process
root      83935  0.0  0.0 112724   984 pts/0    S+   00:10   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
[root@localhost ~]# systemctl restart nginx.service

注:

01表示启用第一个CPU内核,10表示启用第二个CPU内核

worker cpu affinity 0110;表示开启两个进程,第一个进程对应着第一个CPU内核,第二个进程对应着第二个CPU内核。

2核CPU,开启4个进程

worker processes 4 ;

worker cpu affinity 01 10 01 10;

PS:开启了四个进程,它们分别对应着开启2个CPU内核

4个CPU,开启4个进程

worker processes 4 ;

worker cpu affinity 0001 0010 0100 1000;

PS:0001表示启用第一个CPU内核,0010表示启用第二个CPU内核,依此类推

七、网页压缩

1、修改配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
   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;      #压缩类型,表示哪些网页文档启用压缩功能

2、重启并验证

[root@localhost html]# systemctl restart nginx.service

八、盗链与防盗链

1、盗链配置

(1)服务端配置

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

 (2)盗链端配置

[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# vim index.html
<h1>Welcome to nginx!</h1>
<img src="http://www.car.com/car.jpg"/>
[root@localhost html]# vim /etc/hosts
192.168.32.128 www.car.com

2、防盗链配置

(1)服务端配置

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
#server下配置
        location ~*\.(jpg|gif|swf)$ {
             valid_referers *.car.com car.com;
             if ( $invalid_referer ) {
                  rewrite ^/ http://www.car.com/error.png;
                  #return 403;
              }
           } 

注:~*\.(jpg|gif|swf)$:表示匹配不区分大小写,以.jpg或.gif或.swf结尾的文件

(2)防盗链设置参数详细说明:

valid referers:设置信任的网站,即能引用相应图片的网站。

none:浏览器中Referer为空的情况,就是直接在浏览器访问图片。

blocked:referer不为空的情况,但是值被代理或防火墙删除了,这些值不以http://或者https://开头。

后面的网址或者域名:referer中包含相关字符串的网址。

if语句:如果链接的来源域名不在valid referers所列出的列表中,Sinvalid referer为1,则执行后面的操作,即进行重写或返回403页面。

九、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

总结

1、Nginx服务优化包括:隐藏版本号、修改用户和组、设置缓存时间、日志分割、连接超时。

2、Nginx服务深入优化包括:更改进程数、网页压缩、盗链与防盗链、FPM模块参数优化。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值