文章目录
在使用Nginx作为web站点中我们可以通过对Nginx优化,来实现相关的各种功能,比如:
- 隐藏软件版本号
- 更改程序运行用户与组
- 配置网页缓存时间
- Nginx日志切割
- 设置连接超时
- 更改进程数
- 配置网页压缩与防盗链
- fpm参数优化
下面进行每一个功能的配置操作
实验环境
一台centos7做Nginx服务器,IP地址为192.168.179.144
一台Windows做客户端,IP地址为192.168.179.110
首先安装好Nginx,具体操作可以看我的这篇博客
一、隐藏软件版本号
在Nginx中隐藏软件版本号的方法有两种:
1.修改配置文件
2.修改源码
1.1 修改配置文件
1.查看当前安装的Nginx版本
[root@localhost nginx-1.12.2]# curl -I http://192.168.179.144/
HTTP/1.1 200 OK
Server: nginx/1.12.2 '可以看到当前版本号'
Date: Mon, 10 Aug 2020 07:02:43 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 10 Aug 2020 06:58:08 GMT
Connection: keep-alive
ETag: "5f30f000-264"
Accept-Ranges: bytes
2.修改配置文件
[root@localhost nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
server_tokens off; '添加关闭版本号'
[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.179.144/
HTTP/1.1 200 OK
Server: nginx '版本号已隐藏'
Date: Mon, 10 Aug 2020 07:03:58 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 10 Aug 2020 06:58:08 GMT
Connection: keep-alive
ETag: "5f30f000-264"
Accept-Ranges: bytes
1.2 修改源码
修改源码隐藏版本信息需要在配置编译安装Nginx之前操作
[root@localhost nginx-1.12.2]# vim src/core/nginx.h
...
#define NGINX_VERSION "1.1.1" '修改版本号为1.1.1'
#define NGINX_VER "IIS/" NGINX_VERSION '修改软件类型为IIS'
二、更改程序运行用户与组
- Nginx运行时进程需要有用户与组的支持,以实现对网站文件读取时进行访问控制
- Nginx默认使用nobody用户账号与组账号
修改方法有两种:
- 编译安装时指定用户和组
- 修改配置文件指定用户和组
2.1 编译安装时指定用户和组
这个操作在编译安装时进行,具体看此篇博客
创建运行的用户和组
[root@localhost nginx-1.12.0]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \ '指定用户'
--group=nginx \ '指定属组'
--with-http_stub_ status_ module
2.2 修改配置文件指定用户和组
[root@localhost nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
user nginx nginx; '对第一条进行修改'
worker_processes 1;
三、配置网页缓存时间
- 当Nginx将网页数据返回给客户端后,可设置缓存的时间,以方便在日后进行相同内容的请求时直接返回,避免重复请求,加快了访问速度
- 一般针对静态网页设置,对动态网页不设置缓存时间
设置方法
修改配置文件,在http段、或者server段、或者location段加入对特定内容的过期参数
[root@localhost nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
location ~\.(gif|jpg|jpeg|png|ico)$ { '在location段添加'
root html;
expires 1d;
}
四、Nginx日志切割
- 随着Nginx运行时间增加,日志也会增加。为了方便掌握Nginx运行状态,需要时刻关注Nginx日志文件
- 太大的日志文件对监控是一个大灾难
- 定期进行日志文件的切割
- Nginx自身不具备日志分割处理的功能,但可以通过Nginx信号控制功能的脚本实现日志的自动切割
- 通过Linux的计划任务周期性地进行日志切割
编写脚本进行日志切割的思路
- 设置时间变量
- 设置保存日志路径
- 将目前的日志文件进行重命名
- 重建新日志文件
- 删除时间过长的日志文件
- 设置cron任务,定期执行脚本自动进行日志分割
1.修改配置文件
[root@localhost opt]# vim qiege.sh
#!/bin/bash
#Filename:qiege.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)
#'删除30天前的日志'
find $logs_path -mtime +30 | xargs rm -rf
[root@localhost opt]# chmod +x qiege.sh
2.测试
[root@localhost opt]# ./qiege.sh
[root@localhost opt]# ls /var/log/nginx/
test.com-access.log-20200809
[root@localhost opt]# date -s 08/11/20 '修改时间'
[root@localhost opt]# ./qiege.sh
[root@localhost opt]# ls /var/log/nginx/
[root@localhost opt]# ls /var/log/nginx/
test.com-access.log-20200809 test.com-access.log-20200810
3.设置cron任务
[root@localhost opt]# crontab -e
0 1 * * * /opt/qiege.sh
五、设置连接超时
Nginx使用keepalive_ timeout来指定KeepAlive的超时时间(timeout) 。
指定每个TCP连接最多可以保持多长时间。Nginx的默认值是65秒,有些浏览器最多只保持60秒,若将它设置为0,就禁止了keepalive连接。
- 为避免同一客户端长时间占用连接,造成资源浪费,可设置相应的连接超时参数,实现控制连接访问时间
- 超时参数
- Keepalive_timeout
- 设置连接保持超时时间
- Keepalive_timeout
- Client_header_timeout
- 指定等待客户端发送请求头的超时时间
- Client_body_timeout
- 设置请求体读超时时间
[root@localhost opt]# vim /usr/local/nginx/conf/nginx.conf
keepalive_timeout 100; '修改超时时间为100'
client_header_timeout 80; '等待客户端发送请求的超时时间'
client_body_timeout 80; '设置客户端发送请求体超时时间'
六、更改进程数
- 在高并发场景,需要启动更多的Nginx进程以保证快速响应,以处理用户的请求,避免造成阻塞
- 更改进程数的配置方法
修改配置文件,修改进程配置参数 - 修改配置文件的worker_processes参数
一般设为CPU的个数或者核数
在高并发情况下可设置为CPU个数或者核数的2倍 - 增加进程数,可减少了系统的开销,提升了服务速度
[root@localhost opt]# cat /proc/cpuinfo | grep -c "physical"
8 '核心数8'
[root@localhost opt]# vim /usr/local/nginx/conf/nginx.conf
user nobody;
worker_processes 8; '将进程数修改为8'
[root@localhost opt]# service nginx stop
[root@localhost opt]# service nginx start
[root@localhost opt]# ps aux | grep nginx '查看运行进程数的变化'
root 17056 0.0 0.0 20544 676 ? Ss 8月10 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 17057 0.0 0.0 23072 1392 ? S 8月10 0:00 nginx: worker process
nobody 17058 0.0 0.0 23072 1392 ? S 8月10 0:00 nginx: worker process
nobody 17059 0.0 0.0 23072 1392 ? S 8月10 0:00 nginx: worker process
nobody 17060 0.0 0.0 23072 1392 ? S 8月10 0:00 nginx: worker process
nobody 17061 0.0 0.0 23072 1392 ? S 8月10 0:00 nginx: worker process
nobody 17062 0.0 0.0 23072 1932 ? S 8月10 0:00 nginx: worker process
nobody 17063 0.0 0.0 23072 1392 ? S 8月10 0:00 nginx: worker process
nobody 17064 0.0 0.0 23072 1392 ? S 8月10 0:00 nginx: worker process
root 18076 0.0 0.0 112724 988 pts/1 S+ 00:12 0:00 grep --color=auto nginx
七、网页压缩
1.修改配置文件
[root@localhost opt]# vim /usr/local/nginx/conf/nginx.conf
gzip on; '开启gzip压缩功能'
gzip_min_length 1k; '压缩阈值'
gzip_buffers 4 16k; 'buffer大小为4个16k缓冲区大小'
gzip_http_version 1.1; '压缩版本'
gzip_comp_level 6; '压缩比率'
gzip_types text/plain application/x-javascript text/css image/jpg image/jpeg image/png image/gif application/xml text/javascript application/x-httpd-php application/javascript application/json;
'支持压缩的格式'
gzip_disable "MSIE[1-6]\."; '配置禁用gzip条件,支持正则,表示ie6以下不启用gzip'
gzip_vary on; '选择支持very header可以让前端的缓存服务器缓存经过gzip压缩的页面'
2.添加测试图片
[root@localhost html]# ls
50x.html apic.jpg index.html
[root@localhost html]# vim index.html
...
<img src="apic.jpg"/> '添加'
<h1>Welcome to nginx!</h1>
八、配置防盗链
1.配置dns功能(操作过程可看我之前的博客)
2.开启一台新的虚拟机做盗链功能
IP地址为192.168.179.100
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
Listen 192.168.179.100:80
#Listen 80
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# vim index.html
<h1>this is test web</h1>
<img src="http://www.cllt.com/apic.jpg"/> '指向源服务器的图片'
[root@localhost html]# iptables -F
[root@localhost html]# setenforce 0
[root@localhost html]# systemctl start httpd.service
3.开启防盗链功能
[root@localhost named]# vim /usr/local/nginx/conf/nginx.conf
location ~*\.(jpg|gif|jepg)$ {
valid_referers none blocked *.cllt.com cllt.com;
if ( $invalid_referer ) {
rewrite ^/ http://www.cllt.com/error.png;
}
[root@localhost named]# cd /usr/local/nginx/html/
[root@localhost html]# ls
50x.html apic.jpg error.png index.html
九、fpm参数优化
9.1 FPM模块概述
- Nginx的PHP解析功能实现如果是交由FPM处理的,为了提高PHP的处理速度,可对FPM模块进行参数的调整
- FPM模块参数调整,要根据服务器的内存与服务负载进行调整
- 启动fpm进程方式
static:将产生固定数量的fpm进程
dynamic:将以动态的方式产生fpm进程
通过pm参数指定
9.2 FPM优化参数讲解
- Static的方式的参数
pm.max_children:指定启动的进程数量 - Dynamic方式的参数
pm.max_children:指定启动的进程数量最大的数量
pm.start_servers:动态方式下初始的m进程数量
pm.min_spare_servers:动态方式下最小的fpm空闭进程数
pm.max_spare_servers:动态方式下最大的fpm空闭进程数
9.3 FPM参数优化
[root@localhost html]# vim php-fpm.conf
pid = run/php-fpm.pid
pm = dynamic
pm.max_children=20 'static模式下空闲进程数上限,大于下面的值'
pm.start_servers= 5 '动态方式下默认开启的进程数,在最小和最大之间'
pm.min_spare_servers = 2 '动态方式下最少空闲进程数'
pm.max_spare_servers = 8 '动态方式下最大空闲进程数'