Nginx的编译安装和网页优化

Nginx网页优化主要包括

  • 隐藏版本号
  • 网页压缩
  • 网页缓存
  • 防盗链

资源列表

操作系统IP
Centos7192.168.10.51

编译安装Nginx

#下载nginx相关的依赖包
yum -y install pcre-devel zlib-devel gc-c++ gcc

#创建nginx的管理用户
 useradd -M -s/sbin/nologin nginx

#上传nginx-1.12.2.tar.gz软件包
tar zxf nginx-1.12.2.tar.gz -C /usr/src
cd /usr/src/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/
ls -l /usr/local/sbin/nginx

添加系统服务 
#添加系统服务方便控制nginx的启动和关闭

vi /etc/init.d/nginx

#!/bin/bash
# 必须在运行级2,3,4,5下被启动或关闭,启动的优先级是90,关闭的优先级是10
# 90是启动优先级,10是停止优先级,优先级范围是0-100,数字越大,优先级越低
#chkconfig: 2345 10 90
#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

#添加权限和系统服务
chmod +x /etc/init.d/nginx
chkconfig --add nginx
systemctl status nginx

隐藏版本号

启动nginx后可以访问查看默认的版本号

#使用curl -I进行访问
curl -I 192.168.10.51

#编译安装在/usr/local/下所以nginx的配置文件在此目录下

vim /usr/local/nginx/conf/nginx.conf

#在httpd段下面添加以下配置
server_tokens off;

#保存退出后需要重启nginx
systemctl restart nginx

 再次使用curl进行验证,可以看到版本号被隐藏了

网页压缩

#编写配置文件
vim /usr/local/nginx/conf/nginx.conf

#去掉选项前方井号
gzip_vary on;

#在选项旁边添加
    gzip_buffers 4 64k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_min_length 1k;
    gzip_types text/plain text/javascript application/x-javascript text/css text/xml application/xml application/xml+rss;

#重新启动nginx
systemctl restart nginx

 

#编辑网页访问文件

echo 'aaaaaaaaaaaaaaaaaaaaaaaaaaa' > /usr/local/nginx/html/index.html

#进入网页访问文件多复制一些,文件太小的话达不到压缩的要求

vim /usr/local/nginx/html/index.html

#在命令模式下输入yy可以复制本行
#输入10000p可以粘贴10000行

 在浏览器中右击进行检查,点击网络再次刷新可以看到使用gzip进行压缩

 网页缓存

vim /usr/local/nginx/conf/nginx.conf

#在location段下添加
expires 1d;
#网页缓存一天

#重新启动nginx
systemctl restart nginx

 日志分割

#nginx没有自带的日志分割系统,需要使用脚本或者第三方软件进行日志分割

vim nignx_log.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}/access.log-$d
# 重建新日志文件
kill -USR1 $(cat $pid_path)
# 删除 30 天之前的日志文件
find $logs_path -mtime +30 |xargs rm -rf


chmod +x nginx_log.sh

#执行一下可看下效果
./nginx_log.sh 

#可以看到nginx的日志
[root@localhost ~]# cd /var/log/nginx/
[root@localhost nginx]# ls
access.log-20240524


#写任务计划可以每天0点进行日志分割
crontab -e
0 0 * * * /root/nginx_log.sh
查看任务
crontab -l
0 0 * * * /root/nginx_log.sh

防盗链

模拟被盗
操作系统IP
Centos7192.168.10.51
Centos7192.168.10.52
#编辑配置文件,配置两个基于域名的网站
vim /usr/local/nginx/conf/nginx.conf

erver {
        listen       80;
        server_name  www.test01.com;

        location / {
            root   html/test01;
            index  index.html index.htm;
        }
}

erver {
        listen       80;
        server_name  www.test02.com;

        location / {
            root   html/test02;
            index  index.html index.htm;
        }
}

#编辑两个server段,有两个网站,可以更改一个,新添加一个
#检查配置文件是否正确
nginx -t

#创建两个网站的目录
mkdir /usr/local/nginx/html/test01
mkdir /usr/local/nginx/html/test02

#上传两个图片模拟盗链
[root@localhost nginx]# cd /usr/local/nginx/html/test01
[root@localhost test01]# ls
error.png  green.jpg


#编辑test02网站的index.html文件
vim /usr/local/nginx/html/index.html

<html><body><h1>hello</h1>
<img src="http://www.test01.com/green.jpg"/>
</body></html>

#重新启动nginx
systemctl restart nginx

 再打开一台虚拟机这边我开的为桌面版

#添加hosts解析
cat >> /etc/hosts << EOF
192.168.10.51 www.test01.com
192.168.10.51 www.test02.com
EOF

在火狐浏览器访问www.test02.com,可以看出图片属于www.test01.com

 配置防盗
vim /usr/local/nginx/conf/nginx.conf

#在www.test01.com的server段下添加

location ~* \.(gif|jpg|jpeg)$ {
    # 设置允许的引用来源,这里指定了以".test01.com"结尾的任何域名
    valid_referers *.test01.com;
    # 如果请求的referrer不在valid_referers列表内,则$invalid_referer变量会被设置
    if ($invalid_referer) {
        # 重定向不符合条件的请求到www.test01.com/error.png
        rewrite ^/ http://www.test01.com/error.png;
    }
}

#以上是配置解释,可以复制下面的


        location ~* \.(gif|jpg|jpeg)$ {
            valid_referers *.test01.com;
            if ($invalid_referer) {
            rewrite ^/ http://www.test01.com/error.png;
    }
  }

nginx -t
#检查是否出错

systemctl restart nginx
#重新启动nginx

清理浏览器缓存再次访问

  • 18
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值