详述Linux系统中Apache网页与安全优化(二)
发表于:2024-09-27 作者:千家信息网编辑
千家信息网最后更新 2024年09月27日,网页缓存配置网页的缓存时间通过mod_ expire模块配置Apache,使网页能在客户端浏览器缓存一段时间,以避免重复请求启用mod_ expire模块后,会自动生成页面头部信息中的Expires标
千家信息网最后更新 2024年09月27日详述Linux系统中Apache网页与安全优化(二)查看是否安装了
修改
重启
配置
网页缓存
配置网页的缓存时间
- 通过
mod_ expire
模块配置Apache
,使网页能在客户端浏览器缓存一段时间,以避免重复请求 - 启用mod_ expire模块后,会自动生成页面头部信息中的
Expires
标签和Cache-Control
标签,从而降低客户端的访问频率和次数,达到减少不必要的流量和增加访问速度的目的
启用网页缓存功能步骤
- 查看是否安装mod_ expire模块
- 修改配置文件启用缓存功能
- 抓包测试
查看是否安装了mod_ expire
模块
/usr/local/apache/bin/apachectl -t -D DUMP_MODULES
- 如果输出中没有
expires_module (static)
, 则说明编译时没有安装mod_expires
如果没有安装则要重新编译安装
./configure --enable-expires...
make && make install
修改httpd.conf
配置文件
- 启用
mod_ expires
模块, 并设置http
协议下任意格式的文档均60
秒后过期<lfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 60 seconds"
</lfModule>
重启httpd
服务
再次访问测试网站,使用抓包工具Fiddler进行数据抓取分析
配置实例
- 在此处我将接着上一篇实验继续做,在上一篇实验中,手工编译安装
Apache
服务时已经安装过expires
模块,这里我直接测试
[root@localhost ~]# vim /usr/local/httpd/conf/httpd.conf...//省略部分内容...#LoadModule logio_module modules/mod_logio.soLoadModule env_module modules/mod_env.soLoadModule expires_module modules/mod_expires.so //找到此模块,去掉#开启模块LoadModule headers_module modules/mod_headers.so#LoadModule unique_id_module modules/mod_unique_id.so...//省略部分内容... //在文件末尾处添加以下条目,对expires模块进行配置 ExpiresActive On //开启功能 ExpiresDefault" access plus 50 seconds" //设置缓存时间 :wq //保存退出[root@localhost ~]# apachectl -t //验证语法格式Syntax OK //语法正常 [root@localhost ~]# service httpd stop //停止HTTP服务[root@localhost ~]# service httpd start //启动HTTP服务[root@localhost ~]# /usr/local/httpd/bin/apachectl -t -D DUMP_MODULES |grep "expires" //检测模块是否开启 expires_module (shared) //成功开启[root@localhost ~]# netstat -ntap | grep 80 //查看端口是否开启tcp 0 0 192.168.144.133:80 0.0.0.0:* LISTEN 47752/httpd
- 在客户机中访问网页,使用抓包工具看缓存模块是否开启
Apache安全优化
配置防盗链
- 防盗链就是防止别人的网站代码里面盗用服务器的图片、文件、视频等相关资源
- 如果别人盗用网站的这些静态资源,明显的是会增大服务器的带宽压力
- 所以作为网站的维护人员,要杜绝我们服务器的静态资源被其他网站盗用
配置Apache实现防盗链
检查
Apache
是否安装了mod_rewrite
模块/usr/local/apache/bin/apachectl -t -D DUMP_ MODULES
- 如果输出中没有
rewrite_module (static)
, 则说明编译时没有安装mod_ rewrite模块
- 如果没有安装则要重新编译安装
./configure --enable-rewrite...
make && make install
配置规则变量说明
%{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 .*\.(gifljipg|swf)$ http://www.test.com/error.html [R,NC]
配置实例
- 首先安装
DNS
服务,并配置DNS
服务,我们这里是同域名访问网页;在前面手工编译安装Apache
服务时已经安装过放掉连模块插件mod_rewrite
,直接进入HTTP主著配置文件进行配置。
[root@localhost ~]# yum install bind -y已加载插件:fastestmirror, langpacksbase | 3.6 kB 00:00 extras | 2.9 kB 00:00 ...//省略部分内容...已安装: bind.x86_64 32:9.11.4-9.P2.el7 作为依赖被安装: bind-export-libs.x86_64 32:9.11.4-9.P2.el7 作为依赖被升级: bind-libs.x86_64 32:9.11.4-9.P2.el7 bind-libs-lite.x86_64 32:9.11.4-9.P2.el7 bind-license.noarch 32:9.11.4-9.P2.el7 bind-utils.x86_64 32:9.11.4-9.P2.el7 dhclient.x86_64 12:4.2.5-77.el7.centos dhcp-common.x86_64 12:4.2.5-77.el7.centos dhcp-libs.x86_64 12:4.2.5-77.el7.centos 完毕![root@localhost ~]# 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; };...//省略部分内容...:wq[root@localhost ~]# vim /etc/named.rfc1912.zones...//省略部分内容...zone "kgc.com" IN { type master; file "kgc.com.zone"; allow-update { none; };};...//省略部分内容...:wq[root@localhost ~]# cd /var/named/[root@localhost named]# lsdata dynamic named.ca named.empty named.localhost named.loopback slaves[root@localhost named]# cp -p named.localhost kgc.com.zone[root@localhost named]# vim kgc.com.zone$TTL 1D@ IN SOA @ rname.invalid. ( 0 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum NS @ A 127.0.0.1www IN A 192.168.144.133[root@localhost named]# systemctl start named //启动DNS服务
- 这里我们先不开启盗链功能,现在客户机中访问网页图片,然后在开启一台win 7客户机,安装http服务,盗链图片,看是否可以盗链
- 在Linux系统中配置防盗链模块
[root@localhost ~]# vim /usr/local/httpd/conf/httpd.conf //编辑主配置文件...//省略部分内容...#LoadModule userdir_module modules/mod_userdir.soLoadModule alias_module modules/mod_alias.soLoadModule rewrite_module modules/mod_rewrite.so //找到此条,并开启此条目## If you wish httpd to run as a different user or group, you must run...//省略部分内容... //在此标签下添加防盗链条目 # # Possible values for the Options directive are "None", "All", # or any combination of: # Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews # # Note that "MultiViews" must be named *explicitly* --- "Options All" # doesn't give it to you. # # The Options directive is both complicated and important. Please see # http://httpd.apache.org/docs/2.4/mod/core.html#options # for more information. # Options Indexes FollowSymLinks # # AllowOverride controls what directives may be placed in .htaccess files. # It can be "All", "None", or any combination of the keywords: # AllowOverride FileInfo AuthConfig Limit # AllowOverride None # # Controls who can get stuff from this server. # Require all granted RewriteEngine On //开启防盗链功能 RewriteCond %{HTTP_REFERER} !^http://kgc.com/.*$ [NC] RewriteCond %{HTTP_REFERER} !^http://kgc.com$ [NC] RewriteCond %{HTTP_REFERER} !^http://www.kgc.com/.*$ [NC] RewriteCond %{HTTP_REFERER} !^http:// www.kgc.com/$ [NC] RewriteRule .*\.(gif|jpg|swf)$ http://www.kgc.com/error.png ...//省略部分内容... :wq [root@localhost ~]# cd /mnt //进入挂载目录[root@localhost mnt]# ls //查看是否有准备好的防盗链图片apr-1.6.2.tar.gz cronolog-1.6.2-14.el7.x86_64.rpm httpd-2.4.29.tar.bz2 mysql-5.6.26.tar.gzapr-util-1.6.0.tar.gz Discuz_X2.5_SC_UTF8.zip LAMP-php5.6.txt nginx-1.12.0.tar.gzawstats-7.6.tar.gz error.png miao.jpg php-5.6.11.tar.bz2[root@localhost mnt]# cp error.png /usr/local/httpd/htdocs/ //将防盗链图片复制入http站点目录[root@localhost mnt]# cd /usr/local/httpd/htdocs/ //进入站点目录[root@localhost htdocs]# ls //查看error.png index.html miao.jpg //成功复制[root@localhost htdocs]# systemctl start httpd //重启网络服务
- 再次在客户机中访问网页,看防盗链功能是否开启
隐藏版本信息
Apache
隐藏版本信息
Apache
的版本信息,透露了一定的漏洞信息,从而给网站带来安全隐患- 生产环境中要配置
Apache
隐藏版本信息 - 可使用
Fiddler
抓包工具分析
配置Apache
隐藏版本信息
将主配置文件
httpd.conf
以下行注释去掉- #
Include conf/extra/httpd-default.conf
- #
- 修改
httpd-default.conf
文件两个地方 ServerTokens Full
修改为Server Tokens Prod
- 将
ServersSignature On
修改为ServersSignature Off
重启httpd服务,访问网站,抓包测试
配置实例
- 现在客户机中使用抓包工具,看看是否会显示版本信息
- 进入HTTP配置文件,更改信息
[root@localhost htdocs]# vim /usr/local/httpd/conf/httpd.conf ...//省略部分内容...#Include conf/extra/httpd-dav.conf# Various default settingsInclude conf/extra/httpd-default.conf //找到此条目,并去掉注释# Configure mod_proxy_html to understand HTML4/XHTML1Include conf/extra/proxy-html.conf ...//省略部分内容...:wq //保存退出[root@localhost htdocs]# cd /usr/local/httpd/conf/extra/ //进入目录[root@localhost extra]# ls httpd-autoindex.conf httpd-info.conf httpd-mpm.conf httpd-userdir.confhttpd-dav.conf httpd-languages.conf httpd-multilang-errordoc.conf httpd-vhosts.confhttpd-default.conf httpd-manual.conf httpd-ssl.conf proxy-html.conf[root@localhost extra]# vim httpd-default.conf //编辑配置文件...//省略部分内容...# Set to one of: Full | OS | Minor | Minimal | Major | Prod# where Full conveys the most information, and Prod the least.#ServerTokens Prod //找到此条目,并更改Full为Prod## Optionally add a line containing the server version and virtual host# name to server-generated pages (internal error documents, FTP directory # listings, mod_status and mod_info output etc., but not CGI generated # documents or custom error documents).# Set to "EMail" to also include a mailto: link to the ServerAdmin.# Set to one of: On | Off | EMail# ServerSignature Off //并确定此处是否为关闭,默认为关闭## HostnameLookups: Log the names of clients or just their IP addresses# e.g., www.apache.org (on) or 204.62.129.132 (off).:wq //保存退出[root@localhost extra]# systemctl restart httpd.service //重启服务
- 再次在客户机中测试抓包,看是否还显示版本信息
配置
模块
服务
内容
部分
文件
网页
信息
防盗
功能
客户
版本
缓存
网站
规则
客户机
编译
图片
测试
工具
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
软件开发团队网银
软件开发中期规划
CNKI什么数据库
荷兰网络安全工业
嘉昆太网络安全协定
锐捷云桌面服务器接线方式
学校多举措筑牢网络安全坚实防线
校园网络安全防范措施doc
asp架设服务器
pchmi工业组态软件开发
node 数据库怎么开启
网络安全方针政策支持
sql怎样创建学生数据库表
服务器安全软件和堡垒机
网络安全法的责任主体是
小学网络安全简讯
php对数据库进行操作
铜仁互联网科技公司
php变量链接数据库
大学毕业了又想学软件开发
steam 怎么选服务器
数据库窗体设计label13
掌中通服务器连接异常
ntp时间服务器安装
软件开发确认报告
网络安全攻防工程招聘
上饶个人服务器哪家比较好
革命历史红色档案资料数据库
夕采网络技术
国产数据库行情