Apache网页与优化(压缩、缓存、版本信息隐藏、防盗链设置)

本文主要是介绍Apache网页与优化(压缩、缓存、版本信息隐藏、防盗链设置),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

网页与安全优化

  • 网页压缩
    • 1.检查mod_deflate模块是否安装,如未安装则进行重新编译添加模块
    • 2.配置mod_deflate模块启用
    • 3.检查安装情况并启动服务
    • 4.测试mod_deflate压缩是否生效(共两种方式)
  • 网页缓存
    • 1.检查mod_expires模块是否安装,如未安装则进行重新编译添加模块
    • 2.配置mod_expires模块启用
    • 3.检查安装情况,启动服务
    • 4.测试缓存是否生效(共两种)
  • 隐藏版本信息
  • Apache防盗链
    • 1.检查mod_rewrite模块是否安装,如未安装则进行重新编译添加模块
    • 2.配置mod_rewrite模块并用
      • mode_rewrite模块内容字段含义
    • 3.网页准备
      • Web源主机配置
      • 盗链网站主机配置

网页压缩

  • 在企业中,部署Apache后只采用默认的配置参数,会引发网站很多问题,也就相当于是默认配置只是针对以前较低的服务器配置的,以前的配置现在已经不适用于当今互联网时代
  • 为了适应企业需求,所以就需要考虑如何提升Apache的性能与稳定性,因此就有了Apache的优化

1.检查mod_deflate模块是否安装,如未安装则进行重新编译添加模块

[root@localhost conf]# apachectl -t -D DUMP_MODULES | grep "deflate"
[root@localhost /]# systemctl stop httpd.service 
[root@localhost /]# cd /usr/local/httpd/conf/
[root@localhost conf]# ls
extra  httpd.conf  httpd.conf.bak  magic  mime.types  original
[root@localhost conf]# mv httpd.conf httpd.conf.bakbak
【需将原来的httpd.conf文件移动重命名新文件,否则将无法成功编译安装】
[root@localhost conf]# ls
extra  httpd.conf.bak  httpd.conf.bakbak  magic  mime.types  original
[root@localhost conf]# yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel
...过程略...
[root@localhost httpd-2.4.29]# ./configure \
> --prefix=/usr/local/httpd \
> --enable-so \
> --enable-rewrite \
> --enable-charset-lite \
> --enable-cgi \
> --enable-deflate        【加入mod_deflate模块】
...过程略...
[root@localhost httpd-2.4.29]# make -j4 && make install
...编译过程略...

2.配置mod_deflate模块启用

[root@localhost /]# vim /usr/local/httpd/conf/httpd.conf52 Listen 192.168.131.13:80                          【修改为本机ip地址】
105 LoadModule mime_module modules/mod_mime.so        【开启mod_deflate模块】
198 ServerName www.qz.com:80                          【取消注释并修改】
511 <IfModule mod_deflate.c>                          【末行添加】
512 AddOutputFilterByType DEFLATE ceshi/html ceshi/plain ceshi/css ceshi/xml ceshi/javascript ceshi/jpg ceshi/png
【表示对什么样的内容启用gzip压缩】
513 DeflateCompressionLevel 6
【压缩级别,范围为1-9514 SetOutputFilter DEFLATE
【启用deflate模块对本站点的输出进行gzip压缩】
515 </IfModule>

3.检查安装情况并启动服务

[root@localhost conf]# apachectl -t                                    【验证配置文件的配置是否正确】
Syntax OK
[root@localhost conf]# apachectl -t -D DUMP_MODULES | grep "deflate"   【检查DUMP_MODULES模块是否安装】deflate_module (shared)                                               【若已安装则显示这样的正确结果】
[root@localhost /]# systemctl start httpd.service

4.测试mod_deflate压缩是否生效(共两种方式)

[root@localhost /]# cd /usr/local/httpd/htdocs/
【将tea文件传到/usr/local/httpd/htdocs目录下】
[root@localhost htdocs]# ls
bbs  index.html  index.php  tea.jpg
[root@localhost htdocs]# vim index.html<html><body><h1>this is tea! this is tea!this is tea! this is tea!
this is tea! this is tea!this is tea! this is tea!
this is tea! this is tea!this is tea! this is tea!
this is tea! this is tea!this is tea! this is tea!
this is tea! this is tea!this is tea! this is tea!
this is tea! this is tea!this is tea! this is tea!
this is tea! this is tea!this is tea! this is tea!
this is tea! this is tea!this is tea! this is tea!
this is tea! this is tea!this is tea! this is tea!
this is tea! this is tea!this is tea! this is tea!
this is tea! this is tea!this is tea! this is tea!
this is tea! this is tea!this is tea! this is tea!</h1>
<img src="tea.jpg"/>
  • 方法一(Linux)
    • 在Linux系统中,使用自带的火狐浏览器,右键查看元素
    • 选择网络→选择HTML、WS、其他,或者全部
    • 访问http://192.168.131.13(或者域名http://www.qz.com) 双击200响应消息查看响应头中包含Content-Encoding:gzip

