啓用網頁壓縮功能步驟
【1】檢視是否安裝mod_deflate模組
【2】修改組態檔,啓用壓縮功能
AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript '設定對什麼樣的內容啓用gzip壓縮'
DeflateCompressionLevel '壓縮級別'
SetOutputFilter DEFLATE '啓用deflate模組對本站點的輸出進行gzip壓縮'
【3】存取測試
環境準備
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
[root@localhost ~]# cd /opt
[root@localhost opt]# ls
rh
[root@localhost opt]# rz -E
rz waiting to receive.
[root@localhost opt]# ls
apr-1.6.2.tar.gz apr-util-1.6.0.tar.gz httpd-2.4.29.tar.bz2 rh
[root@localhost opt]# tar jxvf httpd-2.4.29.tar.bz2
[root@localhost opt]# tar zxvf apr-1.6.2.tar.gz
[root@localhost opt]# tar zxvf apr-util-1.6.0.tar.gz
[root@localhost opt]# ls
apr-1.6.2 apr-util-1.6.0 httpd-2.4.29 rh
apr-1.6.2.tar.gz apr-util-1.6.0.tar.gz httpd-2.4.29.tar.bz2
[root@localhost opt]# mv apr-1.6.2 httpd-2.4.29/srclib/apr
[root@localhost opt]# mv apr-util-1.6.0 httpd-2.4.29/srclib/apr-util
安裝編譯器和其他工具
[root@localhost opt]# yum -y install \
> gcc \ '編譯器'
> gcc-c++ \ '編譯器'
> make \ 'make工具'
> pcre-devel \ '支援正則表達式的工具'
> expat-devel \ '使網站能解析標籤語言的工具'
> perl \ 'Perl語言工具'
> zlib-devel '壓縮功能軟體包'
configure設定
[root@localhost httpd-2.4.29]# ./configure \
> --prefix=/usr/local/httpd \
> --enable-deflate \ '開啓deflate壓縮功能'
> --enable-so \ '開啓核心功能模組'
> --enable-rewrite \ '開啓重寫功能,如防盜鏈保護'
> --enable-charset-lite \ '開啓字元集'
> --enable-cgi '開啓通用閘道器介面'
編譯安裝
[root@localhost httpd-2.4.29]# make
[root@localhost httpd-2.4.29]# make install
###組態檔路徑太長,建一個軟鏈接
###開啓Apache主組態檔
###必須開啓三個模組:headers(預設開啓)、deflate(需要自行開啓)、filter(預設開啓)
###在Apache主組態檔末尾新增deflate功能(放一張圖片在網頁中做測試檢視gzip)
###開啓deflate功能模組
###指明對html、plain、css、xml、javascript檔案,新增jpg、png圖片,要增加其他功能如視訊,就需要在後面新增型別就行了
###建立最高壓縮包(壓縮級別9)
###所有輸出檔案壓縮過濾
編輯組態檔
[root@localhost httpd-2.4.29]# ln -s /usr/local/httpd/conf/httpd.conf /etc/
[root@localhost httpd-2.4.29]# vim /etc/httpd.conf
112 LoadModule headers_module modules/mod_headers.so '頭部功能'
105 LoadModule deflate_module modules/mod_deflate.so '壓縮功能(去掉開頭#)'
101 LoadModule filter_module modules/mod_filter.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>
Listen 14.0.0.11:80 '開啓監聽埠'
#Listen 80 '註釋掉ipv6的監聽埠功能'
ServerName www.yyc.com:80 '設定服務域名'
[root@localhost httpd-2.4.29]# /usr/local/httpd/bin/httpd -t '驗證語法'
Syntax OK
[root@localhost httpd-2.4.29]# netstat -ntap | grep 80 '檢視埠狀態'
tcp6 0 0 :::80 :::* LISTEN 112181/httpd
'驗證指令碼在/usr/local/httpd/bin/中'
[root@localhost httpd-2.4.29]# cd /usr/local/httpd/bin/
[root@localhost bin]# ls
ab apu-1-config dbmmanage fcgistarter htdigest httxt2dbm
apachectl apxs envvars htcacheclean htpasswd logresolve
apr-1-config checkgid envvars-std htdbm httpd rotatelogs
[root@localhost bin]# ./apachectl -t ###驗證組態檔語法
Syntax OK
[root@localhost bin]# ./apachectl -t -D DUMP_MODULES | grep "deflate" '驗證模組'
deflate_module (shared) '說明網頁裡所有的圖片文字都是經過壓縮的'
更改首頁站點htdocs,加入一張圖片
[root@localhost bin]# cd /usr/local/httpd/
[root@localhost httpd]# ls
bin build cgi-bin conf error htdocs icons include lib logs man manual modules
[root@localhost httpd]# cd htdocs/
[root@localhost htdocs]# ls
index.html
[root@localhost htdocs]# rz -E '拖入jpg格式圖片,與網頁在同一路徑下'
rz waiting to receive.
[root@localhost htdocs]# ls
index.html test.jpg.jpg
[root@localhost htdocs]# vim index.html
<html><body><h1>It works!</h1></body></html>
<img src="test.jpg"/> '加入圖片'
###win10中安裝Fiddler軟體,安裝完開啓軟體,就開抓包了
###開啓網頁,存取14.0.0.11
可以看到request headers和response headers 裏面中的資訊都是經過壓縮的
網頁壓縮完成
通過mod_expire模組設定Apache,使網頁能在用戶端瀏覽器快取一段時間,以避免重複請求
啓用mod_expire模組後,會自動生成頁面頭部資訊中的Expires標籤和Cache-Control標籤,從而降低用戶端的存取頻率和次數,達到減少不必要的流量和增加存取速度的目的
啓用網頁的快取功能步驟
【1】檢視是否安裝mod_expire模組
【2】修改組態檔啓用快取功能
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 60 seconds"
</IfModule>
【3】抓包測試
環境準備
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
[root@localhost ~]# cd /opt
[root@localhost opt]# rz -E
rz waiting to receive.
[root@localhost opt]# ls
apr-1.6.2.tar.gz apr-util-1.6.0.tar.gz httpd-2.4.29.tar.bz2 rh
[root@localhost opt]# tar jxvf httpd-2.4.29.tar.bz2
[root@localhost opt]# tar zxvf apr-1.6.2.tar.gz
[root@localhost opt]# tar zxvf apr-util-1.6.0.tar.gz
[root@localhost opt]# mv apr-1.6.2 httpd-2.4.29/srclib/apr
[root@localhost opt]# mv apr-util-1.6.0 httpd-2.4.29/srclib/apr-util
裝軟體包環境,安裝編譯器和其他工具
[root@localhost opt]# yum -y install \
> gcc \ '編譯器'
> gcc-c++ \ '編譯器'
> make \ 'make工具'
> pcre-devel \ '支援正則表達式的工具'
> expat-devel \ '使網站能解析標籤語言的工具'
> perl \ 'Perl語言工具'
> zlib-devel '壓縮功能軟體包'
configure設定
[root@localhost httpd-2.4.29]# ./configure \
> --prefix=/usr/local/httpd \
> --enable-deflate \ '開啓deflate壓縮功能'
> --enable-so \ '開啓核心功能模組'
> --enable-rewrite \ '開啓重寫功能,如防盜鏈保護'
> --enable-charset-lite \ '開啓字元集'
> --enable-cgi '開啓通用閘道器介面'
編譯安裝
[root@localhost httpd-2.4.29]# make
[root@localhost httpd-2.4.29]# make install
###修改主組態檔
開啓快取模組功能
組態檔末尾新增:
[root@localhost httpd-2.4.29]# vim /usr/local/httpd/conf/httpd.conf
111 LoadModule expires_module modules/mod_expires.so '開啓快取模組功能'
...
509 <IfModule mod_expires.c>
510 ExpiresActive On '開啓快取功能'
511 ExpiresDefault "access plus 50 seconds" '儲存50秒'
512 </IfModule>
測試
[root@localhost httpd-2.4.29]# cd /usr/local/httpd/
[root@localhost httpd]# ls
bin build cgi-bin conf error htdocs icons include lib logs man manual modules
[root@localhost httpd]# cd bin/
[root@localhost bin]# ls
ab apu-1-config dbmmanage fcgistarter htdigest httxt2dbm
apachectl apxs envvars htcacheclean htpasswd logresolve
apr-1-config checkgid envvars-std htdbm httpd rotatelogs
[root@localhost bin]# ./apachectl -t
Syntax OK
[root@localhost bin]# ./apachectl '開啓'
[root@localhost bin]# netstat -ntap | grep httpd
tcp 0 0 14.0.0.11:80 0.0.0.0:* LISTEN 111216/httpd
[root@localhost bin]# ./apachectl -t -D DUMP_MODULES | grep "expires" '驗證模組'
expires_module (shared) '說明網頁裡所有的圖片文字都是經過壓縮的'
更改首頁站點htdocs
[root@localhost bin]# cd ..
[root@localhost httpd]# cd htdocs/
[root@localhost htdocs]# ls
index.html
[root@localhost htdocs]# rz -E
rz waiting to receive.
[root@localhost htdocs]# ls
index.html test.jpg
[root@localhost htdocs]# vim index.html
<html><body><h1>It works!</h1></body></html>
<img src="test.jpg"/>
網頁驗證
%{HTTP_REFERER}:瀏覽header中的鏈接欄位,存放一個鏈接的URL,代表是從哪個鏈接存取所需的網頁
!^:不以後面的字串開頭
.*$:以任意字元結尾
NC:不區分大寫
R:強制跳轉
規則匹配
規則匹配說明
RewriteEngine On:開啓網頁重寫功能
RewriteCond:設定匹配規則
RewriteRule:設定跳轉動作
修改組態檔,啓用防盜鏈功能並設定規則
RewriteEngine On
RewriteCond %{HTTP_REFERER}!^http://test.com/.*$ [NC]
RewriteCond %{HTTP_REFERER}!^http://test.com$ [NC]
RewriteCond %{HTTP_REFERER}!^http://www.test.com/.*$ [NC]
RewriteCond %{HTTP_REFERER}!^http://www.test.com$ [NC]
RewriteRule .*\.(gif|jpg|swf)$ http://www.test.com/error.html [R,NC]
[root@localhost ~]# hostnamectl set-hostname yyc '設定爲官方網站'
[root@localhost ~]# su
[root@yyc ~]# iptables -F
[root@yyc ~]# setenforce 0
設定dns
[root@yyc ~]# yum install bind -y
[root@yyc ~]# vim /etc/named.conf
options {
listen-on port 53 { any; };
listen-on-v6 port 53 { ::1; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
recursing-file "/var/named/data/named.recursing";
secroots-file "/var/named/data/named.secroots";
allow-query { any; };
[root@yyc ~]# vim /etc/named.rfc1912.zones
zone "localhost" IN {
type master;
file "named.localhost";
allow-update { none; };
};
zone "yyc.com" IN {
type master;
file "yyc.com.zone";
allow-update { none; };
};
[root@yyc ~]# cd /var/named/
[root@yyc named]# cp -p named.localhost yyc.com.zone
[root@yyc named]# vim yyc.com.zone
$TTL 1D
@ IN SOA @ rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @
A 127.0.0.1
www IN A 14.0.0.11
[root@yyc named]# systemctl start named
設定環境
[root@yyc named]# rz -E
rz waiting to receive.
[root@yyc named]# ls
apr-1.6.2.tar.gz dynamic named.empty slaves
apr-util-1.6.0.tar.gz httpd-2.4.29.tar.bz2 named.localhost yyc.com.zone
data named.ca named.loopback
[root@yyc named]# tar jxvf httpd-2.4.29.tar.bz2 -C /opt/
[root@yyc named]# tar zxvf apr-1.6.2.tar.gz -C /opt/
[root@yyc named]# tar zxvf apr-util-1.6.0.tar.gz -C /opt/
[root@yyc named]# cd /opt/
[root@yyc opt]# ls
apr-1.6.2 apr-util-1.6.0 httpd-2.4.29 rh
[root@yyc opt]# mv apr-1.6.2/ httpd-2.4.29/srclib/apr
[root@yyc opt]# mv apr-util-1.6.0/ httpd-2.4.29/srclib/apr-util
[root@yyc opt]# cd httpd-2.4.29/
[root@yyc httpd-2.4.29]# ls
ABOUT_APACHE BuildBin.dsp emacs-style LAYOUT NOTICE srclib
acinclude.m4 buildconf httpd.dep libhttpd.dep NWGNUmakefile support
Apache-apr2.dsw CHANGES httpd.dsp libhttpd.dsp os test
Apache.dsw CMakeLists.txt httpd.mak libhttpd.mak README VERSIONING
apache_probes.d config.layout httpd.spec LICENSE README.cmake
ap.d configure include Makefile.in README.platforms
build configure.in INSTALL Makefile.win ROADMAP
BuildAll.dsp docs InstallBin.dsp modules server
[root@yyc httpd-2.4.29]# yum install gcc gcc-c++ pcre pcre-devel perl expat-devel zlib-devel -y
'perl跨平臺'
[root@yyc httpd-2.4.29]# ./configure \
> --prefix=/usr/local/httpd \
> --enable-default \
> --enable-so \
> --enable-rewrite \
> --enable-charset-lite \
> --enable-cgi
[root@yyc httpd-2.4.29]# make&&make install
修改組態檔(暫不開啓防盜鏈),體驗盜鏈
[root@yyc httpd-2.4.29]# ln -s /usr/local/httpd/conf/httpd.conf /etc/
[root@yyc bin]# vim /usr/local/httpd/conf/httpd.conf
Listen 14.0.0.11:80
#Listen 80
...
ServerName www.yyc.com:80
更改首頁站點htdocs,做一個網站
[root@yyc httpd-2.4.29]# cd /usr/local/httpd/
[root@yyc httpd]# ls
bin build cgi-bin conf error htdocs icons include lib logs man manual modules
[root@yyc httpd]# cd htdocs/
[root@yyc htdocs]# ls
index.html
[root@yyc htdocs]# rz -E '拖入jpg格式的兩張圖片'
rz waiting to receive.
[root@yyc htdocs]# ls
error.jpg index.html test.jpg
[root@yyc htdocs]# vim index.html
<html><body><h1>It works!</h1></body></html>
<img src="test.jpg"/>
啓動服務
[root@yyc htdocs]# cd ..
[root@yyc httpd]# ls
bin build cgi-bin conf error htdocs icons include lib logs man manual modules
[root@yyc httpd]# cd bin/
[root@yyc bin]# ./apachectl start ###啓動服務
[root@yyc bin]# netstat -ntap | grep httpd
tcp6 0 0 :::80 :::* LISTEN 110843/httpd
正常存取驗證:
做盜鏈網站
新開一臺虛擬機器
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum install httpd -y
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
Listen 14.0.0.33:80
#Listen 80
...
ServerName www.test.com:80
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls
[root@localhost html]# vim index.html
<h1>this is test web</h1>
<img src="http://www.yyc.com/test.jpg"/> '指的是域名,需要DNS幫助解析'
'指定DNS解析'
[root@localhost html]# echo "nameserver 14.0.0.11" > /etc/resolv.conf
[root@localhost html]# systemctl start httpd
[root@localhost html]# netstat -ntap | grep httpd
tcp 0 0 14.0.0.33:80 0.0.0.0:* LISTEN 79140/httpd
'###驗證盜鏈網站(如果需要域名解析則再安裝bind)'
驗證盜鏈網站:
盜鏈成功
做防盜鏈設定
[root@yyc bin]# cd ..
[root@yyc httpd]# pwd
/usr/local/httpd
[root@yyc httpd]# cd conf/
[root@yyc conf]# ls
extra httpd.conf magic mime.types original
[root@yyc conf]# vim httpd.conf
LoadModule rewrite_module modules/mod_rewrite.so '開啓重寫功能模組(去掉開頭#)'
...
'#####/Diretcory搜尋,在這個標籤末尾加上防盜鏈設定'
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://yyc.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://yyc.com$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.yyc.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.yyc.com/$ [NC]
RewriteRule .*\.(gif|jpg|swf)$ http://www.yyc.com/error.jpg '把error.jpg圖片放到網頁站點目錄下'
'###重新啓動服務'
[root@yyc /]# cd /usr/local/httpd/
[root@yyc httpd]# ls
bin build cgi-bin conf error htdocs icons include lib logs man manual modules
[root@yyc httpd]# cd bin/
[root@yyc bin]# ls
ab apu-1-config dbmmanage fcgistarter htdigest httxt2dbm
apachectl apxs envvars htcacheclean htpasswd logresolve
apr-1-config checkgid envvars-std htdbm httpd rotatelogs
[root@yyc bin]# ./apachectl stop
[root@yyc bin]# ./apachectl start
###win10清除快取空間
防盜鏈成功
設定Apache隱藏版本資訊的必要性
設定Apache隱藏版本資訊詳解
正常情況下是可以通過抓包看到版本號的
[root@yyc bin]# cd ..
[root@yyc httpd]# cd conf/
[root@yyc conf]# ls
extra httpd.conf magic mime.types original
[root@yyc conf]# vim httpd.conf
# Virtual hosts
Include conf/extra/httpd-vhosts.conf ###開啓隱藏功能(去掉#)
修改httpd-default.conf組態檔
[root@yyc conf]# pwd
/usr/local/httpd/conf
[root@yyc conf]# cd extra/
[root@yyc extra]# ls
httpd-autoindex.conf httpd-info.conf httpd-mpm.conf httpd-userdir.conf
httpd-dav.conf httpd-languages.conf httpd-multilang-errordoc.conf httpd-vhosts.conf
httpd-default.conf httpd-manual.conf httpd-ssl.conf proxy-html.conf
[root@yyc extra]# vim httpd-default.conf
55 ServerTokens Prod '將Full改爲Prod'
[root@yyc extra]# cd ..
[root@yyc conf]# ls
extra httpd.conf magic mime.types original
[root@yyc conf]# cd ..
[root@yyc httpd]# ls
bin build cgi-bin conf error htdocs icons include lib logs man manual modules
[root@yyc httpd]# cd bin/
[root@yyc bin]# ls
ab apu-1-config dbmmanage fcgistarter htdigest httxt2dbm
apachectl apxs envvars htcacheclean htpasswd logresolve
apr-1-config checkgid envvars-std htdbm httpd rotatelogs
[root@yyc bin]# ./apachectl stop
[root@yyc bin]# ./apachectl start
瀏覽器網頁驗證: