一、隐藏版本号
未隐藏时客户机访问,并抓包
方法一:修改配置文件,并重启服务
vi /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
server_tokens off; #新增此行,关闭版本号
方法二:修改源码并重新编译安装
[root@localhost ~]# vi /root/nginx-1.12.2/src/core/nginx.h
#define nginx_version 1012002
#define NGINX_VERSION "1.1.1" ###修改版本号,并不是真实版本号
#define NGINX_VER "nginx/" NGINX_VERSION
[root@localhost nginx-1.12.2]# make && make install
[root@localhost ~]# curl -I http://192.168.73.40
HTTP/1.1 200 OK
Server: nginx/1.1.1
二、网页缓存
1、当nginx将网页数据返回给客户端后,可设置缓存的时间,以方便在日后进行相同内容的请求时直接返回,避免重复请求,加快了访问速度。
2、一般针对静态网页设置,对动态网页不设置缓存时间
3、设置方法
在主配置文件的location段加入expires参数,并重启服务
location / {
root html;
index index.html index.htm;
expires 1d; #新增此行,设置缓存时间一天
}
访问并抓包
三、网页压缩
修改配置文件,并重启服务
http {
gzip on; #取消注释,开启gzip压缩功能,新增下面几行
gzip_min_length 1k; #
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 6;
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;
gzip_vary on;
四、连接超时
1、为避免同一客户端长时间占用连接,造成资源浪费,可设置相应的连接超时参数,实现控制连接访问时间
2、Nginx使用keepalive_timeout来指定KeepAlive的超时时间(timeout)
3、指定每个TCP连接最多可以保持多长时间。Nginx的默认值是65秒,有些浏览器最多只保持60秒,
若将它设置为0,就禁止了keepalive连接。
vi /usr/local/nginx/conf/nginx.conf
#keepalive_timeout 0;
keepalive_timeout 180;
client_header_timeout 80; ##等待客户端发送请求头的超时时间 超时会发送408错误
client_body_timeout 80; ##设置请求体的读超时时间
五、修改进程数
1、在高并发场景,需要启动更多的Nginx进程以保证快速响应,以处理用户的请求,避免造成阻塞。
2、修改配置文件的worker_processes参数
一般设为CPU的个数或者核数
在高并发情况下可设置为CPU个数或者核数的2倍
3、增加进程数,可减少了系统的开销,提升了服务速度
4、使用ps aux查看运行进程数的变化情况
[root@client1 html]# cat /proc/cpuinfo | grep -c "physical id"
2
[root@client1 html]# ps aux | grep nginx
root 11825 0.0 0.0 20492 608 ? Ss 11:49 0:00 nginx: master process nginx
nginx 11826 0.0 0.0 23020 1632 ? S 11:49 0:00 nginx: worker process
root 11886 0.0 0.0 112676 980 pts/0 S+ 11:53 0:00 grep --color=auto nginx
[root@client1 html]# vi /etc/nginx.conf
worker_processes 4; ###将进程数1改为4
[root@client1 html]# killall -s QUIT nginx
[root@client1 html]# nginx
[root@client1 html]# ps aux | grep nginx
root 11914 0.0 0.0 20492 612 ? Ss 11:53 0:00 nginx: master process nginx
nginx 11915 0.0 0.0 23020 1372 ? S 11:53 0:00 nginx: worker process
nginx 11916 0.0 0.0 23020 1360 ? S 11:53 0:00 nginx: worker process
nginx 11917 0.0 0.0 23020 1384 ? S 11:53 0:00 nginx: worker process
nginx 11918 0.0 0.0 23020 1384 ? S 11:53 0:00 nginx: worker process
root 11920 0.0 0.0 112676 980 pts/0 S+ 11:54 0:00 grep --color=auto nginx
六、日志分割
1、随着Nginx运行时间增加,日志也会增加。太大的日志文件对监控是一个大灾难。所以需要定期进行日志文件的切割
2、Nginx自身不具备日志分割处理的功能,但可以通过Nginx信号控制功能的脚本实现日志的自动切割(Kill -HUP cat /xxx/log/nginx.pid #平滑重启nginx,类似reload)
-QUIT :结束进程;-USR1:日志分割;-USR2:平滑升级
3、通过Linux的计划任务周期性地进行日志切割
4、编写脚本进行日志切割示例
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
error_log logs/error.log info;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main; ###去除前面#号
[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
[root@localhost ~]# 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) ###生成新的日志
find $logs_path -mtime +30 | xargs rm -rf ###删除30天前的日志(xargs用来传递参数)
[root@localhost ~]# chmod +x fenge.sh
[root@localhost ~]# ./fenge.sh
[root@localhost ~]# cd /var/log/nginx/
[root@localhost nginx]# ll
总用量 44
-rw-r--r--. 1 root root 44866 10月 16 15:11 test.com-access.log-20201015 ###运行之后生成昨天的日志
七、防盗链
源主机网页
[root@localhost ~]# vi /etc/hosts
192.168.247.150 www.zf.com
盗链网站
[root@server1 ~]# vi /var/www/html/index.html
<html><body><h1>This is Copy!</h1><img rc=http://192.168.247.150></body></html>
防盗链配置,并重启服务
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
location ~*\.(gif|jpg|swf)$ {
valid_referers none blocked *.zf.com zf.com;
if ($invalid_referer) {
rewrite ^/ http://www.zf.com/error.png; #errpr.png在网页目录下
}
}
测试,再访问盗链网站
八、FPM模块参数优化
1、Nginx 的 PHP 解析功能实现如果是交由 FPM 处理的,为了提高 PHP 的处理速度,可对
FPM 模块进行参数跳转。
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 空闲进程数
3、优化原因:服务器为云服务器,运行了个人论坛,内存为1.5G,fpm进程数为20,内存消耗近1G,处理比较慢
4、优化参数调整
FPM启动时有5个进程,最小空闲2个进程,最大空闲8个进程,最多可以有20个进程存在
[root@localhost ~]# vi /usr/local/php/etc/php-fpm.d/www.conf
pm = dynamic
pm.max_children = 20
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
[root@localhost ~]# /usr/local/php/sbin/php-fpm -c /usr/local/php/etc/php-fpm.d/www.conf
[root@localhost ~]# netstat -ntap | grep 9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 2094/php-fpm: maste