在这里插入图片描述

  • 方法二(Windows10)
    • 在Windows系统中安装fiddler软件(Windows7则需要先安装Microsoft.NET4再安装fiddler软件)
    • 选择inspectors再选择Headers
    • 访问http://192.168.131.13(或者域名http://www.qz.com) 双击200响应消息查看Content-Encoding:gzip

在这里插入图片描述

网页缓存

1.检查mod_expires模块是否安装,如未安装则进行重新编译添加模块

[root@localhost htdocs]# apachectl -t -D DUMP_MODULES | grep "expires"
[root@localhost htdocs]# systemctl stop httpd.service 
[root@localhost htdocs]# cd /usr/local/httpd/conf/
[root@localhost conf]# ls
extra  httpd.conf  httpd.conf.bak  httpd.conf.bakbak  magic  mime.types  original
[root@localhost conf]# mv httpd.conf httpd.conf.bakbakbak
[root@localhost conf]# ls
extra  httpd.conf.bak  httpd.conf.bakbak  httpd.conf.bakbakbak  magic  mime.types  original
[root@localhost conf]# yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel
...过程略...
[root@localhost httpd-2.4.29]# ./configure \
> --prefix=/usr/local/httpd \
> --enable-so \
> --enable-rewrite \
> --enable-charset-lite \
> --enable-cgi \
> --enable-deflate \
> --enable-expires           【加入mod_expires模块】
...过程略...
[root@localhost httpd-2.4.29]# make -j4 && make install
...过程略...
[root@localhost httpd-2.4.29]# cd -
/usr/local/httpd/conf
[root@localhost conf]# ls
extra       httpd.conf.bak     httpd.conf.bakbakbak  mime.types
httpd.conf  httpd.conf.bakbak  magic                 original

2.配置mod_expires模块启用

[root@localhost conf]# vim /usr/local/httpd/conf/httpd.conf52 Listen 192.168.131.13:80                           【修改为本机IP】
111 LoadModule expires_module modules/mod_expires.so   【取消注释,即开启mod_expires模块】
199 ServerName www.qz.com:80                           【取消注释并修改域名】
510 <IfModule mod_expires.c>                           【末行添加】
511   ExpiresActive On                                 【打开网页缓存功能】
512   ExpiresDefault "access plus 30 seconds"          【缓存时间为30秒】
513 </IfModule>

3.检查安装情况,启动服务

[root@localhost conf]# apachectl -t                                  【验证配置文件的配置是否正确】
Syntax OK
[root@localhost conf]# apachectl -t -D DUMP_MODULES | grep "expires" 【检查mod_expires模块是否已安装】expires_module (shared)                                             【若已安装则显示这样的正确结果】
[root@localhost conf]# systemctl start httpd.service 

4.测试缓存是否生效(共两种)

  • 方法一(Linux)
    • 在Linux系统中,使用自带的火狐浏览器,右键查看元素
    • 选择网络→选择HTML、WS、其他,或者全部
    • 访问http://192.168.131.13(或者域名http://www.qz.com) 双击200响应消息查看响应头中包含Expires选项

在这里插入图片描述

  • 方法二(Windows10)
    • 在Windows系统中安装fiddler软件(Windows7则需要先安装Microsoft.NET4再安装fiddler软件)
    • 选择inspectors再选择Headers
    • 访问http://192.168.131.13(或者域名http://www.qz.com)双击200响应消息查看响应头中包含Expires选项
      在这里插入图片描述

隐藏版本信息

[root@localhost /]# vim /usr/local/httpd/conf/httpd.conf
491 Include conf/extra/httpd-default.conf   【491行取消注释】
[root@localhost /]# vim /usr/local/httpd/conf/extra/httpd-default.conf55 ServerTokens Prod                       【55行进行修改】
【将原本的Full改成Prod,即只显示名称,没有版本】
【ServerTokens表示Server回送给客户端的响应头域是否包含关于服务器OS类型和编译过的模块描述信息】
[root@localhost /]# systemctl restart httpd.service 
  • 浏览器访问http://192.168.131.13 然后双击200消息查看Server选项
    在这里插入图片描述

Apache防盗链

1.检查mod_rewrite模块是否安装,如未安装则进行重新编译添加模块

