学习企业级网站优化一篇就够了!(LAMP之压缩、缓存、防盗链、隐藏版本)

博主,最近考试。本篇博客详细介绍了,如何用apache进行页面压缩、修改网页缓存时间节省资源、防盗链服务和隐藏版本的来优化LAMP架构。

概述

网页压缩来进一步提升网页的浏览速度,它完全不需要任何的成本,只不过是会让您的服务器CPU占用率稍微提升一两个百分点而已或者更少.
网页压缩是一项由 WEB 服务器和浏览器之间共同遵守的协议,也就是说 WEB 服务器和浏览器都必须支持该技术,流行的浏览器都是支持的,包括 IE、FireFox、Opera 等;服务器有 Apache 和 IIS 等。双方的协商过程如下:

1、首先浏览器请求某个 URL 地址,并在请求的头 (head) 中设置属性 accept-encoding 值为 gzip, deflate,表明浏览器支持 gzip 和 deflate 这两种压缩方式(事实上 deflate 也是使用 gzip 压缩协议);

2、WEB 服务器接收到请求后判断浏览器是否支持压缩,如果支持就传送压缩后的响应内容,否则传送不经过压缩的内容;

3、浏览器获取响应内容后,判断内容是否被压缩,如果是则解压缩,然后显示响应页面的内容。

在实际的应用中我们发现压缩的比率往往在 3 到 10 倍,也就是本来 50k 大小的页面,采用压缩后实际传输的内容大小只有 5 至 15k大小,这可以大大节省服务器的网络带宽。

一:网页压缩

1.1:网页压缩zip概述

  • 配置Apache的网页压缩功能,是使用gzip压缩算法来对网页内容进行压缩后在传输到客户端浏览器
  • 作用
    • 降低了网络传输的字节数,加快网页加载的速度
    • 节省流量,改善用户的浏览体验
    • gzip与搜索引擎的抓取工作有着更好的关系

1.2: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.3:首先安装apache

[root@server3 ~]# ll
总用量 8032
-rw-------.  1 root root     1907 826 17:58 anaconda-ks.cfg
-rw-r--r--.  1 root root  1071074 1013 21:35 apr-1.6.2.tar.gz
-rw-r--r--.  1 root root   565507 1013 21:35 apr-util-1.6.0.tar.gz
-rw-r--r--.  1 root root  6567926 1013 21:35 httpd-2.4.29.tar.bz2
-rw-r--r--.  1 root root     1955 826 18:30 initial-setup-ks.cfg
drwxr-xr-x.  2 root root        6 826 18:31 公共
drwxr-xr-x.  2 root root        6 826 18:31 模板
drwxr-xr-x.  2 root root        6 826 18:31 视频
drwxr-xr-x.  2 root root        6 826 18:31 图片
drwxr-xr-x.  2 root root        6 826 18:31 文档
drwxr-xr-x.  2 root root        6 826 18:31 下载
drwxr-xr-x.  2 root root        6 826 18:31 音乐
drwxr-xr-x.  2 root root        6 826 18:31 桌面
[root@server3 ~]# tar xf apr-1.6.2.tar.gz
[root@server3 ~]# tar xf apr-util-1.6.0.tar.gz
[root@server3 ~]# tar xf httpd-2.4.29.tar.bz2
[root@server3 ~]# mv apr-1.6.2 httpd-2.4.29/srclib/apr
[root@server3 ~]# mv apr-util-1.6.0 httpd-2.4.29/srclib/apr-util 

1.4:安装编译器和其他工具

[root@server3 ~]# yum -y install gcc gcc-c++ make pcre pcre-devel zlib-devel expat-devel perl

1.5:configure配置

[root@server3 ~]# cd httpd-2.4.29/
[root@server3 httpd-2.4.29]# ls
ABOUT_APACHE     BuildBin.dsp    config.status  httpd.mak       libhttpd.mak  modules.o         server
acinclude.m4     buildconf       configure      httpd.spec      LICENSE       NOTICE            srclib
Apache-apr2.dsw  buildmark.o     configure.in   include         Makefile      NWGNUmakefile     support
Apache.dsw       CHANGES         docs           INSTALL         Makefile.in   os                test
apache_probes.d  CMakeLists.txt  emacs-style    InstallBin.dsp  Makefile.win  README            VERSIONING
ap.d             config.layout   httpd          LAYOUT          modules       README.cmake
build            config.log      httpd.dep      libhttpd.dep    modules.c     README.platforms
BuildAll.dsp     config.nice     httpd.dsp      libhttpd.dsp    modules.lo    ROADMAP
[root@server3 httpd-2.4.29]# ./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi --enable-cgid --enable-deflate

