文章目录
- 一:配置nginx隐藏版本号
- 二:修改配置文件法
- 三:修改源码法
- 四:修改nginx用户与组
- 五:配置nginx网页缓存时间
- 六 :实现nginx的日志切割
- 七:配置nginx实现连接超时
- 八:更改nginx运行进程数
- 8.1 在高并发场景,需要启动更多的nginx进程以保证快速响应,以处理用户的请求,避免造成阻塞
- 8.2 可以使用ps aux 命令查看nginx运行进程的个数
- 8.3 更改进程数的配置方法
- 8.4 修改配置文件的worker_processes参数
- 8.5 运行进程数多一些,响应访问请求时,nginx就不会临时启动新的进程提供服务,减少了系统的开销,提升了服务速度
- 8.6 使用ps aux 查看运行进程数的变化情况
- 8.7 默认情况,nginx的多个进程可能跑在一个CPU上,可以分配不同的进程给不同的CPU处理,充分利用硬件多核多CPU
- 8.8 例如:在一台4核物理服务器,可进行以下配置,将进程进行分配
- 8.9 演示
- 九:配置nginx实现网页压缩功能
- 十:配置nginx实现防盗链
- 十一: 对FPM模块进行参数优化
前言:
nginx常规的优化
nginx深入优化
一:配置nginx隐藏版本号
1.1 在生产环境中,需要隐藏nginx的版本号,以避免安全漏洞的泄露
1.2 查看方法
使用fidder工具在windows客户端查看nginx版本号
在centos系统中使用“curl -l 网址”命令查看
1.3 nginx隐藏版本号的方法
修改配置文件法
修改源码法
二:修改配置文件法
2.1 nginx的配置文件中的server_tokens 选项的值设置为off
2.2 重启服务,访问网站使用curl -l 命令检测
2.3 若PHP配置文件中配置了fastcgi_param SERVER_SOFTWARE选项,则编辑php-fpm配置文件,将fastcgi_param SERVER_SOFTWARW nginx
- fastcgi_param SERVER_SOFTWARE nginx;
2.4 演示:
2.4.1 curl -I 查看当前网址信息
[root@localhost conf]# curl -I http://192.168.247.190
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Tue, 24 Dec 2019 00:44:45 GMT
Content-Type: text/html
Content-Length: 16
Last-Modified: Thu, 19 Dec 2019 09:39:46 GMT
Connection: keep-alive
ETag: "5dfb4562-10"
Accept-Ranges: bytes
[root@localhost conf]#
2.4.2 修改配置文件,增加关闭版本的参数,重启服务,再次查看
[root@localhost conf]# vim nginx.conf
http {
‘ server_tokens off;
//增加上面参数
[root@localhost conf]# service nginx stop
[root@localhost conf]# service nginx start
[root@localhost conf]# curl -I http://192.168.247.190
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 24 Dec 2019 00:47:48 GMT
Content-Type: text/html
Content-Length: 16
Last-Modified: Thu, 19 Dec 2019 09:39:46 GMT
Connection: keep-alive
ETag: "5dfb4562-10"
Accept-Ranges: bytes
三:修改源码法
3.1 nginx源码文件/usr/src/nginx-1.12.0/src/core/nginx.h包含了版本信息,可以随意设置
3.2 重新编译安装,隐藏版本信息
3.3 示例:
- #define NGINX_VERSION “1.1.1” ,修改版本号为1.1.1
- #define NGINX_VER “IIS/” ,修改软件类型为IIS
3.4 重启服务,访问网站使用curl -l 命令检测
备注:最好在安装编译之前,修改
3.5 演示
3.5.1 首先需要先把版本打开可见
[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf
server_tokens on;
//开启版本
3.5.2 然后到源码包内修改版本
[root@localhost conf]# cd /usr/local/nginx/
[root@localhost nginx]# ls
client_body_temp fastcgi_temp logs sbin uwsgi_temp
conf html proxy_temp scgi_temp
[root@localhost nginx]# cd conf/
[root@localhost conf]# ls
fastcgi.conf koi-utf nginx.conf scgi_params.default
fastcgi.conf.default koi-win nginx.conf.bak uwsgi_params
fastcgi_params mime.types nginx.conf.default uwsgi_params.default
fastcgi_params.default mime.types.default scgi_params win-utf
[root@localhost conf]# cd /opt/nginx-1.12.2/
[root@localhost nginx-1.12.2]# ls
auto CHANGES.ru configure html Makefile objs src
CHANGES conf contrib LICENSE man README
[root@localhost nginx-1.12.2]# cd src/
[root@localhost src]# ls
core event http mail misc os stream
[root@localhost src]# cd core/
//core 代表内核
[root@localhost core]# ls
nginx.c ngx_cycle.h ngx_output_chain.c ngx_rwlock.c
nginx.h ngx_file.c ngx_palloc.c ngx_rwlock.h
ngx_array.c ngx_file.h ngx_palloc.h ngx_sha1.c
ngx_array.h ngx_hash.c ngx_parse.c ngx_sha1.h
ngx_buf.c ngx_hash.h ngx_parse.h ngx_shmtx.c
ngx_buf.h ngx_inet.c ngx_parse_time.c ngx_shmtx.h
ngx_conf_file.c ngx_inet.h ngx_parse_time.h ngx_slab.c
ngx_conf_file.h ngx_list.c ngx_proxy_protocol.c ngx_slab.h
ngx_config.h ngx_list.h ngx_proxy_protocol.h ngx_spinlock.c
ngx_connection.c ngx_log.c ngx_queue.c ngx_string.c
ngx_connection.h ngx_log.h ngx_queue.h ngx_string.h
ngx_core.h ngx_md5.c ngx_radix_tree.c ngx_syslog.c
ngx_cpuinfo.c ngx_md5.h ngx_radix_tree.h ngx_syslog.h
ngx_crc32.c ngx_module.c ngx_rbtree.c ngx_thread_pool.c
ngx_crc32.h ngx_module.h ngx_rbtree.h ngx_thread_pool.h
ngx_crc.h ngx_murmurhash.c ngx_regex.c ngx_times.c
ngx_crypt.c ngx_murmurhash.h ngx_regex.h ngx_times.h
ngx_crypt.h ngx_open_file_cache.c ngx_resolver.c
ngx_cycle.c ngx_open_file_cache.h ngx_resolver.h
[root@localhost core]# vim nginx.h
13 #define NGINX_VERSION "8.8.8"
[root@localhost core]# cd ../../
[root@localhost nginx-1.12.2]# ls
auto CHANGES.ru configure html Makefile objs src
CHANGES conf contrib LICENSE man README
3.5.3 改完内核,后,需要重新编译安装
[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]# vim /usr/local/nginx/conf/nginx.conf
server_tokens on;
3.5.4 然后重启服务,再次查看
[root@localhost nginx-1.12.2]# service nginx stop
[root@localhost nginx-1.12.2]# service nginx start
[root@localhost nginx-1.12.2]# curl -I http://192.168.247.190
HTTP/1.1 200 OK
Server: nginx/8.8.8
Date: Tue, 24 Dec 2019 00:58:48 GMT
Content-Type: text/html
Content-Length: 16
Last-Modified: Thu, 19 Dec 2019 09:39:46 GMT
Connection: keep-alive
ETag: "5dfb4562-10"
Accept-Ranges: bytes
四:修改nginx用户与组
4.1 nginx运行时进程需要有用户与组的支持,以实现 对网站文件读取时进行访问控制
4.2 nginx默认使用nobody用户账号与组账号,一般也要进行修改
4.3 修改的方法
- 编译安装时指定用户与组
- 修改配置文件指定用户与组
4.4 修改配置文件法指定
- 新建用户账号,如nginx
- 修改主配置文件user选项,指定用户账号
- 重启那个nginx服务,使配置生效
- 使用ps aux 命令查看nginx的进程信息,验证运行用户账号改变效果
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
events {
worker_connections 1024;
}
’ user nginx nginx;
//新增
http {
[root@localhost html]# service nginx stop
[root@localhost html]# service nginx start
[root@localhost html]# ps aux | grep nginx
root 8560 0.0 0.0 20548 648 ? Ss 09:37 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 8562 0.0 0.0 23012 1672 ? S 09:37 0:00 nginx: worker process
root 8632 0.0 0.0 112728 972 pts/0 R+ 09:41 0:00 grep --color=auto nginx
五:配置nginx网页缓存时间
5.1 当nginx将网页数据返回给客户端后,可设置缓存的时间,以方便在日后进行相同内容的请求时直接返回,避免重复请求,加快了访问速度
5.2 一般针对静态网页设置,对动态网页不设置缓存时间
5.3 可在windows客户端中使用fidder查看网页缓存时间
5.4 设置方法
- 可修改配置文件,在http段、或者server段、或者location段加入对特定内容的过期参数
5.5 示例
- 修改nginx的配置文件,在location段加入expires参数
location ~\.(gif|jpeg|jpg|ico|bmp|png)$ {
root html;
expires 1d;
}
5.6 演示
5.6.1 复制照片到站点,编辑首页,在首页内加入照片
[root@localhost abc]# cp qq.jpg /usr/local/nginx/html/
[root@localhost abc]# cd /usr/local/
[root@localhost local]# ls
bin etc games include lib lib64 libexec nginx sbin share src
[root@localhost local]# cd nginx/html/
[root@localhost html]# ls
50x.html index.html qq.jpg
[root@localhost html]# vim index.html
15 <img src="qq.jpg"/>
[root@localhost html]# service nginx stop
[root@localhost html]# service nginx start
5.6.2 还未设置缓存时间前,先看一下
5.6.3 修改配置文件,设置缓存时间,重启服务
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
location ~\.(gif|jpeg|jpg|ico|bmp|png)$ {
root html;
expires 1d;
}
[root@localhost html]# service nginx stop
[root@localhost html]# service nginx start
5.6.4 重启服务,抓包查看验证
六 :实现nginx的日志切割
6.1 随着nginx运行时间增加,日志也会增加。为了方便掌握nginx运行状态,需要时刻关注nginx日志文件
6.2 太大的日志文件对监控是一个大灾难
- 定期进行日志文件的切割
6.3 nginx自身不具备日志分割处理的功能,但可以通过nginx信号控制功能的脚本实现日志的自动切割,并通过linux的计划任务周期性地进行日志切割
6.4 编写脚本进行日志切割的思路
- 设置时间变量
- 设置保存日志路径
- 将目前的日志文件进行重命名
- 删除时间过长的日志文件
- 设置cron任务,定期执行脚本自动进行日志分割
xargs 参数,是指把前面的操作结果作为后面的参数
6.5 演示
6.5.1 测试date
[root@localhost html]# date
2019年 12月 24日 星期二 10:38:38 CST
[root@localhost html]# date -d "0 day" "+%Y%m%d"
20191224
[root@localhost html]# date -d "-1 day" "+%Y%m%d"
20191223
[root@localhost html]# date -d "+1 day" "+%Y%m%d"
20191225
6.5.2 编写脚本
[root@localhost html]# 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}/tase.cpm-access.log-$d
kill -USR1 $(cat $pid_path)
find $logs_path -mtime +30 | xargs rm -rf
6.5.3 运行脚本
[root@localhost html]# cd /opt
[root@localhost opt]# chmod +x fenge.sh
[root@localhost opt]# ./fenge.sh
[root@localhost opt]# cd /var/log/nginx/
[root@localhost nginx]# ls
tase.cpm-access.log-20191223
[root@localhost nginx]# date
2019年 12月 24日 星期二 14:31:38 CST
[root@localhost nginx]# date -s 2019-12-25
2019年 12月 25日 星期三 00:00:00 CST
[root@localhost nginx]# cd -
/opt
[root@localhost opt]# ./fenge.sh
[root@localhost opt]# cd -
/var/log/nginx
[root@localhost nginx]# ls
tase.cpm-access.log-20191223 tase.cpm-access.log-20191224
[root@localhost nginx]# cd /usr/local/nginx/logs/
[root@localhost logs]# ls
access.log error.log kgc8080 nginx.pid
[root@localhost logs]#
七:配置nginx实现连接超时
7.1 在企业网站中,为了避免同一个客户长时间占用链接,造成资源浪费,可设置相应的连接超时参数,实现控制连接访问时间
7.2 使用fiddler工具查看connection参数
7.3 超时参数讲解
- Keepalive_timeout
- 设置连接保持超时时间,一般可只设置该参数,默认为75秒,可根据网站的情况设置,或者关闭;可在http段、server段、或者location段设置
- Client_header_timeout
- 指定等待客户端发送请求头的超时时间
- Client_body_timeout
- 设置请求体的超时时间
7.4 演示
7.4.1 修改配置文件
[root@localhost html]# cd /usr/local/nginx/conf/
[root@localhost conf]# ls
fastcgi.conf koi-utf nginx.conf scgi_params.default
fastcgi.conf.default koi-win nginx.conf.bak uwsgi_params
fastcgi_params mime.types nginx.conf.default uwsgi_params.default
fastcgi_params.default mime.types.default scgi_params win-utf
[root@localhost conf]#
[root@localhost conf]# vim nginx.conf
keepalive_timeout 65 180;
client_header_timeout 80;
client_body_timeout 80;
[root@localhost conf]# service nginx stop
[root@localhost conf]# service nginx start
第一个是客户端的超时时间,第二个是服务端的超时时间,参数放在http中
以上就是进行超时时间的设置
八:更改nginx运行进程数
8.1 在高并发场景,需要启动更多的nginx进程以保证快速响应,以处理用户的请求,避免造成阻塞
8.2 可以使用ps aux 命令查看nginx运行进程的个数
8.3 更改进程数的配置方法
- 修改配置文件,修改进程数配置参数
8.4 修改配置文件的worker_processes参数
- 一般设为CPU的个数或者核数
- 在高并发情况下可设置为CPU个数或者核数的2倍
8.5 运行进程数多一些,响应访问请求时,nginx就不会临时启动新的进程提供服务,减少了系统的开销,提升了服务速度
8.6 使用ps aux 查看运行进程数的变化情况
8.7 默认情况,nginx的多个进程可能跑在一个CPU上,可以分配不同的进程给不同的CPU处理,充分利用硬件多核多CPU
8.8 例如:在一台4核物理服务器,可进行以下配置,将进程进行分配
- Worker_cpu_affinity 0001 0010 0100 1000
8.9 演示
8.9.1 先查看nginx的进程,worker工作的是一个
[root@localhost conf]# ps aux |grep nginx
root 8792 0.0 0.0 20548 644 ? Ss 09:48 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 8794 0.0 0.0 23012 1420 ? S 09:48 0:00 nginx: worker process
root 8833 0.0 0.0 112728 972 pts/0 S+ 09:51 0:00 grep --color=auto nginx
[root@localhost conf]#
其中,grep的进程不用看,master是主进程,但是它不干活,干活的是worker,现在只有一个worker,原因看配置文件
[root@localhost conf]# vim nginx.conf
worker_processes 1;
此处设置为1个进程数
worker_connections 1024;
一个进程数可以完成1024个工作量
8.9.2 如果想要优化配置,需要对硬件进行升级
垂直扩展
关闭服务器,init 0
然后扩展服务器cpu
原本
然后重新开启服务器
[root@localhost ~]# cd /proc
[root@localhost proc]# cat cpuinfo
processor : 0
processor : 1
8.9.3 修改配置文件
[root@localhost proc]# vim /usr/local/nginx/conf/nginx.conf
worker_processes 2;
worker_cpu_affinity 01 10;
//增加上面一行
8.9.4 再次查看进程,worker此时变成两个
[root@localhost proc]# service nginx start
[root@localhost proc]# ps aux | grep nginx
root 2647 0.0 0.0 20548 648 ? Ss 10:06 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 2649 0.0 0.0 23012 1412 ? S 10:06 0:00 nginx: worker process
nginx 2650 0.0 0.0 23012 1420 ? S 10:06 0:00 nginx: worker process
root 2665 0.0 0.0 112728 972 pts/0 S+ 10:07 0:00 grep --color=auto nginx
九:配置nginx实现网页压缩功能
9.1 nginx的ngx_http_gzip_module压缩模块提供对文件内容压缩的功能
9.2 允许nginx服务器将输出内容在发送客户端之间进行压缩,已解决网站带宽,提升用户的访问体验,默认已经安装
9.3 可在配置文件中加入相应的压缩功能参数对压缩性能进行优化
9.4 压缩功能参数讲解
- gzip on :开启gzip压缩输出
- gzip_min_length 1k : 表示申请4个单位为16k的内存作为压缩结果流缓存,默认值是申请与原始数据大小相同的内存空间来存储gzip压缩结果
- zip_http_version 1.0 : 用于设置识别http协议版本,默认是1.1,目前大部分浏览器已经支持gzip解压,但处理最慢,也比较消耗服务器CPU资源
length 指超过1K内容的网页才进行压缩
buffers 缓存区
版本基本上可以不设
-
gzip_comp_level 2 :用来指定gzip压缩比,1压缩比最小,处理速度最快;9压缩比最大,传输速度快,但处理速度最快,使用默认即可
-
gzip_types text/plain : 压缩类型,是就对哪些网页文档启用压缩功能
-
gzip_vary on :选项可以让前端的缓存服务器缓存经过gzip压缩的页面
9.5 将以上的压缩功能参数加入到主配置文件httpd配置中段
9.6 重启服务,并用fiddler工具查看开启结果
9.7 演示
9.7.1 修改配置文件
[root@localhost proc]# vim /usr/local/nginx/conf/nginx.conf
gzip on;
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/j
peg image/png image/gif application/xml text/javascript application/x-httpd-p
hp application/javascript application/json;
gzip_disable "MSIE [1-6]\.";
gzip_vary on;
9.7.2 在首页中放入图片验证,重启服务
[root@localhost html]# service nginx stop
[root@localhost html]# service nginx start
[root@localhost html]# systemctl stop firewalld.service
[root@localhost html]# setenforce 0
十:配置nginx实现防盗链
10.1 在企业网站服务中,一般都要配置防盗链功能,以避免网站内容被非法盗用,造成经济损失
10.2 nginx防盗链功能也非常强大。默认情况下,只需要进行简单的配置,即可实现放到链处理
10.3 示例
- 使用两台主机模拟盗链
- 两台主机配置功能如下
10.4 配置nginx实现防盗链
除非用本地的域名去访问
如果匹配的是无效的,否则就跳转一个error.png页面
10.5 重启服务,再次访问图片,检测防盗效果
10.6 配置说明
- valid_referers:设置信任的网站,即能引用相应图片的网站
- none:浏览器中referer为空的情况,就是直接在浏览器访问图片
- blocked:referer不为空的情况,但是值被代理或者防火墙删除了,这些值不以http://或者https://开头
- 后面的网址或者域名referer中包含相关字符串的网址
- if’语句:如果链接的来源域名不再valid_referers所列出的列表中,$invalid-referer为1,则执行后面的操作,即执行重写或返回403页面
10.7 演示
[root@localhost ~]# cd /usr/local/nginx/
[root@localhost nginx]# ls
client_body_temp fastcgi_temp logs sbin uwsgi_temp
conf html proxy_temp scgi_temp
[root@localhost nginx]# cd html/
[root@localhost html]# ls
50x.html index.html qq.jpg
[root@localhost html]# mount.cifs //192.168.254.10/linuxs /abc
Password for root@//192.168.254.10/linuxs:
[root@localhost html]# cd /abc
[root@localhost abc]# ls
error.png
[root@localhost abc]# cp error.png /usr/local/nginx/html/
[root@localhost abc]# cd -
/usr/local/nginx/html
[root@localhost html]# ls
50x.html error.png index.html qq.jpg
[root@localhost html]# grep "qq.jpg" index.html
<img src="qq.jpg">
[root@localhost html]# service nginx start
[root@localhost html]# netstat -natp | grep 80
tcp 0 0 192.168.247.193:80 0.0.0.0:* LISTEN 3038/nginx: master
客户端去访问
使用域名,需要使用dns解析服务
搭建dns详见我前面的博客
然后指定盗链主机和客户机的dns服务器
开始设置盗机
在win10内打开控制面板,控制面板内打开程序和功能,里面有开启或者关闭功能,点击
然后点击internet informatoion servers
更改扩展名为html
打开internet information servers
现在已经默认开启
把网页移动到站点目录下
关闭盗机防火墙
然后客户端去访问盗机网站
盗链成功,接下来做防盗链操作
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
44 location ~*\.(jpg|gif|swf)$ {
45 valid_referers none blocked *.shl.com shl.com;
46 if ( $invalid_referer ) {
47 rewrite ^/ http://www.shl.com/error.png;
48 }
49 }
验证
重启nginx服务
[root@localhost named]# service nginx stop
[root@localhost named]# service nginx start
再次查看
备注:使用ip地址的方式去访问被盗链网站,也会显示error图片
注意两台服务器的防火墙要关掉
十一: 对FPM模块进行参数优化
11.1 nginx的PHP解析功能实现如果是交由FPM处理的,为了提高PHP的处理速度,可对FPM模块进行参数的调整
11.2 FPM模块参数调整,要根据服务器的内存与服务负载进行调整
11.3 启动fpm进程方式
- static:将产生固定数量的fpm进程
- dynamic:将以动态的方式产生fpm进程
- 通过pm参数指定
11.4 FPM优化参数讲解
- static的方式的参数
- pm.max_children:指定启动的进程数量
- Dynamic方式的参数
- pm.max_children:指定启动的进程数量最大的数量
- pm.static_servers:动态方式下初始的fpm进程数量
- pm.min_spare_servers:动态方式下最小的fpm空闲进程数
- pm.max_spare_servers:动态方式下最大的fpm空闲进程数
最小空闲进程数是在不着急的时候最少的进程数
主要看设置的空闲占用率
11.5 FPM优化参数调整示例
- 优化原因:服务器为云服务器,运行了个人论坛,内存为1.5G,fpm进程数为20,内存消耗近1G,处理比较慢
- 优化参数调整
- FPM启动时有5个进程,最小空闲2个进程,最大空闲8个进程,最多可以有20个进程的存在
[root@localhost ~]# vim /usr/local/php/etc/php-fpm.conf
17 pid = run/php-fpm.pid
18 pm = dynamic
19 pm.max_children=20
20 pm.static_servers = 5
21 pm.min_spare_servers = 2
22 pm.max_spare_servers = 8
servers:动态方式下初始的fpm进程数量
- pm.min_spare_servers:动态方式下最小的fpm空闲进程数
- pm.max_spare_servers:动态方式下最大的fpm空闲进程数
最小空闲进程数是在不着急的时候最少的进程数
主要看设置的空闲占用率
11.5 FPM优化参数调整示例
- 优化原因:服务器为云服务器,运行了个人论坛,内存为1.5G,fpm进程数为20,内存消耗近1G,处理比较慢
- 优化参数调整
- FPM启动时有5个进程,最小空闲2个进程,最大空闲8个进程,最多可以有20个进程的存在
[root@localhost ~]# vim /usr/local/php/etc/php-fpm.conf
17 pid = run/php-fpm.pid
18 pm = dynamic
19 pm.max_children=20
20 pm.static_servers = 5
21 pm.min_spare_servers = 2
22 pm.max_spare_servers = 8
[外链图片转存中…(img-kEwiGL1R-1577320861181)]