Apache优化
文章目录
Apache网页优化
网页压缩
网页优化概述
在企业中,部署Apache后会有默认的配置参数,如果不及时进行优化配置,在当今互联网时代,会引发网站很多问题
为了适应企业需求,就需要考虑如何提升Apache的性能与稳定性,这就是Apache的优化内容
优化内容
配置网页压缩功能
工作模式的选择与参数优化
配置防盗链
配置隐藏版本号
…
网页gzip概述
配置Apache的网页压缩功能,是使用gzip压缩算法来对网页内容进行压缩后在传输到客户端浏览器
作用
降低了网络传输的字节数,加快网页加载的速度
节省流量,改善用户的浏览体验
gzip与搜索引擎的抓取工作有着更好的关系
Apache的压缩模块
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加载速度更快
配置网页压缩功能
启用网页压缩功能步骤
1:查看是否安装mod_deflate模块
2:修改配置文件,启用压缩功能
3:抓包测试
检查是否安装mod_deflate模块
执行apachectl -t -D DUMP_MODULES命令
如果输出中没有deflate_module(static),说明编译时没有安装mod_deflate模块
没有安装则需要从新编译安装
./configure --enable-deflate…
make && make install
实验
[root@localhost ~]# cd /opt
[root@localhost opt]# mkdir apache
[root@localhost opt]# cd apache/
apr-1.6.2.tar.gz apr-util-1.6.0.tar.gz httpd-2.4.29.tar.bz2
[root@localhost apache]# tar xzvf apr-1.6.2.tar.gz
[root@localhost apache]# tar xzvf apr-util-1.6.0.tar.gz
[root@localhost apache]# tar xjvf httpd-2.4.29.tar.bz2
[root@localhost apache]# mv apr-1.6.2 httpd-2.4.29/srclib/apr
[root@localhost apache]# mv apr-util-1.6.0 httpd-2.4.29/srclib/apr-util
[root@localhost apache]# yum -y install \
> gcc \
> gcc-c++ \
> make \
> pcre-devel \
> expat-devel \
> perl
[root@localhost apache]# yum -y install zlib-devel
[root@localhost apache]# cd httpd-2.4.29/
[root@localhost httpd-2.4.29]# ./configure \
> --prefix=/usr/local/httpd \
> --enable-daflate \
> --enable-so \
> --enable-rewrite \
> --enable-charset-lite \
> --enable-cgi
[root@localhost httpd-2.4.29]# make && make install
[root@localhost httpd-2.4.29]# vim /usr/local/httpd/conf/httpd.conf
51 Listen 192.168.20.20:80
52 #Listen 80
198 ServerName www.hui.com:80
101 LoadModule filter_module modules/mod_filter.so
105 LoadModule deflate_module modules/mod_deflate.so
112 LoadModule headers_module modules/mod_headers.so
509 <IfMOdule mod_deflate.c>
510 AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript text/jpg text/png
511 DeflateCompressionLevel 9
512 SetOutputFilter DEFLATE
513 </Ifmodule>
[root@localhost httpd-2.4.29]# /usr/local/httpd/bin/httpd -t
Syntax OK
[root@localhost httpd-2.4.29]# /usr/local/httpd/bin/httpd
[root@localhost httpd-2.4.29]# netstat -ntap | grep httpd
tcp 0 0 192.168.20.20:80 0.0.0.0:* LISTEN 106654/httpd
[root@localhost httpd-2.4.29]# cd /usr/local/httpd/bin/
[root@localhost bin]# ./apachectl -t -D DUMP_MODULES | grep "deflate"
deflate_module (shared)
[root@localhost bin]# cd /usr/local/httpd/htdocs/
[root@localhost htdocs]# ls
01.png index.html
[root@localhost htdocs]# vim index.html
<html><body><h1>It works!</h1></body></html>
<img src="01.png"/>
[root@localhost htdocs]# systemctl stop firewalld.service
[root@localhost htdocs]# iptables -F
[root@localhost htdocs]# setenforce 0
网页缓存
配置网页的缓存时间概述
通过mod_expire模块配置Apache,使网页能在客户端浏览器缓存一段时间,以避免重复请求
启用mod_expire模块后,会自动生成页面头部信息中的Expires标签和Cache-Control标签,从而降低客户端的访问频率和次数,达到减少不必要的流量和增加访问速度的目的
启用网页的缓存功能步骤
1:检查是否安装mod_expire模块
2:修改配置文件启用缓存功能
3:抓包测试
启用功能步骤
查看是否安装mod_expire模块
/usr/local/apache/bin/apachectl -t -D DUMP_MODULES
如果输出中没有expires_module(static),则说明编译时没有安装mod_expires
若没有安装则需要重新编译安装
./configure --enable-expires…
make && make install
修改httpd.conf配置文件
启用mod_expires模块,并设置http协议下任意格式的文档均60秒后过期
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 60 seconds"
</IfModule>
重启httpd服务
再次访问测试网站,使用抓包工具Fiddler进行数据抓取分析
实验
[root@localhost ~]# cd /opt
[root@localhost opt]# mkdir apache
[root@localhost opt]# cd apache/
apr-1.6.2.tar.gz apr-util-1.6.0.tar.gz httpd-2.4.29.tar.bz2
[root@localhost apache]# tar xzvf apr-1.6.2.tar.gz
[root@localhost apache]# tar xzvf apr-util-1.6.0.tar.gz
[root@localhost apache]# tar xjvf httpd-2.4.29.tar.bz2
[root@localhost apache]# mv apr-1.6.2 httpd-2.4.29/srclib/apr
[root@localhost apache]# mv apr-util-1.6.0 httpd-2.4.29/srclib/apr-util
[root@localhost apache]# yum -y install \
> gcc \
> gcc-c++ \
> make \
> pcre-devel \
> expat-devel \
> perl
[root@localhost apache]# yum -y install zlib-devel
[root@localhost apache]# cd httpd-2.4.29/
[root@localhost httpd-2.4.29]# ./configure \
> --prefix=/usr/local/httpd \
> --enable-daflate \
> --enable-so \
> --enable-rewrite \
> --enable-charset-lite \
> --enable-cgi
[root@localhost httpd-2.4.29]# make && make install
[root@localhost httpd-2.4.29]# vim /usr/local/httpd/conf/httpd.conf
51 Listen 192.168.20.20:80
52 #Listen 80
198 ServerName www.hui.com:80
110 LoadModule expires_module modules/mod_expires.so
508 <IfModule mod_expires.c>
509 ExpiresActive On
510 ExpiresDefault "access plus 50 seconds"
511 </IfModule>
[root@localhost httpd-2.4.29]# cd /usr/local/httpd/bin/
[root@localhost bin]# ./apachectl -t
Syntax OK
[root@localhost bin]# ./apachectl
[root@localhost bin]# netstat -natp | grep httpd
tcp 0 0 192.168.20.20:80 0.0.0.0:* LISTEN 105910/httpd
[root@localhost bin]# ./apachectl -t -D DUMP_MODULES | grep "expires"
expires_module (shared)
[root@localhost bin]# cd /usr/local/httpd/htdocs/
[root@localhost htdocs]# ls
02.jpg index.html
[root@localhost htdocs]# vim index.html
<html><body><h1>It works!</h1></body></html>
<img src="02.jpg"/>
[root@localhost htdocs]# systemctl stop firewalld.service
[root@localhost htdocs]# iptables -F
[root@localhost htdocs]# setenforce 0
[root@localhost ~]# hostnamectl set-hostname hui
[root@localhost ~]# su
[root@hui ~]# iptables -F
[root@hui ~]# setenforce 0
[root@hui ~]# yum -y install bind
[root@hui ~]# vim /etc/named.conf
12 options {
13 listen-on port 53 { any; };
14 listen-on-v6 port 53 { ::1; };
15 directory "/var/named";
16 dump-file "/var/named/data/cache_dump.db";
17 statistics-file "/var/named/data/named_stats.txt";
18 memstatistics-file "/var/named/data/named_mem_stats.txt";
19 recursing-file "/var/named/data/named.recursing";
20 secroots-file "/var/named/data/named.secroots";
21 allow-query { any; };
[root@hui ~]# vim /etc/named.rfc1912.zones
25 zone "hui.com" IN {
26 type master;
27 file "hui.com.zone";
28 allow-update { none; };
29 };
[root@hui ~]# cd /var/named/
[root@hui named]# cp -p named.localhost hui.com.zone
[root@hui named]# vim hui.com.zone
10 www IN A 192.168.20.20
[root@hui named]# systemctl start named
[
root@hui named]# yum -y install zlib-devel
[root@hui httpd-2.4.29]# yum -y install gcc gcc-c++ make pcre-devel expat-devel perl
[root@hui named]# cd /opt
apr-1.6.2.tar.gz apr-util-1.6.0.tar.gz httpd-2.4.29.tar.bz2
[root@hui opt]# tar zvxf apr-util-1.6.0.tar.gz
[root@hui opt]# tar zvxf apr-1.6.2.tar.gz
[root@hui opt]# tar jvxf httpd-2.4.29.tar.bz2
[root@hui opt]# mv apr-1.6.2 /opt/httpd-2.4.29/srclib/apr
[root@hui opt]# mv apr-util-1.6.0 /opt/httpd-2.4.29/srclib/apr-util
[root@hui opt]# cd httpd-2.4.29/
[root@hui httpd-2.4.29]# ./configure \
> --prefix=/usr/local/httpd \
> --enable-deflate \
> --enable-so \
> --enable-rewrite \
> --enable-charset-lite \
> --enable-cgi
[root@hui httpd-2.4.29]# make && make install
[root@hui httpd-2.4.29]# ln -s /usr/local/httpd/conf/httpd.conf /etc/
[root@hui httpd-2.4.29]# vim /etc/httpd.conf
51 Listen 192.168.20.20:80
52 #Listen 80
156 LoadModule rewrite_module modules/mod_rewrite.so
198 ServerName www.hui.com:80
[root@hui httpd-2.4.29]# cd /usr/local/httpd/htdocs/
<html><body><h1>It works!</h1></body></html>
<img src="01.jpg"/>
[root@hui htdocs]# cd ..
[root@hui httpd]# cd bin
[root@hui bin]# ./apachectl start
另一台主机做盗链
[root@localhost ~]# hostnamectl set-hostname daolian
[root@localhost ~]# su
[root@daolian ~]# yum -y install httpd
[root@daolian ~]# vim /etc/httpd/conf/httpd.conf
41 Listen 192.168.20.10:80
42 #Listen 80
95 ServerName www.fang.com:80
[root@daolian ~]# cd /var/www/html/
[root@daolian html]# vim index.html
<h1>this is fang</h1>
<img src="http://www.hui.com/01.jpg"/>
[root@daolian html]# echo "nameserver 192.168.20.20" > /etc/resolv.conf
[root@daolian html]# systemctl start httpd.service
[root@daolian html]# systemctl stop firewalld.service
[root@daolian html]# setenforce 0
[root@daolian html]# iptables -F
[root@hui httpd-2.4.29]# vim /etc/httpd.conf
156 LoadModule rewrite_module modules/mod_rewrite.so
249 RewriteEngine On
250 RewriteCond %{HTTP_REFERER} !^http://hui.com/.*$ [NC]
251 RewriteCond %{HTTP_REFERER} !^http://hui.com$ [NC]
252 RewriteCond %{HTTP_REFERER} !^http://www.hui.com/.*$ [NC]
253 RewriteCond %{HTTP_REFERER} !^http://www.hui.com/$ [NC]
254 RewriteRule . *\.(gif|jpg|swf)$ http://www.hui.com/01.png
[root@hui httpd-2.4.29]# cd /usr/local/httpd/htdocs/
[root@hui htdocs]# ls
01.jpg 01.png index.html
[root@hui htdocs]# cd /usr/local/httpd/bin/
[root@hui bin]# ./apachectl stop
[root@hui bin]# ./apachectl start
隐藏版本信息实例
[root@hui htdocs]# vim /etc/httpd.conf
496 Include conf/extra/httpd-default.conf
[root@hui htdocs]# cd ../conf/extra/
[root@hui extra]# ls
httpd-autoindex.conf httpd-default.conf httpd-languages.conf httpd-mpm.conf httpd-ssl.conf httpd-vhosts.conf
httpd-dav.conf httpd-info.conf httpd-manual.conf httpd-multilang-errordoc.conf httpd-userdir.conf proxy-html.conf
[root@hui extra]# vim httpd-default.conf
55 ServerTokens Prod
[root@hui extra]# cd ../../bin
[root@hui bin]# ./apachectl stop
[root@hui bin]# ./apachectl start