时间:2022-10-11来源:系统城装机大师作者:佚名
在Nginx的ngx_http_gzip_module压缩模块提供对文件内容压缩的功能。进行相关的配置修改,就能实现Nginx页面的压缩,达到节约带宽,提升用户访问速度
| 1 2 3 4 5 6 7 8 9 10 11 |
vim /usr/local/nginx/conf/nginx.confhttp {..........gzip on; gzip_min_length 1k; gzip_buffers 4 64k; gzip_http_version 1.1; 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;} |

重启服务,进行访问测试:

当Nginx将网页数据返回给客户端后,可设置缓存的时间,以方便在日后进行相同内容的请求时直接返回,避免重复请求,加快了访问速度。
一般针对静态网页设置,对动态网页不设置缓存时间。
| 1 2 3 4 5 6 7 8 |
vim /usr/local/nginx/conf/nginx.confhttp {................. location ~* \.(gif|jpg|jepg|bmp|ico)$ { root html; expires 1d; }} |

重启服务,测试访问:

| 1 2 3 4 5 6 7 8 |
vim /usr/local/nginx/conf/nginx.conf http { ...... keepalive_timeout 65 180; //设置连接超时时间 client_header_timeout 80; client_body_timeout 80; ...... } |

重启服务,访问测试:

在高并发场景,需要启动更多的Nginx进程以保证快速响应,以处理用户的请求,避免造成阻塞。
查看cpu的核心数,根据核心数来设置工作进程数
| 1 2 3 4 5 6 |
#1、查看cpu核数cat /proc/cpuinfo |grep processor|wc -l或cat /proc/cpuinfo |grep -c processor或cat /proc/cpuinfo | grep -c "physical id" |

| 1 | [root@localhost html]#ps aux | grep nginx |

修改工作进程核心数 :
| 1 2 3 4 |
vim /usr/local/nginx/conf/nginx.conf worker_processes 2; #修改为与CPU核数相同 worker_cpu_affinity 01 10; #设置每个进程由不同cpu处理,进程数配为4时0001 0010 0100 1000 |

测试结果:
| 1 | [root@localhost html]#ps aux | grep nginx |

——隐藏Nginx的版本号
| 1 | [root@localhost ~]#curl -I 192.168.73.105 |

火狐访问浏览器 ,F12查看网络信息

方法一:修改配置文件,关闭版本号
| 1 2 3 4 5 6 7 |
vim /usr/local/nginx/conf/nginx.confhttp { include mime.types; default_type application/octet-stream; server_tokens off; #添加这一行,关闭版本号 ......} |

测试结果:


方法二:修改源码文件中的版本号,重新编译安装
| 1 2 3 4 5 6 7 8 |
cd /opt/nginx-1.12.0/src/core/#修改前进行备份cp nginx.h nginx.h.bak vimn ginx.h #define NGINX_VERSION "1.12.0"#define NGINX_VER "nginx/" NGINX_VERSION |

修改完配置后切换到Nginx软件包中,进行重新编译安装 :
| 1 2 3 4 |
cd /opt/nginx-1.12.0/./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module make -J 2 && make install |
再次修改主配置文件,打开版本号显示 :
| 1 2 3 4 5 6 7 |
vim /usr/local/nginx/conf/nginx.conf http { include mime.types; default_type application/octet-stream; server_tokens on; ...... } |

重启服务,进行测试:

Nginx与apache的不同之处,就是Nginx本身并为设计日志分割工具,所以需要运维人员进行脚本编写来实现日志分割
编写日志分割脚本
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
cd /optvim cutlogs.sh #!/bin/bash #nginx 分割日志脚本#用变量day获取前天日期的时间记录day=$(date -d "-1 day" "+%Y%m%d")#获取日志的目录logs_path="/var/log/nginx"#获取运行时nginx的进程号pid_path="/usr/local/nginx/logs/nginx.pid"#二元表达式,如果前面不成立则执行后面的式子#前面判断该目录是否存在,后面则表示不存在则自动创建该目录[ -d $logs_path ] || mkdir -p $logs_path#将生成的日志按照date生成的时间格式改名并移动到指定的路径中保存mv /usr/local/nginx/logs/access.log ${logs_path}/access.log-$day#重新生成一个新的日志kill -USR1 $(cat $pid_path)#日志文件清理,将30天前的日志进行清除find $logs_path -mtime +30 -exec rm -rf {} \ ; |

2. 执行脚本进行测试

3. 将日志脚本添加至计划性任务
| 1 2 3 4 |
[root@localhost opt]#vim cutlogs.sh[root@localhost opt]#chmod +x cutlogs.sh [root@localhost opt]#crontab -e0 1 * * * /opt/cutlogs.sh |


Nginx盗链的过程与apache一致,均是通过网页浏览,将网站图片重定向到自己的网站上
https://www.jb51.net/article/264450.htm
修改主配配置文件,添加防盗链设置
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
vim /usr/local/nginx/conf/nginx.confhttp {...........server{...........location ~* \.(jpg|gif|swf)$ { root html; expires 1d; valid_referers none blocked *.test.com test.com; if ( $invalid_referer ) { rewrite ^/ http://www.test.com/error.png; } }............}...............} |

在 /var/local/nginx/html 下放置好error.png

进行盗链测试
盗链主机html网页设置:

第三方用户访问盗链主机:
访问前设置:
1.关闭防火墙工具firewalld和selinux
2.将域名对应的IP添加到 /etc/hosts 中

第三方访问原主机 :

Nginx的PHP解析功能实现如果是交由FPM处理的,为了提高PHP的处理速度,可对FPM模块进行参数的调整。
根据服务器的内存与服务负载,调整FPM模块参数。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
vim /usr/local/php/etc/php-fpm.conf pid = run/php-fpm.pidvim /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 #动态方式下最大空闲进程数 kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid` #重启php-fpmnetstat -anpt | grep 9000 |
到此这篇关于Nginx的优化、安全与防盗链的文章就介绍到这了
2024-07-07
myeclipse怎么导入tomcat教程2024-07-07
myeclipse如何启动tomcat2024-07-07
myeclipse如何绑定tomcat上线了一个小的预约程序,配置通过Nginx进行访问入口,默认的日志是没有请求时间的,因此需要配置一下,将每一次的请求的访问响应时间记录出来,备查与优化使用....
2023-03-17