码迷,mamicode.com
首页 > 其他好文 > 详细

Nginx优化与防盗链技术

时间:2020-11-04 18:19:39      阅读:19      评论:0      收藏:0      [点我收藏+]

标签:mkdir   bytes   lin   客户   读取   选择   ps aux   相同   传输   

Nginx优化与防盗链

 

 

前言

为了适应企业需求,就需要考虑如何提升Nginx的性能与稳定性,这就是Nginx优化的内容,本次博客主要讲述Nginx的优化以及防盗链的部署。

一 隐藏版本号

1.1、隐藏Nginx版本号,避免安全漏洞泄露
1.2、Nginx隐藏版本号的方法
未隐藏版本号前使用curl -I(大写的i)命令检测结果

1 [root@localhost ~]# curl -I http://20.0.0.10
2 HTTP/1.1 200 OK
3 Server: nginx/1.12.2
4 Date: Fri, 16 Oct 2020 06:15:34 GMT
 
 1 vi /usr/local/nginx/conf/nginx.conf
 2 http {
 3     include       mime.types;
 4     default_type  application/octet-stream;
 5 
 6     server_tokens off; ###关闭版本号
 7 [root@localhost ~]# systemctl restart nginx
 8 
 9 [root@localhost ~]# curl -I http://20.0.0.10
10 HTTP/1.1 200 OK
11 Server: nginx
12 Date: Fri, 16 Oct 2020 06:25:37 GMT
 
 
 1 [root@localhost ~]# vi /root/nginx-1.12.2/src/core/nginx.h
 2 #define nginx_version      1012002
 3 #define NGINX_VERSION      "1.1.1" ###修改版本号
 4 #define NGINX_VER          "nginx/" NGINX_VERSION
 5 
 6 [root@localhost nginx-1.12.2]# make && make install
 7 
 8 [root@localhost ~]# curl -I http://20.0.0.10
 9 HTTP/1.1 200 OK
10 Server: nginx/1.1.1
 

二 修改Nginx用户和组

3.1、Nginx运行时进程需要有用户与组的支持,以实现对网站文件读取时进行访问控制
3.2、Nginx默认使用nobody用户账号与组账号
3.3、修改的方法
编译安装时指定用户与组

1 [root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
2 user  nginx nginx;
3 
4 [root@localhost ~]# systemctl restart nginx
5 [root@localhost ~]# ps aux | grep nginx ###查看进程信息

三、配置Nginx网页缓存时间

3.1、当nginx将网页数据返回给客户端后,可设置缓存的时间,以方便在日后进行相同内容的请求时直接返回,避免重复请求,加快了访问速度。
3.2、一般针对静态网页设置,对动态网页不设置缓存时间
3.3、设置方法
在主配置文件的location段加入expires参数

 
1 [root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
2 location / {
3             root   html;
4             index  index.html index.htm;
5             expires 1d; ###设置缓存时间为1天
6         }
7 
8 [root@localhost ~]# systemctl restart nginx
 

技术图片

四、实现Nginx的日志切割

4.1、随着Nginx运行时间增加,日志也会增加。太大的日志文件对监控是一个大灾难。所以需要定期进行日志文件的切割
4.2、Nginx自身不具备日志分割处理的功能,但可以通过Nginx信号控制功能的脚本实现日志的自动切割(Kill -HUP cat /xxx/log/nginx.pid #平滑重启nginx,类似reload)
-QUIT :结束进程;-USR1:日志分割;-USR2:平滑升级
4.3、通过Linux的计划任务周期性地进行日志切割
4.4、编写脚本进行日志切割示例

 
 1 [root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
 2 error_log  logs/error.log  info;
 3 
 4  log_format  main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘
 5                       ‘$status $body_bytes_sent "$http_referer" ‘
 6                       ‘"$http_user_agent" "$http_x_forwarded_for"‘;
 7 
 8     access_log  logs/access.log  main; ###去除前面#号
 9 
10 [root@localhost ~]# nginx -t ###检查配置文件是否正确
11 nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
12 nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
13 
14 [root@localhost ~]# vim fenge.sh
15 #!/bin/bash
16 #Filename:fenge.sh
17 d=$(date -d "-1 day" "+%Y%m%d") 
18 logs_path="/var/log/nginx"
19 pid_path="/usr/local/nginx/logs/nginx.pid" ###设置日期及路径变量
20 [ -d $logs_path ] || mkdir -p $logs_path ###自动创建日志目录
21 mv /usr/local/nginx/logs/access.log ${logs_path}/test.com-access.log-$d ###分割新的日志
22 kill -HUP $(cat $pid_path) ###生成新的日志
23 find $logs_path -mtime +30 | xargs rm -rf ###删除30天前的日志(xargs用来传递参数)
24 
25 [root@localhost ~]# chmod +x fenge.sh
26 [root@localhost ~]# ./fenge.sh
27 [root@localhost ~]# cd /var/log/nginx/
28 [root@localhost nginx]# ll
29 总用量 44
30 -rw-r--r--. 1 root root 44866 10月 16 15:11 test.com-access.log-20201015 ###运行之后生成昨天的日志
 

4.5、关于日期

 
 1 ###获取当天的的日期
 2 [root@localhost ~]# date +%Y%m%d
 3 20201016
 4 [root@localhost ~]# date
 5 2020年 10月 16日 星期五 15:40:12 CST
 6 
 7 ###昨天
 8 [root@localhost ~]# date -d "-1 day"
 9 2020年 10月 15日 星期四 15:41:09 CST
10 
11 ###明天
12 [root@localhost ~]# date -d "day"
13 2020年 10月 17日 星期六 15:41:51 CST
14 
15 ###前一天的日期
16 [root@localhost ~]# date -d "-1 day" +%d
17 15
18 
19 ###前一小时
20 [root@localhost ~]# date -d "-1 hour" +%H
21 14
22 
23 ###前一分钟
24 [root@localhost ~]# date -d "-1 min" +%M
25 48
26 
27 ###前一秒钟
28 [root@localhost ~]# date -d "-1 second" +%S
29 18
 

五、配置Nginx实现连接超时

5.1、为避免同一客户端长时间占用连接,造成资源浪费,可设置相应的连接超时参数,实现控制连接访问时间
5.2、Nginx使用keepalive_timeout来指定KeepAlive的超时时间(timeout)
5.3、指定每个TCP连接最多可以保持多长时间。Nginx的默认值是65秒,有些浏览器最多只保持60秒,
若将它设置为0,就禁止了keepalive连接。

 
1 [root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
2    #keepalive_timeout  0;
3     keepalive_timeout  180;
4     client_header_timeout 80;    ##等待客户端发送请求头的超时时间 超时会发送408错误
5     client_body_timeout 80;    ##设置请求体的读超时时间
6 
7 [root@localhost ~]# systemctl restart nginx
 

六、更改Nginx运行进程数

1、在高并发场景,需要启动更多的Nginx进程以保证快速响应,以处理用户的请求,避免造成阻塞。
2、修改配置文件的worker_processes参数
一般设为CPU的个数或者核数
在高并发情况下可设置为CPU个数或者核数的2倍
3、增加进程数,可减少了系统的开销,提升了服务速度
4、使用ps aux查看运行进程数的变化情况

 
 1 [root@localhost ~]# cat /proc/cpuinfo | grep -c "physical id" ###查看物理CPU的个数
 2 4
 3 [root@localhost ~]# ps aux | grep nginx ###查看运行行程数
 4 root       1025  0.0  0.0  20500   632 ?        Ss   16:06   0:00 nginx: master process /usr/local/nginx/sbin/nginx
 5 nginx      1030  0.0  0.0  22948  1656 ?        S    16:06   0:00 nginx: worker process
 6 root       1885  0.0  0.0 112680   984 pts/0    S+   16:13   0:00 grep --color=auto nginx
 7 
 8 [root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
 9 worker_processes  4; ###将进程数改为4
10 
11 [root@localhost ~]# systemctl restart nginx
12 [root@localhost ~]# ps aux | grep nginx ###查看运行进程数变化
13 root       1030  0.0  0.0  20500   632 ?        Ss   16:17   0:00 nginx: master process /usr/local/nginx/sbin/nginx
14 nginx      1031  0.0  0.0  22948  1408 ?        S    16:17   0:00 nginx: worker process
15 nginx      1032  0.0  0.0  22948  1408 ?        S    16:17   0:00 nginx: worker process
16 nginx      1033  0.0  0.0  22948  1408 ?        S    16:17   0:00 nginx: worker process
17 nginx      1034  0.0  0.0  22948  1408 ?        S    16:17   0:00 nginx: worker process
18 root       1731  0.0  0.0 112680   980 pts/0    S+   16:18   0:00 grep --color=auto nginx
 

七、配置Nginx实现网页压缩功能

7.1、修改配置文件

 
 1 [root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
 2  gzip  on; ###开启gzip压缩功能
 3     gzip_min_length 1k; ###压缩阈值(超过1k的文件进行压缩)
 4     gzip_buffers 4 16k; ###buffer(缓冲)大小为4个16k缓冲区大小
 5     gzip_http_version 1.1; ###压缩版本
 6     gzip_comp_level 6; ###压缩比率,最小为1,处理速度快,传输速度慢;最大为9,处理速度慢,传输速度快
 7     gzip_types text/plain application/x-javascript text/css image/jpg image/png image/gif application/xml text/javascript application/x-http-php application/javascript application/json; 
 8     gzip_vary on; ###选择支持vary header可以让前端的缓存服务器缓存经过gzip压缩的页面
 9     
10 [root@localhost ~]# vi /usr/local/nginx/html/index.html ###插入图片
11 <html><body><h1>web 1</h1><img src=a.jpg / ></body></html>
12 [root@localhost ~]# systemctl restart nginx
 

7.2、验证

技术图片

技术图片

八、Nginx防盗链设置

8.1、盗链主机配置(IP:20.0.0.30)

1 [root@localhost ~]# yum -y install httpd
2 [root@localhost ~]# vi /etc/hosts
3 20.0.0.10 www.xuhao.com

8.2、盗链网站首页配置

1 [root@server1 ~]# vi /var/www/html/index.html
2 <html><body><h1>server 2</h1><img src=http://20.0.0.10/a.jpg / ></body></html>

技术图片

8.3、源主机防盗链配置

 
1 [root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
2  location ~*\.(gif|jpg|swf)$ {
3              valid_referers none blocked *.test.com test.com;
4              if ($invalid_referer) {
5              rewrite ^/ http://www.test.com/error.png;
6              }
7     }
 

8.4、测试

技术图片

 

 技术图片

 

 技术图片

 

九、对FPM模块进行参数优化

9.1、Nginx 的 PHP 解析功能实现如果是交由 FPM 处理的,为了提高 PHP 的处理速度,可对
FPM 模块进行参数跳转。
9.2、FPM 优化参数:
pm 使用哪种方式启动 fpm 进程,可以说 static 和 dynamic,前者将产生固定数量的 fpm 进程,后者将以动态的方式产生 fpm 进程
pm.max_children :static 方式下开启的 fpm 进程数
pm.start_servers :动态方式下初始的 fpm 进程数量
pm.min_spare_servers :动态方式下最大的 fpm 空闲进程数
pm.max_spare_servers :动态方式下最大的 fpm 空闲进程数
9.3、优化原因:服务器为云服务器,运行了个人论坛,内存为1.5G,fpm进程数为20,内存消耗近1G,处理比较慢
9.4、优化参数调整
FPM启动时有5个进程,最小空闲2个进程,最大空闲8个进程,最多
可以有20个进程存在

 
 1 [root@localhost ~]# vi /usr/local/php/etc/php-fpm.d/www.conf
 2 pm = dynamic
 3 pm.max_children = 20
 4 pm.start_servers = 5
 5 pm.min_spare_servers = 2
 6 pm.max_spare_servers = 8
 7 
 8 [root@localhost ~]# /usr/local/php/sbin/php-fpm -c /usr/local/php/etc/php-fpm.d/www.conf
 9 [root@localhost ~]# netstat -ntap | grep 9000
10 tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      2094/php-fpm: maste
 

Nginx优化与防盗链技术

标签:mkdir   bytes   lin   客户   读取   选择   ps aux   相同   传输   

原文地址:https://www.cnblogs.com/wqhao/p/13922138.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!