4.Apache网页优化
文章目录
- Apache网页优化
- 网页压缩
- 网页缓存
- 隐藏版本信息
- Apache防盗链
Apache网页优化
Apache网页优化
- 网页压缩
- 网页缓存
Apache安全优化
- 隐藏版本信息
- 配置防盗链
网页压缩
配置Apache的网页压缩功能,是使用gzip压缩算法来对网页内容进行压缩后再传输到客户端浏览器
作用
- 降低了网络传输的字节数,加快网页加载的速度
- 节省流量,改善用户的浏览体验
- gzip与搜索引擎的抓取工具有着更好的关系
Apache实现网页压缩的功能模块包括
- mod_ gzip 模块
- mod_ deflate 模块
Apache 1.x
- 没有内建网页压缩技术,但可使用第三方mod_ _gzip 模块执行压缩
Apache 2.x
- 在开发的时候,内建了mod_ deflate 这个模块,取代mod_ gzip
mod_gzip 模块与mod_deflate 模块
- 两者均使用gzip压缩算法,运作原理类似
- mod_deflate压缩速度略快, 而mod_gzip的压缩比略高_
- mod_gzip对服务器CPU的占用要高一些
- 高流量的服务器使用mod_deflate可能会比mod_gzip 加载速度更快
apachectl -t -D DUMP_MODULES | grep "deflate" ##查看是否有相关模块
systemctl stop httpd
cd /usr/local/httpd/conf
mv httpd.conf httpd.conf.bak./configure \
--prefix=/usr/local/httpd \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi \
--enable-deflate make -j2
make install
vim /usr/local/httpd/conf/httpd.conf--52行--修改
Listen 192.198.80.10:80--199行--修改
ServerName www.ggl.com:80--105行--取消注释
LoadModule deflate_module modules/mod_deflate.so #开启mod_deflate 模块--末行添加--<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript text/jpg text/png #代表对什么样的内容启用gzip压缩
DeflateCompressionLevel 9 #代表压缩级别,范围为1~9
SetOutputFilter DEFLATE #代表启用deflate 模块对本站点的输出进行gzip压缩
</IfModule><IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript text/jpg text/png
DeflateCompressionLevel 9
SetOutputFilter DEFLATE
</IfModule>
apachectl -t #验证配置文件的配置是否正确
apachectl -t -D DUMP_MODULES | grep "deflate" #检查 mod_deflate 模块是否已安装deflate_module (shared) #已安装的正确结果systemctl start httpd
5.测试 mod_deflate 压缩是否生效
cd /usr/local/httpd/htdocs先将game.jpg文件传到/usr/local/httpd/htdocs目录下
vim index.html
<html><body><h1>It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!</h1>
<img src="game.jpg"/>
</body></html>
网页缓存
- 通过mod_ expire模块配置 Apache,使网页能在客户端浏览器缓存一段时间, 以避免重复请求
- 启用mod_ expire模块后, 会自动生成页面头部信息中的Expires标签和Cache-Control标签,客户端浏览器根据标签决定I下次访问是在本地机器的缓存中获取页面不需要向服务器再次发出请求,从而降低客户端的访问频率和次数,达到减少不必要的流量和增加访问速度的目的
apachectl -t -D DUMP_MODULES | grep "expires"检查是否安装 mod_expires 模块
systemctl stop httpd
cd /usr/local/httpd/conf
mv httpd.conf httpd.conf.bak1yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel./configure \
--prefix=/usr/local/httpd \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi \
--enable-deflate \
--enable-expires make -j2
make install
vim /usr/local/httpd/conf/httpd.conf--52行--修改
Listen 192.198.80.10:80--111行--取消注释
LoadModule expires_module modules/mod_expires.so #开启mod_expires 模块--199行--取消注释,修改
ServerName www.ggl.com:80--末行添加--<IfModule mod_expires.c>ExpiresActive On #打开网页缓存功能ExpiresDefault "access plus 60 seconds" #设置缓存60秒
</IfModule><IfModule mod_expires.c>ExpiresActive On ExpiresDefault "access plus 3600 seconds"
</IfModule>
apachectl -t #验证配置文件的配置是否正确
apachectl -t -D DUMP_MODULES | grep "expires" #检查 mod_deflate 模块是否已安装deflate_module (shared) #已安装的正确结果systemctl start httpd
隐藏版本信息
vim /usr/local/httpd/conf/httpd.conf--491行--取消注释
Include conf/extra/httpd-default.conf
vim /usr/local/httpd/conf/extra/httpd-default.conf--55行--修改
ServerTokens Prod #将原本的 Full 改为 Prod,只显示名称,没有版本
#ServerTokens 表示 Server 回送给客户端的响应头域是否包含关于服务器 OS 类型和编译过的模块描述信息。
systemctl restart httpd
Apache防盗链
##检查是否安装 mod_rewrite 模块apachectl -t -D DUMP_MODULES | grep "rewrite"
##配置 mod_rewrite 模块启用
vim /usr/local/httpd/conf/httpd.conf--157行--取消注释
LoadModule rewrite_module modules/mod_rewrite.so--224行--
<Directory "/usr/local/httpd/htdocs">Options Indexes FollowSymLinksAllowOverride NoneRequire all grantedRewriteEngine On #打开 rewrite 功能,加入 mode_rewrite 模块内容RewriteCond %{HTTP_REFERER} !^http://ggl.com/.*$ [NC] #设置匹配规则RewriteCond %{HTTP_REFERER} !^http://ggl.com$ [NC]RewriteCond %{HTTP_REFERER} !^http://www.ggl.com/.*$ [NC]RewriteCond %{HTTP_REFERER} !^http://www.ggl.com/$ [NC]RewriteRule .*\.(gif|jpg|swf)$ http://www.ggl.com/error.png #设置跳转动作
</Directory><Directory "/usr/local/httpd/htdocs">Options Indexes FollowSymLinksAllowOverride NoneRequire all grantedRewriteEngine On RewriteCond %{HTTP_REFERER} !^http://ggl.com/.*$ [NC] RewriteCond %{HTTP_REFERER} !^http://ggl.com$ [NC]RewriteCond %{HTTP_REFERER} !^http://www.ggl.com/.*$ [NC]RewriteCond %{HTTP_REFERER} !^http://www.ggl.com/$ [NC]RewriteRule .*\.(gif|jpg|swf)$ http://www.ggl.com/error.png
</Directory>
命令解释
RewriteCond % {HTTP_REFERER} !^http://www.kgc.com/.*$ [NC] 的字段含义:“%{HTTP_REFERER}” :存放一个链接的 URL,表示从哪个链接中转访问目录下的静态资源。
“!^” :表示不以后面的字符串开头。
“http://www.kgc.com” :是本网站的路径,按整个字符串匹配。
“.*$” :表示以任意字符结尾。
“[NC]” :表示不区分大小写字母。RewriteRule .*\.(gif|jpg|swf)$ http://www.kgc.com/error.png 的字段含义:
“.” :表示匹配一个字符。
“*” :表示匹配 0 到多个字符,与“.”合起来的意思是匹配 0 到多次前面的任意字符,如果是 1 到多次匹配可以用“+”表示。
“\.” :在这里的“\”是转义符,“\.”就代表符号“.”的意思。因为“.”在指令中是属于规则字符,有相应的含义, 如果需要匹配,需要在前面加个转义符“\”,其它规则字符如果需要匹配,也做同样处理。
“(gif|jpg|swf)” :表示匹配“gif”、“jpg”、“swf”任意一个,“$”表示结束。最后的规则是以“.gif”、“.jpg”、“.swf”结尾, 前面是1到多个字符的字符串,也就是匹配图片类型的文件。
“http://www.kgc.com/error.png” :表示转发到这个路径 。
4.Apache网页优化相关推荐
- Apache网页优化概述
Apache网页优化概述 Apache网页优化概述 一.网页压缩 1.检查是否安装mod_ deflate 模块 2.如果没有安装mod_deflate 模块,重新编译安装 Apache 添加 mod ...
- 手把手教你,嘴对嘴传达------Apache网页优化
文章目录 Apache网页优化 一.Apache网页压缩 1.网页优化概述 2. 网页gzip概述 3.Apache的压缩模块 4.配置网页压缩功能 5.Apache网页压缩实操 (1) 手动编译安装 ...
- 【Web网站服务】Apache网页优化
Apache网页优化 一.网页压缩 1.1网页压缩步骤 二.网页缓存 三.隐藏版本信息 五.Apache防盗链 一.网页压缩 在企业中,部署Apache后只采用默认的配置参数,会引发网站很多问题,换言 ...
- 【Web服务器】Apache网页优化
文章目录 一.Apache网页优化概述 1.优化内容 2.网页压缩 2.1gzip概述 2.2作用 2.3Apache的压缩模块 概述 mod_gzip模块与mod_deflate模块 3.配置网页压 ...
- Apache网页优化和安全优化
文章目录 一.Apache网页优化 1.1网页压缩 1.2配置网页压缩功能并进行验证 1.3配置网页的缓存时间并进行验证 二.Apache安全优化 2.1配置防盗链 2.2隐藏版本信息 一.Apach ...
- 【Apache 网页优化】
文章目录 一.Apahce 网页优化 1.网页压缩 2.网页缓存 二.Apachen的安全优化 1.隐藏版本信息 2.Apache 防盗链 一.Apahce 网页优化 1.网页压缩 1.检查是否安装 ...
- Apache网页优化部署
一.gzip介绍 配置Apache的网页压缩功能,是使用gzip压缩算法来对网页内容进行压缩后再传输到客户端浏览器 作用 降低了网络传输的字节数:加快网页加载的速度 节省流量,改善用户的浏览体验 gz ...
- Apache网页优化之网页压缩
Apache网页压缩技术 实验介绍:本实验在虚拟机的Linux系统上搭建http-2.4.2,并在配置过程中开启mod_deflate模块,实现网页的压缩功能,最终通过fiddler抓包工具,验证mo ...
- apache 压缩html,Apache网页优化之网页压缩
Apache网页压缩技术 实验介绍:本实验在虚拟机的Linux系统上搭建http-2.4.2,并在配置过程中开启mod_deflate模块,实现网页的压缩功能,最终通过fiddler抓包工具,验证mo ...
最新文章
- PHP----------php封装的一些简单实用的方法汇总
- django框架--路由系统
- java exec执行tar_用java调用rpmbuild 报错,同一条命令直接复制到终端却能运行
- 了解CUDA计算(一)
- 网络工程师面试PK--胜者为王
- GDB and core
- I/O 多路复用之select
- centos6 postgresql安装
- C语言bound函数,C/C++-STL中lower_bound与upper_bound的用法以及cmp函数
- 谨防职业病 IT人士必不可少的四类食物
- macOS 安装和管理多个Python版本
- 如何将常用软件在Finder工具栏上置顶?
- matlab iir滤波器设计 实验报告,IIR数字滤波器的设计实验报告
- 2021年考研经验分享(初试408分)
- 职业经理人的团队管理
- 漫画:面试过程的神回复
- css画钟表_css3实现钟表效果
- PMBOK(第六版) PMP笔记——《十三》第十三章(项目干系人管理)
- Linux—常用十大命令
- SonarQube安装及使用