[root@localhost /]# apachectl -t -D DUMP_MODULES | grep "rewrite"
[root@localhost /]# systemctl stop httpd.service 
[root@localhost /]# cd /usr/local/httpd/conf/
[root@localhost conf]# ls
extra       httpd.conf.bak     httpd.conf.bakbakbak  mime.types
httpd.conf  httpd.conf.bakbak  magic                 original
[root@localhost conf]# mv httpd.conf httpd.conf.bakbakbakbak
[root@localhost conf]# ls
extra           httpd.conf.bakbak     httpd.conf.bakbakbakbak  mime.types
httpd.conf.bak  httpd.conf.bakbakbak  magic                    original
[root@localhost conf]# yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel
...过程略...
[root@localhost httpd-2.4.29]# cd /opt/httpd-2.4.29/
[root@localhost httpd-2.4.29]# ./configure \
> --prefix=/usr/local/httpd \
> --enable-so \
> --enable-rewrite \          【将mod_rewrite模块加入】
> --enable-charset-lite \
> --enable-cgi \
> --enable-deflate \
> --enable-expires
...过程略...
[root@localhost httpd-2.4.29]# make -j4 && make install
...过程略...
[root@localhost httpd-2.4.29]# cd -
/usr/local/httpd/conf
[root@localhost conf]# ls
extra       httpd.conf.bak     httpd.conf.bakbakbak     magic       original
httpd.conf  httpd.conf.bakbak  httpd.conf.bakbakbakbak  mime.types

2.配置mod_rewrite模块并用

157 LoadModule rewrite_module modules/mod_rewrite.so                【取消注释】
224 <Directory "/usr/local/httpd/htdocs">
......
237     Options Indexes FollowSymLinks
......
244     AllowOverride None
......
249     Require all granted
250     RewriteEngine On                                            【打开rewrite功能并加入mode_rewrite模块内容】     
251     RewriteCond %{HTTP_REFERER} !^http://qz.com/.*$ [NC]   
252     RewriteCond %{HTTP_REFERER} !^http://qz.com$ [NC]
253     RewriteCond %{HTTP_REFERER} !^http://www.qz.com/.*$ [NC]
254     RewriteCond %{HTTP_REFERER} !^http://www.qz.com/$ [NC]
255     RewriteRule .*\.(gif|jpg|swf)$ http://www.qz.com/fuck.png  【设置跳转动作】

mode_rewrite模块内容字段含义

  • RewriteCond %{HTTP_REFERER} !^http://www.qz.com/.*$ [NC]
    • %{HTTP_REFERER}
      存放一个链接的URL,表示从某个链接访问所需的网页
    • !^
      表示不以后面的字符串开头
    • http://www.qz.com/
      本网站的路径,按整个字符串匹配
    • .*$
      表示以任意字符结尾
    • [NC]
      表示不区分大小写
  • RewriteRule .*\.(gif|jpg|swf)$ http://www.qz.com/fuck.png
    • .
      表示匹配一个字符
    • *
      表示匹配0到多个字符,与.合起来的意思则为匹配0到多次前面的任意字符,如果是1到多次匹配则可用+表示
    • .
      这里的\是转义符,.就代表符号.的意思。
      因为.在指令中是属于规则字符,有相应的含义,所以如果需要匹配,则需要在前面加个转义符\,,其他规则字符需要匹配,也许做同样的处理
    • (gif|jpg|swf)$
      表示匹配gif、jpg、swf任意一个,$表示结束。最后的规则是以.gif、.jpg、.swf结尾,前面1到多个字符的字符串,也就是匹配图片类型的文件
    • http://www.qz.com/fuck.png
      表示转发到这个路径
  • 整个配置的含义
    使用本网站以外的网站域名访问本站的图片文件时,显示fuck.png这个图片

3.网页准备

Web源主机配置

[root@localhost /]# cd /usr/local/httpd/htdocs/
[root@localhost htdocs]# ls
bbs  fuck.png  index.html  index.php  tea.jpg
[root@localhost htdocs]# vim index.html <html><body><h1>this is tea!</h1>
<img src="tea.jpg"/>
</body></html>[root@localhost htdocs]# echo "192.168.131.13 www.qz.com" >> /etc/hosts
[root@localhost htdocs]# echo "192.168.131.9 www.qzqz.com" >> /etc/hosts

盗链网站主机配置

[root@localhost /]# yum -y install httpd   
[root@localhost /]# cd /var/www/html/
【yum安装的httpd服务的默认路径为/var/www/html/】
【编译安装的httpd服务的默认路径为cd /usr/local/httpd/htdocs/[root@localhost html]# ls
index.html
[root@localhost html]# vim index.html <html><body>this is dao!
<img src="http://www.qz.com/tea.jpg"/>
</body></html>
[root@localhost html]# echo "192.168.131.13 www.qz.com" >> /etc/hosts
[root@localhost html]# echo "192.168.131.9 www.qzqz.com" >> /etc/hosts
  • 在盗图网站主机上进行浏览器验证
  • hhtp://www.qzqz.com
    在这里插入图片描述

