目录
一、Nginx 服务优化
1、查看方法
2.浏览器查看
二、隐藏版本号
1、查看版本号
2、隐藏版本号
3、修改源码文件,重新编译安装??
三、修改用户和组?
四、缓存时间
五、日志切割
六、连接超时
七、更改进程数
八、配置网页压缩
九、配置防盗链
1、防盗链主机(服务端192.168.100.10)
2、盗链主机(客户机192.168.100.20)?
3、盗链主机进行验证(192.168.100.20)
十、fpm优化
一、Nginx 服务优化
在企业信息化应用环境中,服务器的安全性和响应速度需要根据实际情况进行响应参数配置,以达到最优的用户体验。 默认的?Nginx?安装参数只能提供最基本的服务,还需要调如网页缓存时间、连接超时、网页压缩等响应参数,才能发挥出服务器的最大作用。
1、查看方法
?curl 命令
curl -l(小写)?http://192.168.100.20查看的是头部信息
curl -I(大写)

2.浏览器查看
浏览器 → 开发者工具(F12) → 选择network → 刷新页面(Ctrl +r) → 选择请求 → 选择 headlers → 查看版本
二、隐藏版本号
1、查看版本号

?curl -I http://192.168.100.20

本地查看

2、隐藏版本号
ginx服务开启之后,在网页访问时,按F12键,就可以看到nginx的版本号,这样安全性不够高,我们可以通过隐藏版本号或者修改版本名称来进行提高安全性。
vim /usr/local/nginx/conf/nginx.conf http { ? ? include ? ? ? mime.types; ? ? default_type ?application/octet-stream; ? ? server_tokens off;?? ??? ??? ??? ??? ??? ??? ??? ?#添加,关闭版本号 ? ? ...... }
systemctl restart nginx ? ?#重启服务 curl -I http://192.168.100.20? ?#查看版本号 ??



3、修改源码文件,重新编译安装??
vim /opt/nginx-1.20.2/src/core/nginx.h?
?13 #define NGINX_VERSION ? ? ?"1.22.0"? ? ?#修改版本号名称? “ ” ?14 #define NGINX_VER ? ? ? ? ?"nginx/" NGINX_VERSION? ? ?#修改服务器类型

需要重新编译
cd /opt/lnmp/nginx-1.22.0/?
./configure \ --prefix=/usr/local/nginx \ --user=nginx \ --group=nginx \ --with-http_stub_status_module
?
make && make install? ?安装
vim /usr/local/nginx/conf/nginx.conf? ?修改版本号



三、修改用户和组?
修改前

vim /usr/local/nginx/conf/nginx.conf user nginx nginx;
systemctl restart nginx?
ps aux |grep nginx ?#主进程由root创建,子进程由nginx创建

?修改后

四、缓存时间
缓存时间表示,当客户端访问nginx服务器时,可以设置缓存时间,假如设置了1天。表示客户端再一天内访问该页面,将不会再向nginx服务器发送连接请求,而是直接从自己的缓存中读取就行,超过一天,就需要重新申请连接,可以设置天,时,分等。
vim /usr/local/nginx/conf/nginx.conf http { ...... ?? ?server { ?? ?......? ?? ??? ?location / { ?? ??? ??? ?root html; ?? ??? ??? ?index index.html index.htm; ?? ??? ?} ?? ??? ? ?? ??? ?location ~ \.(gif|jpg|jepg|png|bmp|ico)$ { ?? ??? ?#加入新的 location,以图片作为缓存对象 ?? ??? ??? ?root html; ?? ??? ??? ?expires 1d;?? ??? ??? ??? ??? ??? ??? ??? ??? ?#指定缓存时间,1天 ?? ??? ?} ...... ?? ?} }


网页查看

五、日志切割
vim /opt/jiaoben/rizhi.sh #!/bin/bash
#显示前一天的时间?+%F
d=$(date -d "-1 day" "+%Y%m%d") ? ? ? ? ? ? ? ?
#日志目录?
logs_path="/var/log/nginx"
#nginx进程 pid_path=`cat /usr/local/nginx/logs/nginx.pid`
[ -d $logs_path ] || mkdir -p $logs_path ? ? ? ?#创建日志文件目录
#移动并重命名日志文件 mv /usr/local/nginx/logs/access.log ${logs_path}/gang.com-access.log-{$d}
#通过 kill 或 killall 命令发送 HUP 信号表示重载配置 kill -USR1 $pid_path? #删除30天前的日志文件 ? ? ? ? ? ? ? ? ?? find $logs_path -mtime +30 -exec rm -rf {} \; #find $logs_path -mtime +30 |xargs rm -rf


crontab -e 0 1 * * * /opt/jiaoben/rizhi.sh ? #设置周期性计划
atime (access ?time) ? #最后一次访问文件或目录的时间
mtime (modify time) ?#最后一次修改文件或目录(内容)的时间
ctime ?(change ?time) ?#最后一次改变文件或目录(属性)的时间
六、连接超时
keepalive_timeout(tcp连接三次握手的超时时间) 指定keepalive的超时时间(timeout),指定每个TCP连接最多可保持多长时间,服务器将会在这个时间后关闭连接,nginx的默认值是65秒,有些浏览器最多只保持60秒,所以可设定为60秒,若将它设置为0,就禁止了keepalive连接。 第二个参数(可选的)指定了再相应头keep-alive:timeout=time中的time值。这个头能让一些浏览器主动关闭连接,这样服务器就不必去关闭连接了。没有这个参数,nginx不会发送keep-alive相应头。
client_header_timeout(客户端请求头的超时时间) 客户端向服务器发送一个完整的request header(请求头)的超时时间,如果客户端再指定时间内没有发送一个完整的request header,nginx返回httpd408的返回码(request timed out:请求超时)
clent_body_timeout(客户端请求体的超时时间) 指定客户端与服务器建立连接后发送request body的超时时间,如果客户端在指定时间内没有发送任何内容,nginx返回HTTPD408(request timed out)
HTTP有一个KeepAlive模式,它告诉web服务器在处理完一个请求后保持这个TCP连接的打开状态。若接收到来自客户端的其它请求,服务端会利用这个未被关闭的连接,而不需要再建立一个连接。 ?KeepAlive 在一段时间内保持打开状态,它们会在这段时间内占用资源。占用过多就会影响性能
vim /usr/local/nginx/conf/nginx.conf http { ......? ? ? keepalive_timeout 65 180;? ?#指定每个tcp连接超时时间65秒 ? ? client_header_timeout 80;? ?#请求头的超时时间为80秒 ? ? client_body_timeout 80;? ? ??#请求体的超时时间为80秒 ......? }
systemctl restart nginx 重启nginx


七、更改进程数
cat /proc/cpuinfo |grep -c "physical id" #查看cpu核数 ps aux |grep nginx ?#查看nginx主进程中包含几个子进程
vim /usr/local/nginx/conf/nginx.conf worker_processes 2; ? ?#修改为核数相同或者2倍 worker_cpu_affinity 01 10; ? #设置每个进程由不同cpu处理,进程数配为4时0001 ?0010 0100 1000
systemctl restart nginx ? #重启nginx服务

八、配置网页压缩
vim /usr/local/nginx/conf/nginx.conf http { ......? ? ?gzip on; ? ? ? #取消注释,开启gzip压缩功能 ? ?gzip_min_length 1k; ? ? ? ?#最小压缩文件大小 ? ?gzip_buffers 4 64k; ? ? ? ?#压缩缓冲区,大小为4个64k缓冲区 ? ?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; ?#压缩类型,表示哪些网页文档启用压缩功能 ......? }

cd /usr/local/nginx/html #先将123.jpg文件传到/usr/local/nginx/html目录下 vim index.html ......? <img src="game.jpg"/>?? ??? ??? ??? ?#网页中插入图片 </body> </html>
?


九、配置防盗链
vim /usr/local/nginx/conf/nginx.conf http { ...... ? server { ? ...... ? ?location ~* \.(gif|jpg|jpeg|bmp|ico)$ {? ? ? ? ? ? ? ? ?valid_referers *.gang.com gang.com; ? ? ? ? ? ?if ( $invalid_referer) { ? ? ? ? ? ? ? rewrite ^/ http://www.gang.com/error.png; ? ? ? ? } ? ? ? ? }
? ...... ? } }
?valid_referers :设置信任的网站,可以正常使用图片;? ?后面的网址或者域名 :referer 中包含相关字符串的网址; if语句:如果链接的来源域名不在valid_referers所列出的列表中,$invalid_referer为1,则执行后面的操作,即进行重写或返回403 页面
1、防盗链主机(服务端192.168.100.10)
cd /usr/local/nginx/html?
vim 1.jpg



vim /usr/local/nginx/conf/nginx.conf

2、盗链主机(客户机192.168.100.20)?

vim /usr/local/nginx/html/index.html


3、盗链主机进行验证(192.168.100.20)

十、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 | grep 9000
|