博主,最近考試,本篇博客詳細介紹了,如何用apache進行頁面壓縮、修改網頁快取時間節省資源、防盜鏈服務和隱藏版本的來優化LAMP架構,
文章目錄
- 概述
- 一:網頁壓縮
- 1.1:網頁壓縮zip概述
- 1.2:Apache的壓縮模塊
- 1.3:首先安裝apache
- 1.4:安裝編譯器和其他工具
- 1.5:configure配置
- 1.6:make編譯make install
- 1.7:修改組態檔
- 1.8:驗證語法重啟服務
- 1.9:編輯測驗網頁
- 二:網頁快取
- 2.1:Apache網頁快取實驗
- 2.2:安裝編譯器和其他工具
- 2.3:configure配置
- 2.4:make編譯make install
- 2.5:修改組態檔
- 2.6:重啟服務
- 三:Apache防盜鏈服務
- 3.1:什么是防盜鏈
- 3.2:環境介紹
- 3.3:模仿盜鏈程序
- 3.4:主機開啟防盜鏈功能
- 3.5:修改組態檔
- 3.6:驗證組態檔重啟服務
- 四:隱藏版本資訊
- 4.1:隱藏Apache版本資訊作用
- 4.2:配置Apache隱藏版本資訊
- 4.3:修改版本資訊
- 4.4:訪問驗證
- 五:本次實驗所用圖片
概述
網頁壓縮來進一步提升網頁的瀏覽速度,它完全不需要任何的成本,只不過是會讓您的服務器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 8月 26 17:58 anaconda-ks.cfg
-rw-r--r--. 1 root root 1071074 10月 13 21:35 apr-1.6.2.tar.gz
-rw-r--r--. 1 root root 565507 10月 13 21:35 apr-util-1.6.0.tar.gz
-rw-r--r--. 1 root root 6567926 10月 13 21:35 httpd-2.4.29.tar.bz2
-rw-r--r--. 1 root root 1955 8月 26 18:30 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 6 8月 26 18:31 公共
drwxr-xr-x. 2 root root 6 8月 26 18:31 模板
drwxr-xr-x. 2 root root 6 8月 26 18:31 視頻
drwxr-xr-x. 2 root root 6 8月 26 18:31 圖片
drwxr-xr-x. 2 root root 6 8月 26 18:31 檔案
drwxr-xr-x. 2 root root 6 8月 26 18:31 下載
drwxr-xr-x. 2 root root 6 8月 26 18:31 音樂
drwxr-xr-x. 2 root root 6 8月 26 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 10月 13 22:48 20201013120035500.png
-rw-r--r--. 1 root root 2506246 10月 13 22:39 a.png
-rw-r--r--. 1 root root 76 10月 13 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 8月 26 17:58 anaconda-ks.cfg
-rw-r--r--. 1 root root 1071074 10月 13 21:35 apr-1.6.2.tar.gz
-rw-r--r--. 1 root root 565507 10月 13 21:35 apr-util-1.6.0.tar.gz
-rw-r--r--. 1 root root 6567926 10月 13 21:35 httpd-2.4.29.tar.bz2
-rw-r--r--. 1 root root 1955 8月 26 18:30 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 6 8月 26 18:31 公共
drwxr-xr-x. 2 root root 6 8月 26 18:31 模板
drwxr-xr-x. 2 root root 6 8月 26 18:31 視頻
drwxr-xr-x. 2 root root 6 8月 26 18:31 圖片
drwxr-xr-x. 2 root root 6 8月 26 18:31 檔案
drwxr-xr-x. 2 root root 6 8月 26 18:31 下載
drwxr-xr-x. 2 root root 6 8月 26 18:31 音樂
drwxr-xr-x. 2 root root 6 8月 26 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:訪問驗證

五:本次實驗所用圖片


轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/178983.html
標籤:其他