这篇关于Apache网页与优化(压缩、缓存、版本信息隐藏、防盗链设置)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/798751

相关文章

关于C++中的虚拟继承的一些总结(虚拟继承,覆盖,派生,隐藏)

1.为什么要引入虚拟继承 虚拟继承是多重继承中特有的概念。虚拟基类是为解决多重继承而出现的。如:类D继承自类B1、B2,而类B1、B2都继承自类A,因此在类D中两次出现类A中的变量和函数。为了节省内存空间,可以将B1、B2对A的继承定义为虚拟继承,而A就成了虚拟基类。实现的代码如下: class A class B1:public virtual A; class B2:pu

[word] word设置上标快捷键 #学习方法#其他#媒体

word设置上标快捷键 办公中,少不了使用word,这个是大家必备的软件,今天给大家分享word设置上标快捷键,希望在办公中能帮到您! 1、添加上标 在录入一些公式,或者是化学产品时,需要添加上标内容,按下快捷键Ctrl+shift++就能将需要的内容设置为上标符号。 word设置上标快捷键的方法就是以上内容了,需要的小伙伴都可以试一试呢!

uniapp接入微信小程序原生代码配置方案(优化版)

uniapp项目需要把微信小程序原生语法的功能代码嵌套过来,无需把原生代码转换为uniapp,可以配置拷贝的方式集成过来 1、拷贝代码包到src目录 2、vue.config.js中配置原生代码包直接拷贝到编译目录中 3、pages.json中配置分包目录,原生入口组件的路径 4、manifest.json中配置分包,使用原生组件 5、需要把原生代码包里的页面修改成组件的方

问题-windows-VPN不正确关闭导致网页打不开

为什么会发生这类事情呢? 主要原因是关机之前vpn没有关掉导致的。 至于为什么没关掉vpn会导致网页打不开,我猜测是因为vpn建立的链接没被更改。 正确关掉vpn的时候,会把ip链接断掉,如果你不正确关掉,ip链接没有断掉,此时你vpn又是没启动的,没有域名解析,所以就打不开网站。 你可以在打不开网页的时候,把vpn打开,你会发现网络又可以登录了。 方法一 注意:方法一虽然方便,但是可能会有

模型压缩综述

https://www.cnblogs.com/shixiangwan/p/9015010.html

如何设置windows计划任务

如何设置windows计划任务 前言:在工作过程中写了一个python脚本,用于调用jira接口查询bug单数量,想要在本地定时任务执行,每天发送到钉钉群提醒,写下操作步骤用于记录。 1. 准备 Python 脚本 确保你的 Python 脚本已经保存到一个文件,比如 jira_reminder.py。 2. 创建批处理文件 为了方便任务计划程序运行 Python 脚本,创建一个批处理文

服务器雪崩的应对策略之----SQL优化

SQL语句的优化是数据库性能优化的重要方面,特别是在处理大规模数据或高频访问时。作为一个C++程序员,理解SQL优化不仅有助于编写高效的数据库操作代码,还能增强对系统性能瓶颈的整体把握。以下是详细的SQL语句优化技巧和策略: SQL优化 1. 选择合适的数据类型2. 使用索引3. 优化查询4. 范式化和反范式化5. 查询重写6. 使用缓存7. 优化数据库设计8. 分析和监控9. 调整配置1、

FastAdmin/bootstrapTable 表格中生成的按钮设置成文字

公司有个系统后台框架用的是FastAdmin,后台表格的操作栏按钮只有图标,想要设置成文字。 查资料后发现其实很简单,主需要新增“text”属性即可,如下 buttons: [{name: 'acceptcompany',title: '复核企业',text:'复核企业',classname: 'btn btn-xs btn-primary btn-dialog',icon: 'fa fa-pe

Java中如何优化数据库查询性能?

Java中如何优化数据库查询性能? 大家好,我是免费搭建查券返利机器人省钱赚佣金就用微赚淘客系统3.0的小编,也是冬天不穿秋裤,天冷也要风度的程序猿!今天我们将深入探讨在Java中如何优化数据库查询性能,这是提升应用程序响应速度和用户体验的关键技术。 优化数据库查询性能的重要性 在现代应用开发中,数据库查询是最常见的操作之一。随着数据量的增加和业务复杂度的提升,数据库查询的性能优化显得尤为重

众所周知,配置即代码≠基础设置即代码

​前段时间翻到几条留言,问: “配置即代码和基础设施即代码一样吗?” “配置即代码是什么?怎么都是基础设施即代码?” 我们都是知道,DevOp的快速发展,让服务器管理与配置的时间大大减少,配置即代码和基础设施即代码作为DevOps的重要实践,在其中起到了关键性作用。 不少人将二者看作是一件事,配置即大代码是关于管理特定的应用程序配置设置本身,而基础设施即代码更关注的是部署支持应用程序环境所需的