千家信息网

Nginx优化之缓存与压缩、版本隐藏

发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,安装配置Nginx挂载远程源码包到本地mount.cifs //192.168.100.10/LNMP-C7 /mnt //挂载到/mnt目录下解压源码包到/opt目录下[root@lo
千家信息网最后更新 2026年11月02日Nginx优化之缓存与压缩、版本隐藏

安装配置Nginx

挂载远程源码包到本地

mount.cifs //192.168.100.10/LNMP-C7 /mnt        //挂载到/mnt目录下

解压源码包到/opt目录下

[root@localhost ~]# cd /abc                                                       //切换到挂载点目录[root@localhost abc]# lsDiscuz_X3.4_SC_UTF8.zip    nginx-1.12.2.tar.gzmysql-boost-5.7.20.tar.gz  php-7.1.10.tar.gz[root@localhost abc]# tar zxvf nginx-1.12.2.tar.gz -C /opt        //解压Nginx源码包到/opt下[root@localhost abc]# cd /opt/                                                  //切换到解压的目录下[root@localhost opt]# lsnginx-1.12.2  rh

安装编译需要的环境组件包

[root@localhost opt]# yum -y install \gcc \                                              //c语言gcc-c++ \                                      //c++语言pcre-devel \                                  //pcre语言工具zlib-devel                                     //数据压缩用的函式库

创建程序名为nginx的用户并编译Nginx

[root@localhost opt]# useradd -M -s /sbin/nologin nginx     //创建程序用户,限定其[root@localhost opt]# cd nginx-1.12.2/                                //切换到nginx目录下[root@localhost nginx-1.12.2]# ./configure \                        //配置nginx> --prefix=/usr/local/nginx \                                                  //安装路径> --user=nginx \                                                                   //用户名> --group=nginx \                                                                 //用户组> --with-http_stub_status_module                                       //访问状态统计模块

编译和安装

[root@localhost nginx-1.12.0]# make && make install                               //编译及安装

制作Nginx管理脚本,便于管理使用

[root@localhost nginx]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/                                                                                         //创建软连接                                                                 [root@nginx nginx-1.12.2]# vim /etc/init.d/nginx             //编辑启动脚本#!/bin/bash# chkconfig: - 99 20# description: Nginx Service Control ScriptPROG="/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@nginx nginx-1.12.2]# chmod +x /etc/init.d/nginx                  //给脚本执行权限[root@nginx nginx-1.12.2]# chkconfig --add nginx                        //添加到service管理器中[root@nginx nginx-1.12.2]# yum install elinks -y                           //[root@nginx nginx-1.12.2]# service nginx start                             //启动Nginx服务[root@nginx nginx-1.12.2]# netstat -ntap | grep 80tcp        0      0 0.0.0.0:80        0.0.0.0:*          LISTEN      42028/nginx: master [root@nginx nginx-1.12.2]# systemctl stop firewalld.service                   //关闭防火墙[root@nginx nginx-1.12.2]# setenforce 0                                          //关闭增强型安全功能[root@nginx nginx-1.12.2]# elinks http://192.168.131.133/                                                                                                           

配置Nginx网页缓存

复制图片到站点目录

[root@localhost nginx-1.12.0]# ls /abcDiscuz_X3.4_SC_UTF8.zip    nginx-1.12.2.tar.gztupian.png                   php-7.1.10.tar.bz2mysql-boost-5.7.20.tar.gz  php-7.1.20.tar.gznginx-1.12.0.tar.gz[root@localhost nginx-1.12.0]# cp /abc/game.jpg /usr/local/nginx/html/[root@localhost nginx-1.12.0]# cd /usr/local/nginx/html/[root@localhost html]# ls50x.html  game.jpg  index.html

修改网页信息,将图片加到index.html文件中

[root@localhost html]# vim index.html

Welcome to nginx!

//添加图片路径
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf             //修改配置文件events {        worker_connections  1024;}        user nginx nginx;                     //修改Nginx用户和组    #deny access to .htaccess files, if Apache's document root    #concurs with nginx's one    location ~\.(gif|jepg|jpg|ico|bmp|png)$ {              //添加支持图片格式        root html;           //站点        expires 1d;         //缓存一天        }[root@localhost html]# service nginx stop                  //重启服务[root@localhost html]# service nginx start 

使用虚拟机访问网页,并使用fiddler查看缓存

Nginx网页压缩

修改Nginx.conf文件

