Nginx优化(日志分割、网页压缩、进程管理、隐藏版本、缓存)
前言
在学习了LAMP架构之后,我们就对Apache的优化进行了深层次的理解。同样我们学习了LNMP之后,就需要对nginx进行深层次的学习。
实验环境
1台centos7虚拟机
1台win10主机
1.手工编译安装nginx
1.1将nginx的源码包解压到/opt/目录下面,在安装必要的编码包
[root@localhost ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg LNMP-C7 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@localhost ~]# cd LNMP-C7/
[root@localhost LNMP-C7]# ls
Discuz_X3.4_SC_UTF8.zip nginx-1.12.2.tar.gz php-7.1.20.tar.bz2
mysql-boost-5.7.20.tar.gz php-5.6.11.tar.bz2 php-7.1.20.tar.gz
ncurses-5.6.tar.gz php-7.1.10.tar.bz2 zend-loader-php5.6-linux-x86_64_update1.tar.gz
[root@localhost LNMP-C7]# tar -zxvf nginx-1.12.2.tar.gz -C /opt/
[root@localhost nginx-1.12.2]# yum -y install pcre pcre-devel gcc gcc-c++ make zlib-devel
[root@localhost nginx-1.12.2]# rpm -q gcc
gcc-4.8.5-39.el7.x86_64
[root@localhost nginx-1.12.2]# rpm -q gcc-c++
gcc-c++-4.8.5-39.el7.x86_64
[root@localhost nginx-1.12.2]# rpm -q zlib-devel
zlib-devel-1.2.7-18.el7.x86_64
[root@localhost nginx-1.12.2]# rpm -q pcre
pcre-8.32-17.el7.x86_64
[root@localhost nginx-1.12.2]# rpm -q pcre-devel
pcre-devel-8.32-17.el7.x86_64
[root@localhost nginx-1.12.2]# rpm -q make
make-3.82-24.el7.x86_64
1.2开始编译安装nginx
[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx
[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
1.3优化nginx的一些操作
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost nginx-1.12.2]# 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 nginx-1.12.2]# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
[root@localhost nginx-1.12.2]# cd /etc/init.d
[root@localhost init.d]# ls
functions netconsole network nginx README
[root@localhost init.d]# chmod +x nginx
[root@localhost init.d]# ls
functions netconsole network nginx README
[root@localhost init.d]# chkconfig --add nginx
[root@localhost init.d]# chkconfig --level 35 nginx on
[root@localhost init.d]# service nginx start
[root@localhost init.d]# netstat -ntap |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 41820/nginx: master
2.日志分割
2.1创建一个脚本,来管理我们的日志文件
[root@localhost init.d]# 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}/test.com-access.log-$d
kill -USR1 $(cat $pid_path)
find $logs_path -mtime +30|xargs rm -rf
[root@localhost init.d]# cd /opt
[root@localhost opt]# ls
fenge.sh nginx-1.12.2 rh
[root@localhost opt]# chmod +x fenge.sh
[root@localhost opt]# ./fenge.sh
[root@localhost opt]# crontab -e
0 1 * * * /opt/fenge.sh //每天一点钟执行脚本一次
2.2查看我们的日志文件是否能够创建
[root@localhost opt]# cd /var/log/nginx/
[root@localhost nginx]# ls
test.com-access.log-20191223
2.3把脚本里面的一些命令进行解释
date -d “-n day” 将日期从今天往前退n天
[root@localhost nginx]# date
2019年 12月 24日 星期二 16:56:37 CST
[root@localhost nginx]# date -d "-1 day"
2019年 12月 23日 星期一 16:56:55 CST
“+%Y%m%d” “+”是连接符 “%Y”是年份 "%m"是月份 “%d”是日
[root@localhost nginx]# date -d "-1 day" "+%Y%m%d"
20191223
创建昨天的日志文件
[root@localhost nginx]# /usr/local/nginx/logs/access.log ${logs_path}/test.com-access.log-$d
mv /usr/local/nginx/logs/access.log l o g s p a t h / t e s t . c o m − a c c e s s . l o g − {logs_path}/test.com-access.log- logspath/test.com−access.log−d 将nginx的日志文件移动到/var/log/nginx/目录下面
[root@localhost nginx]# cd /usr/local/nginx/logs/
[root@localhost logs]# ls
access.log error.log nginx.pid
日志文件每30天清空一次
find $logs_path -mtime +30|xargs rm -rf
3.网页压缩
3.1我们将图片放入站点目录中,并将图片写入nginx服务器的默认网页中
[root@localhost logs]# cd /usr/local/nginx
[root@localhost nginx]# ls
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
[root@localhost nginx]# cd html
[root@localhost html]# ls
50x.html index.html
[root@localhost html]# ls
1.gif 50x.html index.html
[root@localhost html]# vim index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<img src="1.gif" /> //图片的相对路径
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@localhost html]# systemctl stop firewalld
[root@localhost html]# setenforce 0
3.2我们在win10主机里面进行测试
打开抓包软件,查看抓包的内容
3.3我们修改nginx的主配置文件
[root@localhost html]# cd ..
[root@localhost nginx]# ls
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
[root@localhost nginx]# cd conf
[root@localhost conf]# ls
fastcgi.conf koi-utf nginx.conf uwsgi_params
fastcgi.conf.default koi-win nginx.conf.default uwsgi_params.default
fastcgi_params mime.types scgi_params win-utf
fastcgi_params.default mime.types.default scgi_params.default
[root@localhost conf]# vim 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/png image/gif
application/xml text/javascript application/x-httpd-php application/javascript application/json;
gzip_vary on;
[root@localhost conf]# vim nginx.conf
[root@localhost conf]# service nginx stop
[root@localhost conf]# service nginx start
3.4我们再去win10主机里面抓包
4.进程管理
4.1查看核心数
[root@localhost conf]# cat /proc/cpuinfo | grep -c "physical"
2
4.2我们对nginx的主配置文件进行修改
[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
[root@localhost conf]# cat /proc/cpuinfo | grep -c "physical"
2
[root@localhost conf]# ps -aux|grep nginx
root 42852 0.0 0.0 20544 608 ? Ss 17:51 0:00 nginx: master process /usr/local/ngin/sbin/nginx
nginx 42853 0.0 0.0 23072 1392 ? S 17:51 0:00 nginx: worker process
[root@localhost conf]# vim nginx.conf
worker_processes 2;
worker_cpu_affinity 01 10;
[root@localhost conf]# service nginx stop
[root@localhost conf]# service nginx start
[root@localhost conf]# cat /proc/cpuinfo | grep -c "physical"
2
[root@localhost conf]# ps -aux|grep nginx
root 42852 0.0 0.0 20544 608 ? Ss 17:51 0:00 nginx: master process /usr/local/ngin/sbin/nginx
nginx 42853 0.0 0.0 23072 1392 ? S 17:51 0:00 nginx: worker process
nginx 42854 0.0 0.0 23072 1392 ? S 17:51 0:00 nginx: worker process
root 42935 0.0 0.0 112728 972 pts/1 R+ 17:56 0:00 grep --color=auto nginx
5.隐藏版本
5.1查看自己的nginx的版本
[root@localhost conf]# curl -I http://192.168.73.174/
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Tue, 24 Dec 2019 10:01:19 GMT
Content-Type: text/html
Content-Length: 632
Last-Modified: Tue, 24 Dec 2019 09:20:57 GMT
Connection: keep-alive
Keep-Alive: timeout=180
ETag: "5e01d879-278"
Accept-Ranges: bytes
5.2方法一:修改主配置文件
[root@localhost conf]# vim nginx.conf
server_tokens off;
[root@localhost conf]# service nginx stop
[root@localhost conf]# service nginx start
[root@localhost conf]# curl -I http://192.168.73.174/
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 24 Dec 2019 10:11:31 GMT
Content-Type: text/html
Content-Length: 632
Last-Modified: Tue, 24 Dec 2019 09:20:57 GMT
Connection: keep-alive
Keep-Alive: timeout=180
ETag: "5e01d879-278"
Accept-Ranges: bytes
5.3方法二:在编译前,修改nginx的一个核心参数
[root@localhost conf]# cd
[root@localhost ~]# cd /opt
[root@localhost opt]# ls
fenge.sh nginx-1.12.2 rh
[root@localhost opt]# cd nginx-1.12.2/
[root@localhost nginx-1.12.2]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE Makefile man objs README src
[root@localhost nginx-1.12.2]# cd src
[root@localhost src]# ls
core event http mail misc os stream
[root@localhost src]# cd core/
[root@localhost core]# ls
nginx.c ngx_crc.h ngx_md5.c ngx_proxy_protocol.c ngx_sha1.h
nginx.h ngx_crypt.c ngx_md5.h ngx_proxy_protocol.h ngx_shmtx.c
ngx_array.c ngx_crypt.h ngx_module.c ngx_queue.c ngx_shmtx.h
ngx_array.h ngx_cycle.c ngx_module.h ngx_queue.h ngx_slab.c
ngx_buf.c ngx_cycle.h ngx_murmurhash.c ngx_radix_tree.c ngx_slab.h
ngx_buf.h ngx_file.c ngx_murmurhash.h ngx_radix_tree.h ngx_spinlock.c
ngx_conf_file.c ngx_file.h ngx_open_file_cache.c ngx_rbtree.c ngx_string.c
ngx_conf_file.h ngx_hash.c ngx_open_file_cache.h ngx_rbtree.h ngx_string.h
ngx_config.h ngx_hash.h ngx_output_chain.c ngx_regex.c ngx_syslog.c
ngx_connection.c ngx_inet.c ngx_palloc.c ngx_regex.h ngx_syslog.h
ngx_connection.h ngx_inet.h ngx_palloc.h ngx_resolver.c ngx_thread_pool.c
ngx_core.h ngx_list.c ngx_parse.c ngx_resolver.h ngx_thread_pool.h
ngx_cpuinfo.c ngx_list.h ngx_parse.h ngx_rwlock.c ngx_times.c
ngx_crc32.c ngx_log.c ngx_parse_time.c ngx_rwlock.h ngx_times.h
ngx_crc32.h ngx_log.h ngx_parse_time.h ngx_sha1.c
[root@localhost core]# vim nginx.h
#define NGINX_VERSION "1.1.1"
5.4重新编译nginx,然后查看版本
[root@localhost core]# cd ..
[root@localhost src]# cd ..
[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]# service nginx stop
[root@localhost nginx-1.12.2]# service nginx start
[root@localhost nginx-1.12.2]# curl -I http://192.168.73.174/
HTTP/1.1 200 OK
Server: nginx/1.1.1
Date: Tue, 24 Dec 2019 10:01:19 GMT
Content-Type: text/html
Content-Length: 632
Last-Modified: Tue, 24 Dec 2019 09:20:57 GMT
Connection: keep-alive
Keep-Alive: timeout=180
ETag: "5e01d879-278"
Accept-Ranges: bytes
6.缓存
6.1在win10主机里面访问
6.2修改主配置文件
[root@localhost nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
location ~\.(gif|jpeg|jpg|ico|bmp|png)$ {
root html;
expires 1d;
}
[root@localhost nginx-1.12.2]# service nginx stop
[root@localhost nginx-1.12.2]# service nginx start
6.3我们去win10里面进行抓包,并查看信息
实验总结
我们在做这个实验的时候,一定要注意有些细节,我就遇到了很多的问题,比如:字母写少了,写错了等等,都会导致服务重启失败,我们需要培养自己的耐心,争取少犯错。然后就是网页压缩那一块,多试试。对于实验,我们要多积累积累,在线网中会有好处的。