在企业信息化应用环境中,服务器的安全性和响应速度需要根据实际情况进行相应参数配置,以达到最优的用户体验
默认的nginx安装参数只能供最基本的服务,还需要调整如网页缓存时间,连接超时,网页压缩等相应参数,才能发挥出服务器的最大作用
在使用Nginx作为web站点中我们可以通过对Nginx优化,来实现相关的各种功能
- 隐藏软件版本号
- 更改程序运行用户与组
- 配置网页缓存时间
- Nginx日志切割
- 设置连接超时
- 更改进程数
- 配置网页压缩与防盗链
- fpm参数优化
Nginx安装步骤看我以前博客
隐藏软件版本号
查看当前安装的Nginx版本
[root@promote nginx-1.12.2]# curl -I http://192.168.110.15
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Tue, 11 Aug 2020 01:22:13 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 11 Aug 2020 01:20:27 GMT
Connection: keep-alive
ETag: "5f31f25b-264"
Accept-Ranges: bytes
修改配置文件
[root@promote nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
server_tokens off; #添加关闭版本号
[root@promote nginx-1.12.2]# service nginx restart
[root@promote nginx-1.12.2]# curl -I http://192.168.110.15
HTTP/1.1 200 OK
Server: nginx #已经隐藏
Date: Tue, 11 Aug 2020 01:28:08 GMT
修改源码
修改源码隐藏版本信息需要在配置编译安装Nginx之前操作
[root@promote nginx-1.12.2]# vim src/core/nginx.h
#define nginx_version 1012002
#define NGINX_VERSION "1.1.1" #随便改个数字
#define NGINX_VER "nginx/" NGINX_VERSION #修改软件类型为IIS
更改程序运行用户与组
Nginx运行时进程需要有用户与组的支持,以实现对网站文件读取时进行访问控制
Nginx默认使用nobody用户账号与组账号
修改方法有两种
- 编译安装时指定用户和组
- 修改配置文件指定用户和组
这个操作需要在编译安装时进行
[root@promote nginx-1.12.0]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \ #指定用户
--group=nginx \ #指定属组
--with-http_stub_ status_ module
或者修改配置文件指定用户和组
[root@promote nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
user nginx nginx; #对第一条进行修改
worker_processes 1;
配置网页缓存时间
当Nginx将网页数据返回给客户端后,可设置缓存的时间,以方便在日后进行相同内容的请求时直接返回,避免重复请求,加快了访问速度
一般针对静态网页设置,对动态网页不设置缓存时间
修改配置文件,在http段、或者server段、或者location段加入对特定内容的过期参数
[root@promote nginx-1.12.2]# cd /usr/local/nginx/html/
[root@promote html]# ls
50x.html b.jpg index.html #添加一个图片
[root@promote html]# vim /usr/local/nginx/conf/nginx.conf
location ~\.(gif|jpg|jpeg|png|ico)$ { 在location段添加特定内容参数
root html;
expires 1d;
}
重启服务访问网页抓包
Nginx日志切割
编写脚本进行日志切割的思路
[root@promote 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@promote opt]# chmod +x qiege.sh
[root@promote opt]# ./qiege.sh
[root@promote opt]# ls /var/log/nginx/
test.com-access.log-20200810
设置cron任务
[root@promote opt]# crontab -e
0 1 * * * /opt/qiege.sh
设置连接超时
Nginx使用keepalive_ timeout来指定KeepAlive的超时时间(timeout) 。
指定每个TCP连接最多可以保持多长时间。Nginx的默认值是65秒,有些浏览器最多只保持60秒,若将它设置为0,就禁止了keepalive连接。
为避免同一客户端长时间占用连接,造成资源浪费,可设置相应的连接超时参数,实现控制连接访问时间
[root@promote 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@promote opt]# cat /proc/cpuinfo | grep -c "physical" #查看核心数
8
[root@promote opt]# vim /usr/local/nginx/conf/nginx.conf
worker_processes 8; #将进程数修改为8
[root@promote opt]# service nginx restart
[root@promote opt]# ps aux | grep nginx
root 72511 0.0 0.0 20540 668 ? Ss 10:50 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 72512 0.0 0.0 23068 1388 ? S 10:50 0:00 nginx: worker process
nginx 72513 0.0 0.0 23068 1388 ? S 10:50 0:00 nginx: worker process
nginx 72514 0.0 0.0 23068 1388 ? S 10:50 0:00 nginx: worker process
nginx 72515 0.0 0.0 23068 1388 ? S 10:50 0:00 nginx: worker process
nginx 72516 0.0 0.0 23068 1388 ? S 10:50 0:00 nginx: worker process
nginx 72517 0.0 0.0 23068 1388 ? S 10:50 0:00 nginx: worker process
nginx 72518 0.0 0.0 23068 1388 ? S 10:50 0:00 nginx: worker process
nginx 72519 0.0 0.0 23068 1388 ? S 10:50 0:00 nginx: worker process
root 72523 0.0 0.0 112724 988 pts/0 S+ 10:50 0:00 grep --color=auto nginx
网页压缩
修改配置文件
[root@promote 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压缩的页面
配置防盗链
在配置文件里配置防盗链功能
具体实验过程看我以前Apache防盗链实验
[root@promote named]# vim /usr/local/nginx/conf/nginx.conf
location ~*\.(jpg|gif|jepg)$ {
valid_referers none blocked *.aaa.com aaa.com;
if ( $invalid_referer ) {
rewrite ^/ http://www.aaa.com/c.png;
}
[root@promote named]# cd /usr/local/nginx/html/
[root@promote html]# ls
50x.html b.jpg c.png index.html
FPM参数优化
Nginx的PHP解析功能实现如果是交由FPM处理的,为了提高PHP的处理速度,可对FPM模块进行参数的调整
FPM模块参数调整,要根据服务器的内存与服务负载进行调整
启动fpm进程方式
- static:将产生固定数量的fpm进程
- dynamic:将以动态的方式产生fpm进程
- 通过pm参数指定
Static的方式的参数
- pm.max_children:指定启动的进程数量
Dynamic方式的参数
- pm.max_children:指定启动的进程数量最大的数量
- pm.start_servers:动态方式下初始的m进程数量
- pm.min_spare_servers:动态方式下最小的fpm空闭进程数
- pm.max_spare_servers:动态方式下最大的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 #动态方式下最大空闲进程数