启用网页压缩功能步骤
【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
510 AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript text/jpg text/png
511 DeflateCompressionLevel 9
512 SetOutputFilter DEFLATE
513
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
It works!
"test.jpg"/> '加入图片'
###win10中安装Fiddler软件,安装完打开软件,就开抓包了
###打开网页,访问14.0.0.11
可以看到request headers和response headers 里面中的信息都是经过压缩的
网页压缩完成
通过mod_expire模块配置Apache,使网页能在客户端浏览器缓存一段时间,以避免重复请求
启用mod_expire模块后,会自动生成页面头部信息中的Expires标签和Cache-Control标签,从而降低客户端的访问频率和次数,达到减少不必要的流量和增加访问速度的目的
启用网页的缓存功能步骤
【1】查看是否安装mod_expire模块
【2】修改配置文件启用缓存功能
ExpiresActive On
ExpiresDefault "access plus 60 seconds"
【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
510 ExpiresActive On '开启缓存功能'
511 ExpiresDefault "access plus 50 seconds" '保存50秒'
512
测试
[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
It works!
"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
It works!
"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
this is test web
"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
配置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