Apache webpage and security optimization-webpage caching (connotation experiment)

Foreword:

  • Apache’s mod_expries module automatically generates Express tags and Cache-Control tags in the header information of the page.
  • The client browser determines according to the tag that the next visit is to fetch the page in the cache of the local machine, and does not need to make a request to the server again, thereby reducing the client’s access frequency and number of times, achieving the purpose of reducing unnecessary traffic and increasing access speed

First, configure web cache

1.1 Environment configuration
  • Mount shared files and extract them to / opt /
 [@localhost ~]# mount.cifs //192.168.181.1/LAMP-C7 /mnt Password for root@//192.168.181.1/ LAMP-C7 : [@localhost ~]# cd /mnt [root@localhost mnt]# ls amoeba-mysql-binary-2.2.0.tar.gz Discuz_X2.5_SC_UTF8.zip mha.rar apr-1.6.2.tar.gz httpd-2.4.29.tar.bz2 mysql-5.6.26.tar.gz apr-util-1.6.0.tar.gz LNMP-C7 php-5.6.11.tar.bz2 awstats-7.6.tar.gz LNMP-C7.rar cronolog-1.6.2-14.el7.x86_64.rpm mha 
  • Extract the cross-platform component package and source package
 [root@localhost mnt]# tar zxvf apr-1.6.2.tar.gz -C /opt [root@localhost mnt]# tar zxvf apr-util-1.6.0.tar.gz -C /opt ...............省略部分内容[root@localhost mnt]# tar jxvf httpd-2.4.29.tar.bz2 -C /opt ...............省略部分内容[root@localhost mnt]# cd /opt [root@localhost opt]# ls apr-1.6.2 apr-util-1.6.0 httpd-2.4.29 rh [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 
  • Install compilation tools
 [root@lamp opt]# yum -y install \ > gcc \ > gcc-c++ \ > make \ > pcre-devel \ > zlib-devel \ > expat-devel \ > pcre \ > perl ...........省略部分内容 
  • Configuration compilation
 [root@lamp opt]# cd /opt/httpd-2.4.29/ [root@lamp httpd-2.4.29]# ./configure \ --prefix=/usr/local/httpd \ --enable-so \ --enable-deflate \ --enable-expires \ --enable-rewrite \ --enable-charset-lite \ --enable-cgi ........省略部分内容[root@localhost httpd-2.4.29]# make #生产可执行的二进制文件........省略部分内容[root@localhost httpd-2.4.29]# make install #复制二进制文件到系统,配置应用环境........省略部分内容 
1.2 Modify the configuration file
Insert picture description here

To enable web caching:

  • Create soft links for easy management
 [root@localhost httpd-2.4.29]# ln -s /usr/local/httpd/conf/httpd.conf /etc/httpd.conf 
  • Modify the configuration file
 Listen 192.168.181.173 : 80 #开启ipv4监听,ip地址指向本机(51行) #Listen 80 #关闭ipv6端口监听(52行) ServerName www.demo02. com : 80 #修改域名(198行) LoadModule expires_module modules/mod_expires.so #启用expires模块(111行) #跳转到末行添加以下内容<IfModule mod_expires.c> #对expires模块进行配置ExpiresActive On #开启功能ExpiresDefault "access plus 50 seconds" #设置缓存时间为50秒</IfModule> -------wq 
  • Check syntax
 [root@localhost httpd-2.4.29]# cd /usr/local/httpd/bin/ [root@localhost bin]# ./apachectl -t Syntax OK 
  • Turn on Apache services, turn off firewalls, and enhance security features
 [root@localhost bin]# ./apachectl start [root@localhost bin]# netstat -natp | grep 80 tcp 0 0 192.168.181.129 : 80 0.0.0.0 : * LISTEN 46246/httpd [root@localhost bin]# systemctl stop firewalld.service [root@localhost bin]# setenforce 0 [root@localhost bin]# ./apachectl -t -D DUMP_MOOULES | grep "expire" #查看expire模块是否启用Syntax OK 

Verification

2.1 Verifying cache settings with packet capture tools
  • Start a win10 virtual machine (package capture tool is installed)
Insert picture description here
  • Visit the site and view packet capture tools

Both the first visit and subsequent refresh visits have cache time information. At this time, we can modify the expires module in the configuration file at any time to update the cache time.

to sum up:

Web page caching is relatively simple, and we will continue to introduce the anti-hotlink and hidden versions of Apache web pages and security optimization.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.