1 )安装环境包
yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel
上传nginx压缩包到/opt目录下
2 ) 解压nginx压缩包
tar -zxvf nginx-1.12.2.tar.gz
3 )创建运行用户
useradd -M -s /sbin/nologin nginx
vim /opt/nginx-1.12.2/src/core/nginx.h
#define nginx_version 1012002
#define NGINX_VERSION "1.12.2" ##1.12.2是版本号,直接修改为1.1.1
#define NGINX_VER "nginx/" NGINX_VERSION
cd /opt/nginx-1.12.2
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
make && make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
[root@localhost sbin]# 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 sbin]# nginx
[root@localhost sbin]# netstat -napt |grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 70403/nginx: master
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
$o start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage: $0 {start|stop|restart|lsreload}"
exit 1
esac
exit 0
nginx -t
chkconfig --add nginx
chmod +x nginx 增加执行权限
[root@localhost html]# curl -I http://14.0.0.8
HTTP/1.1 200 OK
Server: Nginx/1.1.1
Date: Mon, 10 Aug 2020 10:15:48 GMT
Content-Type: text/html
Content-Length: 634
Last-Modified: Mon, 10 Aug 2020 07:50:36 GMT
Connection: keep-alive
ETag: "5f30fc4c-27a"
Accept-Ranges: bytes
vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
server_tokens off; ##添加
}
vim /usr/local/nginx/conf/nginx.conf
user nginx nginx; '//添加'
service nginx restart
ps aux |grep nginx
root 130034 0.0 0.0 20220 620 ? Ss 19:41 0:00 nginx:master process
/usr/local/sbin/nginx
nginx 130035 0.0 0.0 20664 1512? S 19:41 0:00 nginx:worker process
[root@localhost nginx-1.12.2]#./configure /
--prxfix=/usr/local/nginx \
--user=nginx \
--group=nginx \
vim /usr/local/nginx/conf/nginx.conf
location ~\.(gif|jpg|jepg|png|bmp|ico)$ {
root html;
expires 1d;
}
'//1d,一天'
vim /usr/local/nginx/html/index.html
访问14.0.0.8
[root @www logs]# 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
chmod +x /opt/fenge.sh 增加执行权限
执行
[root@localhost opt]# ./fenge.sh
[root@localhost opt]# netstat -napt|grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 30192/nginx: master
[root@localhost opt]# ls /var/log/nginx/
test.com-access.log-20200809
crontab -e ‘//设置周期性任务’
0 1 * * * /opt/fenge.sh
通过更改时间验证
[root@localhost opt]# date -s 08/12/20
2020年 08月 12日 星期三 00:00:00 CST
[root@localhost opt]# ./fenge.sh
[root@localhost opt]# ls /var/log/nginx/
test.com-access.log-20200811
[root@localhost opt]# date -s 08/15/20
2020年 08月 15日 星期六 00:00:00 CST
[root@localhost opt]# ./fenge.sh
[root@localhost opt]# ls /var/log/nginx/
test.com-access.log-20200811 test.com-access.log-20200814
[root@localhost opt]#
----date -d +1(second minute hour day month year)–
--------kill -QUIT 5410 结束进程 -HUP 平滑重启 类似 reload -USRl 日志分隔 -USR2平滑升级-----
date -d “-1 day” “+%Y%m%d” ‘//##时间向前推进一天’
date -s 2019-12-25 ‘//##时间向后推移一天’
Keepalive_timeout设置
vim /usr/local/nginx/conf/nginx.conf
[root@localhost opt]# cat /proc/cpuinfo|grep -c “physical”
8
[root@localhost opt]# ^C
[root@localhost opt]# ps aux |grep nginx
root 31441 0.0 0.0 20540 804 ? Ss 8月14 0:00 nginx: m
nginx 31442 0.0 0.0 23068 1640 ? S 8月14 0:00 nginx: w
root
有1个进程
[root@localhost opt]# ps aux |grep nginx
root 32106 0.0 0.0 20540 612 ? Ss 00:35 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 32107 0.0 0.0 23068 1388 ? S 00:35 0:00 nginx: worker process
nginx 32108 0.0 0.0 23068 1388 ? S 00:35 0:00 nginx: worker process
root 32112 0.0 0.0 112724 984 pts/0 S+ 00:36 0:00 grep --color=auto nginx
有两个进程了
优化
网页压缩配置
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/jpeg image/png image/gif application/xml text/javascript application/x-httpd-php application/javascript application/json;
gzip_disable "MSIE [1-6]\.";
gzip_vary on;
重启服务
[root@localhost ~]# service nginx stop
[root@localhost ~]# service nginx start
win10客户机访问14.0.0.8
用于压缩功能了
编辑配置文件(主机1)
vim /usr/local/nginx/conf/nginx.conf
location ~*.(jpg|gif|swf)$ {
valid_referers none blocked *.kgc.com kgc.com;
if ( $invalid_referer ) {
rewrite ^/ http://www.kgc.com/error.png;
}
}
重启服务
systemctl restart nginx
dl2oFtFn-1597499441057)]
用于压缩功能了
编辑配置文件(主机1)
vim /usr/local/nginx/conf/nginx.conf
location ~*.(jpg|gif|swf)$ {
valid_referers none blocked *.kgc.com kgc.com;
if ( $invalid_referer ) {
rewrite ^/ http://www.kgc.com/error.png;
}
}
[外链图片转存中…(img-GqvGYfr9-1597499441058)]
重启服务
systemctl restart nginx