一、如何给配置文件加高亮?
给配置文件加高亮方便查看及排错
1.新建目录.vim
[root@server1 ~]# mkdir .vim
2.切换到nginx-1.16.0/contrib/目录下,复制vim/*(vim下所以文件)到 ~/.vim
[root@server1 ~]# cd nginx-1.16.0/contrib/
[root@server1 contrib]# cp -r vim/* ~/.vim
3.查看配置文件有高亮
[root@server1 contrib]# cd /usr/local/nginx/conf/
[root@server1 conf]# vim nginx.conf
二、打包日志
- 企业中要打包前一天的日志,如何实现?
1.切换到nginx的日志目录下
[root@server1 ~]# cd /usr/local/nginx/
[root@server1 nginx]# ls
client_body_temp fastcgi_temp logs sbin uwsgi_temp
conf html proxy_temp scgi_temp
[root@server1 nginx]# cd logs/
[root@server1 logs]# ls
access.log error.log nginx.pid
2.以前一天的日期为名打包日志
[root@server1 logs]# mv access.log `date +%F -d -1day`access.log
[root@server1 logs]# ls
2019-04-30access.log error.log nginx.pid
3.重新加载nginx服务,查看日志
[root@server1 logs]# /usr/local/nginx/sbin/nginx -s reopen
[root@server1 logs]# ls
4.查看日志大小
[root@server1 logs]# du -sh *
- 编写脚本,定期打包日志
[root@server1 logs]# vim backup.sh
#!/bin/bash
LOG_PATH=/usr/local/nginx/logs/oldlogs
CUR_LOG_PATH=/usr/local/nginx/logs
YESTERDAY=$(date +%F -d -1day)
mv $CUR_LOG_PATH/access.log $LOG_PATH/${YESTERDAY}_access.log
mv $CUR_LOG_PATH/error.log $LOG_PATH/${YESTERDAY}_error.log
kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid)
2.给脚本赋予可执行权限
[root@server1 logs]# chmod +x backup.sh
3.新建日志目录
[root@server1 logs]# mkdir oldlogs
4.执行脚本,查看日志
[root@server1 logs]# ./backup.sh
[root@server1 logs]# ls
access.log backup.sh error.log nginx.pid oldlogs
[root@server1 logs]# cd oldlogs/
[root@server1 oldlogs]# ls
5.设置定时任务
[root@server1 logs]# crontab -e
[root@server1 logs]# crontab -l
0 0 * * * /bin/bash /usr/local/nginx/logs/backup.sh
三、压缩抓包减少内存占用
1.编辑nginx的配置文件
[root@server1 ~]# cd /usr/local/nginx/conf
[root@server1 conf]# ls
fastcgi.conf koi-win scgi_params
fastcgi.conf.default mime.types scgi_params.default
fastcgi_params mime.types.default uwsgi_params
fastcgi_params.default nginx.conf uwsgi_params.default
koi-utf nginx.conf.default win-utf
[root@server1 conf]# vim nginx.conf
gzip on; ##打开gzip开关
gzip_min_length 1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml tex
t/javascript application/x-httpd/php image/jpeg image/gif image/png;
2.切换到默认发布目录下,复制/etc/passwd到当前路径下
[root@server1 nginx]# cd html/
[root@server1 html]# cp /etc/passwd .
3.删除默认发布文件
[root@server1 html]# rm -fr index.html
4.passwd重命名为默认发布文件
[root@server1 html]# mv passwd index.html
5.编辑配置文件,查看文件大小
[root@server1 html]# vim index.html
[root@server1 html]# du -sh index.html
144K index.html
7.重新加载nginx服务
[root@server1 html]# /usr/local/nginx/sbin/nginx -s reload
8.浏览器中访问172.25.21.1,可以查看压缩后的文件大小为2.58kb
9.编辑配置文件,关闭压缩的命令,重新加载nginx服务
[root@server1 conf]# vim nginx.conf
[root@server1 conf]# /usr/local/nginx/sbin/nginx -s reload
10.浏览器中查看文件大小依然为143.34kb
四、nginx的启动脚本
1.切换到系统默认启动脚本的目录下
[root@server1 ~]# cd /usr/lib/systemd/system
2.安装httpd
[root@server1 system]# yum install httpd -y
3.复制httpd的启动脚本到/etc/systemd/system/目录下并重命名为nginx.service
[root@server1 system]# cp httpd.service /etc/systemd/system/nginx.service
4.切换到/etc/systemd/system/目录下,修改启动脚本
[root@server1 system]# cd /etc/systemd/system/
[root@server1 system]# ls
[root@server1 system]# vim nginx.service
[Unit]
Description=The Nginx HTTP Server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
5.启动脚本配置成功
[root@server1 system]# systemctl start nginx.service
[root@server1 system]# ps ax
[root@server1 system]# systemctl stop nginx.service
[root@server1 system]# ps ax
五、
1.编辑nginx配置文件
[root@server1 conf]# vim nginx.conf
2.在/usr/local/nginx/html/目录下,新建目录search,并放入一个图片
[root@server1 nginx]# cd html/
[root@server1 html]# mkdir search
[root@server1 html]# ls
50x.html index.html search
[root@server1 html]# cd search/
[root@server1 search]# ls
vim.jpg
3.语法检测并重启服务
[root@server1 search]# /usr/local/nginx/sbin/nginx -t
[root@server1 search]# systemctl reload nginx
4.测试:模拟客户端请求
[root@foundation21 ~]# ab -c 1 -n 10 http://172.25.21.1/search/vim.jpg
[root@server1 search]# cd ..
[root@server1 html]# cd ..
[root@server1 nginx]# cd conf/
[root@server1 conf]# vim nginx.conf
[root@server1 conf]# systemctl reload nginx
[root@foundation21 ~]# ab -c 1 -n 10 http://172.25.21.1/search/vim.jpg
限制客户端下载速率为50K
1.编辑nginx配置文件
[root@server1 conf]# vim nginx.conf
2.重启服务
[root@server1 conf]# systemctl reload nginx
3.测试:
[root@foundation21 ~]# ab -c 1 -n 5 http://172.25.21.1/search/vim.jpg
系统日志
1.编辑nginx配置文件
[root@server1 conf]# vim nginx.conf
2.重启服务
[root@server1 conf]# systemctl reload nginx
[root@server1 conf]# vim nginx.conf
[root@server1 conf]# cd …/logs/
[root@server1 logs]# ls
access.log backup.sh error.log nginx.pid oldlogs
[root@server1 logs]# vim access.log
[root@server1 logs]# cd ..
[root@server1 nginx]# cd conf/
[root@server1 conf]# vim nginx.conf
access_log logs/westos.access.log main; ##添加的内容
[root@server1 conf]# systemctl reload nginx
[root@foundation21 ~]# ab -c 1 -n 5 http://172.25.21.1/search/vim.jpg
[root@server1 conf]# cd ..
[root@server1 nginx]# cd logs/
[root@server1 logs]# ls
access.log backup.sh error.log nginx.pid oldlogs westos.access.log
[root@server1 logs]# vim westos.access.log