1.6:make编译make install

[root@server3 httpd-2.4.29]# make && make install

1.7:修改配置文件

[root@server3 ~]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
[root@server3 ~]# vi /etc/init.d/httpd 
[root@server3 ~]# chkconfig --add httpd
[root@server3 ~]# ln -s /usr/local/httpd/conf/ /httpd.conf /etc/
[root@server3 ~]# ln -s /usr/local/httpd/bin/* /usr/local/bin/
[root@server3 ~]# vi /usr/local/httpd/conf/httpd.conf 
LoadModule headers_module modules/mod_headers.so#/搜索/deflate,若没有,需要检查之前配置重新编译   搜索就取消注释
末行添加

ADDOutputFilterByType DEFLATE text/html eext/css text/plain text/xml text/javascript image/png image/jpg image/jepg image/gif application/x-httpd-php application/x-javascript
DeflateCompressionLevel 9
SetOutputFilter DEFLATE

1.8:验证语法重启服务

[root@server3 ~]# httpd  -t
[root@server3 ~]# systemctl stop firewalld.service 
[root@server3 ~]# setenforce 0
[root@server3 ~]# systemctl start httpd
[root@server3 ~]# netstat -anpt |grep 80
tcp6       0      0 :::80                   :::*                    LISTEN      36409/httpd         

1.9:编辑测试网页

测试网页放入一张图片
[root@server3 ~]# cd /usr/local/httpd/htdocs/
[root@server3 htdocs]# ll
总用量 4900
-rw-r--r--. 1 root root 2506246 1013 22:48 20201013120035500.png
-rw-r--r--. 1 root root 2506246 1013 22:39 a.png
-rw-r--r--. 1 root root      76 1013 22:50 index.html
[root@server3 htdocs]# cat index.html 
<html><body><h1>apache</h1><img src="20201013120035500.png"/></body></html>
  • 打开宿主机
  • 安装fiddler工具

在这里插入图片描述

在这里插入图片描述
发现网页支持压缩

二:网页缓存

配置网页的缓存时间概述

  • 通过mod_expire模块配置Apache,使网页能在客户端浏览器缓存一段时间,以避免重复请求
  • 启用mod_expire模块后,会自动生成页面头部信息中的Expires标签和Cache-Control标签,从而降低客户端的访问频率和次数,达到减少不必要的流量和增加访问速度的目的

2.1:Apache网页缓存实验

  • 跟网页压缩环境相同
[root@server3 ~]# ll
总用量 8032
-rw-------.  1 root root     1907 826 17:58 anaconda-ks.cfg
-rw-r--r--.  1 root root  1071074 1013 21:35 apr-1.6.2.tar.gz
-rw-r--r--.  1 root root   565507 1013 21:35 apr-util-1.6.0.tar.gz
-rw-r--r--.  1 root root  6567926 1013 21:35 httpd-2.4.29.tar.bz2
-rw-r--r--.  1 root root     1955 826 18:30 initial-setup-ks.cfg
drwxr-xr-x.  2 root root        6 826 18:31 公共
drwxr-xr-x.  2 root root        6 826 18:31 模板
drwxr-xr-x.  2 root root        6 826 18:31 视频
drwxr-xr-x.  2 root root        6 826 18:31 图片
drwxr-xr-x.  2 root root        6 826 18:31 文档
drwxr-xr-x.  2 root root        6 826 18:31 下载
drwxr-xr-x.  2 root root        6 826 18:31 音乐
drwxr-xr-x.  2 root root        6 826 18:31 桌面
[root@server3 ~]# tar xf apr-1.6.2.tar.gz
[root@server3 ~]# tar xf apr-util-1.6.0.tar.gz
[root@server3 ~]# tar xf httpd-2.4.29.tar.bz2
[root@server3 ~]# mv apr-1.6.2 httpd-2.4.29/srclib/apr
[root@server3 ~]# mv apr-util-1.6.0 httpd-2.4.29/srclib/apr-util 

2.2:安装编译器和其他工具

[root@server3 ~]# yum -y install gcc gcc-c++ make pcre pcre-devel zlib-devel expat-devel perl

2.3:configure配置

./configure  --prefix=/usr/local/httpd --enable-deflate  --enable-expires  --enable-so  --enable-rewrite  --enable-charset-lite --enable-expires #

2.4:make编译make install

[root@server3 httpd-2.4.29]# make && make install

2.5:修改配置文件