[root@localhost ~]# cd /usr/local/nginx/conf/[root@localhost conf]# vim nginx.confgzip  on;                   //使用x键删除此行前的井号注释gzip_min_length 1k;                  //压缩阈值gzip_buffers 4 16k;                   //buffers大小为4个16k缓冲区大小gzip_http_version 1.1;              //压缩版本号gzip_comp_level 6;//压缩比率,最小为1,处理快但传输慢;最大为9,处理慢,但传输快;此处设6,相对适中gzip_types text/plain application/x-javascript text/css image/jpg image/jpegimage/png image/gif application/xml text/javascript application/x-httpd-php application/javascript application/json;                     //支持的类型格式类型gzip_disable "MSIE [1-6]\.";//配置禁用gzip条件,支持正则表达式,表示ie6以下不启用gzipgzip_vary on;                     //让前端的缓存服务器缓存经过gzip压缩的页面

复制图片到站点目录

[root@localhost conf]# cd ../html/[root@localhost html]# cp /abc/tupian.png/[root@localhost html]# ls50x.html  tupian.png  index.html

修改站点首页内容

[root@localhost html]# vim index.html

Welcome to nginx!

//在h2标签下添加图片路径[root@localhost html]# systemctl stop nginx.service //重启服务[root@localhost html]# systemctl start nginx.service [root@localhost html]# systemctl stop firewalld.service //关闭防火墙 [root@localhost html]# setenforce 0 //关闭增强型安全功能

使用测试机访问网页,并使用fiddler查看压缩

Nginx版本隐藏

隐藏版本号

[root@localhost init.d]# curl -I http://192.168.131.133/              //查看Nginx信息HTTP/1.1 200 OKServer: nginx/1.12.2           //显示版本号Date: Tue, 12 Nov 2019 14:23:24 GMTContent-Type: text/htmlContent-Length: 612Last-Modified: Tue, 12 Nov 2019 13:46:35 GMTConnection: keep-aliveETag: "5dcab7bb-264"Accept-Ranges: bytes[root@localhost init.d]# vim /usr/local/nginx/conf/nginx.conf  ##修改配置文件http {              //在http下添加        include       mime.types;        default_type  application/octet-stream;        server_tokens off;            //关闭版本号[root@localhost init.d]# service nginx stop         //关闭服务[root@localhost init.d]# service nginx start        //开启服务[root@localhost init.d]# curl -I http://192.168.131.133/          //查看Nginx信息HTTP/1.1 200 OK      Server: nginx                   //版本号被隐藏Date: Tue, 12 Nov 2019 14:22:00 GMTContent-Type: text/htmlContent-Length: 612Last-Modified: Tue, 12 Nov 2019 13:46:35 GMTConnection: keep-aliveETag: "5dcab7bb-264"Accept-Ranges: bytes

伪造版本号

[root@localhost init.d]# vim /usr/local/nginx/conf/nginx.confhttp {        include       mime.types;        default_type  application/octet-stream;        server_tokens on;                  //开启版本号
[root@localhost init.d]# cd /opt/nginx-1.12.2/src/core/             //切换到src源码包目录[root@localhost core]# vim nginx.h       //进入配置文件#define NGINX_VERSION      "1.1.1"               //此处版本号伪造成1.1.1
[root@localhost core]# cd /opt/nginx-1.12.2/         //切换目录到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.0]# 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.131.133/            //查看Nginx信息HTTP/1.1 200 OK Server: nginx/1.1.1              //此时的版本号就是伪造的版本号Date: Tue, 12 Nov 2019 14:34:02 GMTContent-Type: text/htmlContent-Length: 612Last-Modified: Tue, 12 Nov 2019 13:46:35 GMTConnection: keep-aliveETag: "5dcab7bb-264"Accept-Ranges: bytes
版本 目录 配置 服务 图片 缓存 文件 用户 网页 切换 编译 信息 源码 脚本 语言 路径 支持 管理 安全 功能 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网科技有限公司 新华互联网科技学校 平安科技 互联网 唐信互联网科技 互联网科技大会 国外互联网科技网站 互联网金融中的现代科技 河南猎农互联网科技有限公司 成都互联网科技公司工作好不好 亳州好赞互联网科技有限公司 互联网新科技动漫ppt素材 朱啸虎曾投资哪些互联网科技企业 徐州市互联网科技有限公司 互联网科技圈年度人物 保山互联网科技靠谱吗 唐山双旺合互联网科技产业园 盐城市攀科互联网科技有限公司 数字科技和互联网金融哪个好 互联网科技创新方面的资料 每日互联网科技新闻 科技互联网 广州紫鲸互联网科技有限公司 易互联网科技有限公司 唐信互联网科技 时间互联网络科技 盛创互联网科技有限公司 南山科技园互联网公司有哪些 成都铂石互联网科技做什么的 厦门千工互联网科技有限公司 互联网企业和网络科技有限公司
0