[root@server3 httpd-2.4.29]# vi /etc/httpd.conf 
LoadModule expires_module modules/mod_expires.so  #去掉前面注释
#末行添加
<IfModule mod_expires.c>
    ExpiresActive On                         #开启缓存功能
    ExpiresDefault "access plus 60 seconds"  #保持60s
</IfModule>

2.6:重启服务

[root@server3 httpd-2.4.29]# httpd -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::9a22:5aea:2642:6dff. Set the 'ServerName' directive globally to suppress this message
Syntax OK
[root@server3 httpd-2.4.29]# apachectl -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::9a22:5aea:2642:6dff. Set the 'ServerName' directive globally to suppress this message
Syntax OK
[root@server3 httpd-2.4.29]# netstat -anpt | grep httpd
tcp6       0      0 :::80                   :::*                    LISTEN      96339/httpd         
[root@server3 httpd-2.4.29]# systemctl restart httpd.service 


客户机网页测试
在这里插入图片描述
修改最大缓存时间为40s
在这里插入图片描述

三:Apache防盗链服务

3.1:什么是防盗链

  • 防盗链是为了防止别人的网站代码里面盗用我们自己服务器上的图片、文件、视频等相关资源
  • 如果别人盗用网站的这些静态资源,明显的是增大服务器的带宽压力
  • 作为网站的维护人员,要杜绝服务器的静态资源被其他网站盗用

3.2:环境介绍

IP地址 域名 用途
192.168.158.30 www. 服务器
192.168.158.10 www 盗链网站
客户端 Windows 10 客户机

3.3:模仿盗链过程

  • 两台主机配置测试页面
  • 盗链网站的测试页面,盗用源主机网站目录下的一个logo.jpg文件
  • 在Windows中访问验证
[root@server1 html]# cat index.html 
<html><body><h1>apache</h1><img src="http://192.168.158.30/20201013120035500.png"/></body></html>

在这里插入图片描述

在这里插入图片描述
图片不在本地

3.4:主机开启防盗链功能

[root@server3 htdocs]# vi /etc/httpd.conf 
LoadModule rewrite_module modules/mod_rewrite.so   #去注释

3.5:修改配置文件


#再合适位置添加以下内容
RewriteEngine On
    RewriteCond %{
    
    HTTP_REFERER} !^http://192.168.158.30$ [NC]
    RewriteCond %{
    
    HTTP_REFERER} !^http://192.168.158.30/* [NC]
    RewriteCond %{HTTP_REFERER} !^http://192.168.158.30/.*$ [NC]
    RewriteRule .*\.(gif|png|swf)$ http://192.168.158.30/error.jpg [R,NC]    #重写为网址的error图片

只有访问IP为192.168.158.30才能获得资源

3.6:验证配置文件重启服务

[root@server3 htdocs]# httpd -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::9a22:5aea:2642:6dff. Set the 'ServerName' directive globally to suppress this message
Syntax OK
[root@server3 htdocs]# apachectl -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::9a22:5aea:2642:6dff. Set the 'ServerName' directive globally to suppress this message
Syntax OK
[root@server3 htdocs]# netstat -anpt | grep httpd
tcp6       0      0 :::80                   :::*                    LISTEN      110626/httpd   
  • 测试效果

使用Windows主机测试
在这里插入图片描述
在这里插入图片描述
以上通过其他网站获取不到我服务器的资源

四:隐藏版本信息

4.1:隐藏Apache版本信息作用

攻击者往往是先通过扫描软件的版本信息然后进行针对性的攻击,通常每个版本都不是完美的,所以如果知道版本,就可以进行针对性的攻击,在apache安装完成应该第一时间隐藏它的版本信息

4.2:配置Apache隐藏版本信息

Apache的版本信息,透露了-定的漏洞信息,从而给
网站带来安全隐患生产环境中要配置Apache隐藏版本信息

4.3:修改版本信息

将主配置文件httpd.conf以下行注释去掉

[root@server3 conf]# pwd
/usr/local/httpd/conf
[root@server3 conf]# vi /etc/httpd.conf 
Include conf/extra/httpd-default.conf            ##去注释 
[root@server3 conf]# vi extra/httpd-default.conf 
ServerTokens Prod
Serversignature Off
选项 输出格式
ServerTokens Prod ServerTokens Major
ServerTokens Minor Server:Apache/2.0
ServerTokens Min Server:Apache/2.0.41
ServerTokens OS Server: Apache/2.0.41 (Unix)
ServerTokens Full Server: Apache/2.0.41 (Unix) PHP/4.2.2 MyMod/1.2

4.4:访问验证

在这里插入图片描述

五:本次实验所用图片

在这里插入图片描述

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qyf158236/article/details/109059382