han8809 发表于 2018-11-22 10:02:25

Apache 实现http协议自动转成https协议,Apache 防DDOS*** 使用mod_rpaf模块 mod_evasive模块

  一:实践环境介绍
  二:配置Apache,实现访问http页面自动转成https页面
  需求1:整个站点都实现http自动转https
  需求2:整个站点使用http协议,只有某个单独页面从http自动转到https
  


[*]  实验环境介绍

使用yum 安装apache
Apache版本
# httpd -v
Server version: Apache/2.2.15 (Unix)
Server built:   Aug 13 2013 17:29:28
使用yum 安装openssl
# yum install openssl
openssl version
OpenSSL 1.0.1e-fips 11 Feb 2013


2. 配置Apache,实现访问http页面自动转成https页面
Apache官方rewrite使用文档:
http://man.chinaunix.net/newsoft/ApacheMenual_CN_2.2new/rewrite/rewrite_guide.html


需求1:整个站点都实现http自动转https
1.       配置apache支持ssl协议
# vi /etc/httpd/conf.d/ssl.conf
LoadModule ssl_module modules/mod_ssl.so
Listen 443
……

DocumentRoot "/var/www/html/"
ServerName www.test.com:443    #不是必须配置的
SSLEngine on      #为虚拟机开启ssl协议
SSLCertificateFile /etc/pki/tls/certs/localhost.crt         #加载ssl证书文件
SSLCertificateKeyFile /etc/pki/tls/private/localhost.key      #加载ssl私钥文件

2.       修改# vi /etc/httpd/conf/httpd.conf文件,将80端口自动转到443端口
Listen 80
Include conf.d/*.conf   #加载conf.d目录下的配置文件
ServerName www.test.com:80      #这里是否配置不影响http转https,只是避免重启时提示
……
# add by sxr 这里是实现转发的关键,整个站点都转就把这一段写在httpd.conf中
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
#RewriteCond %{SERVER_PORT} !^443$            #这样写也有效只是匹配方式不同而已
#RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1       #这样的正则也有效与下面相同
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI}
RewriteLog /var/log/httpd/rewrite.log          #增加记录转发的日志,可以不设置
RewriteLogLevel 10
保存,重启apache生效

访问浏览器测试,输入192.168.123.75回车以后自动转成https://192.168.123.75
查看rewrite.log日志,红色的表示rewrite的动作
# tail -f /var/log/httpd/rewrite.log
172.16.10.48 - - (2) init rewrite engine with requested uri /
172.16.10.48 - - (3) applying pattern '^.*$' to uri '/'
172.16.10.48 - - (4) RewriteCond: input='80' pattern='80' => matched
172.16.10.48 - - (2) rewrite '/' -> 'https://192.168.123.75/'
172.16.10.48 - - (2) explicitly forcing redirect with https://192.168.123.75/
172.16.10.48 - - (1) escaping https://192.168.123.75/ for redirect
172.16.10.48 - - (1) redirect to https://192.168.123.75/




需求2:整个站点使用http协议,只有某个单独页面从http自动转到https
修改# vi /etc/httpd/conf/httpd.conf文件,在文件末尾添加
# add by sxr
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule /login.html https://%{SERVER_NAME}%{REQUEST_URI}
RewriteLog /var/log/httpd/rewrite.log
RewriteLogLevel 10
保存
注意:如果这里配置了虚拟主机的话,要把Rewrte部分写进虚拟主机里才能有效果。
测试看看
首先,我们访问ip地址192.168.123.75,打开默认页面,不会转到https,如下图

访问192.168.123.75/login.html,立马就转到https协议了
访问域名的效果与访问ip一样。




Apache 防DDOS*** 使用mod_rpaf模块 mod_evasive模块
  1使用mod_rpaf模块解决代理后端的Apache获取到互联网真实的IP(默认Apache获取的是代理服务器的IP)
  1.1mod_rpaf介绍
  1.2mod_rpaf安装
  2使用mod_evasive模块自动阻止DDOS***
  2.1mod_evasive介绍:
  2.2mod_evasive安装
  2.3mod_evasive测试
1使用mod_rpaf模块解决代理后端的Apache获取到互联网真实的IP(默认Apache获取的是代理服务器的IP)
  
1.1mod_rpaf介绍
mod_rpaf模块可以让躲在代理服务器后面的Apache服务器获取到代理外面的互联网真实访问IP地址(默认Apache获得的是来自代理服务器的IP),然后再利用mod_evasive模块自动判断连接频率,把可疑ip阻挡掉,或配合ipables将判断的ip地址添加到黑名单。
官网下载:http://www.stderr.net/apache/rpaf/
1.2mod_rpaf安装
当前最新版本是:mod_rpaf-0.6.tar.gz
1.       安装模块
wget http://stderr.net/apache/rpaf/download/mod_rpaf-0.6.tar.gz
tar xvfz mod_rpaf-0.6.tar.gz
cd mod_rpaf-0.6
/usr/local/apache/bin/apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c
2.       配置apache
LoadModule rpaf_module modules/mod_rpaf-2.0.so
#Include conf/extra/mod_rpaf.conf

RPAFenable On
RPAFproxy_ips 183.136.133.* 183.60.211.* 220.181.55.* 101.226.4.* 180.153.235.* 122.143.15.* 27.221.20.* 202.102.85.* 61.160.224.* 112.25.60.* 182.140.227.*
RPAFsethostname On
RPAFheader X-Forwarded-For
注意:RPAFproxy_ips 指填写代理服务器的ip地址,不是Apache服务自身的地址。网段可以写成类似183.136.133.*这种格式。与mod_evasive模块的网段写法类似。
# 修改apache的日志输出格式,如下
LogFormat "\"%{X-Forwarded-For}i\" %h %l %u %t \"%r\" %>s %b %{deviceId}i \"%{Referer}i\" \"%{User-Agent}i\" " combined
3.       重启apache使之生效


2使用mod_evasive模块自动阻止DDOS***
  
2.1mod_evasive介绍:
mod_evasive 是Apache(httpd)服务器防DDOS***的一个模块。对于WEB服务器来说,是目前比较好的一个防护DDOS***的扩展模块。虽然并不能完全防御 DDOS***,但在一定条件下,还是起到缓服Apache(httpd)服务器的***压力,可以配合iptables命令实现自动判断***ip,自动添加到iptables阻止列表中。实践证明效果明显有效。
官方站点:http://www.zdziarski.com
2.2mod_evasive安装
阅读README文件,里面有安装和配置的方法,支持Apache 1.3 and 2.0
1)安装模块
$APACHE_ROOT/bin/apxs -i -a -c mod_evasive20.c
安装完成后,modules/mod_evasive20.so目录下将增加一个mod_evasive20.so的文件。
注:apxs 命令用于编译模块工具;如果安装的apache不是默认的rpm包,apxs命令位于您安装的apache安装路径bin目录下。
2)配置httpd.conf文件
LoadModule evasive20_module modules/mod_evasive20.so

    DOSHashTableSize    5120
    DOSPageCount      3
    DOSSiteCount      50
    DOSPageInterval   1
    DOSSiteInterval   1
    DOSBlockingPeriod   10
    DOSLogDir "/tmp"
#   DOSSystemCommand    "su - someuser -c '/sbin/... %s ...'"
DOSSystemCommand    "sudo /sbin/iptables -A INPUT -s %s -p tcp --dport 80 -j DROP"
DOSEmailNotify shenxiaoran@7500.com.cn
    DOSWhiteList 127.0.0.1
    DOSWhiteList 192.168.123.*
说明:这里使用sudo 命令执行iptables实现自动把识别的***ip添加到防火墙内策略。
3)重启apache使模块生效
参数说明:

DOSHashTableSize 3097
#记录和存放黑名单表大小,如果服务器访问量很大,可以加大该值,增加这个值会消耗更多的内存空间
DOSPageCount 5
#同一个页面在同一时间内可以被同一个用户访问的次数,超过该数字就会被列为***,同一时间的数值可以在DosPageInterval参数中设置.
DOSSiteCount 100
#同一个用户在同一个网站内可以同时打开的访问数,同一个时间的数值在DOSSiteInterval中设置。
DOSPageInterval 1
#设置DOSPageCount中时间长度标准,默认值为1。
DOSSiteInterval 1
设置DOSSiteCount中时间长度标准,默认值为1。
DOSBlockingPeriod 10
#被封时间间隔秒,这中间会收到 403 (Forbidden) 的返回,默认10秒。
# 下面3项可选添加,主要用来邮件提醒;自定义shell命令;记录日志。
DOSEmailNotify victorman45@sohu.com
#设置受到***时接收***信息提示的邮箱地址。
DOSSystemCommand    "su - root -c '/opt/foundir/csmail/mta/bin/sendmail %s < /tmp/ddos_warnning.eml'"
#用来调用自定义shell脚本来执行发信报警或其它动作。%s是引用的地址变量
DOSLogDir "/var/log/mod_evasive"
#***日志存放目录,注意这个目录的权限,是运行apache程序的用户。
DOSWhitelist    127.0.0.*
DOSWhitelist    10.1.6.51
#添加白名单,加多个就添加多行,写一行不生效。
2.3mod_evasive测试
1. 查看 /tmp目录下,发现有dos-xxx.xxx.xxx.xxx文件说明模块生效了。
-rw-r--r-- 1 daemondaemon      6 Oct 24 11:55 dos-110.167.181.201
-rw-r--r-- 1 daemondaemon      6 Oct 24 13:51 dos-110.176.79.135
-rw-r--r-- 1 daemondaemon      6 Oct 24 13:31 dos-110.211.157.21
-rw-r--r-- 1 daemondaemon      6 Oct 24 12:12 dos-110.80.111.183
-rw-r--r-- 1 daemondaemon      6 Oct 24 13:26 dos-110.89.10.25
-rw-r--r-- 1 daemondaemon      6 Oct 24 13:27 dos-110.90.41.56
-rw-r--r-- 1 daemondaemon      6 Oct 24 14:32 dos-111.174.167.183
-rw-r--r-- 1 daemondaemon      6 Oct 24 11:52 dos-111.177.13.104
-rw-r--r-- 1 daemondaemon      6 Oct 24 11:36 dos-112.224.21.14
同时查看iptables列表,观察是否成功添加了策略。
# iptables –L
2. 在mod_evasive_1.10.1.tar.gz解压目录下面有个测试脚本,test.pl是用perl写的,如果你看到下面的截图,就表示该模块安装成功。


如果你使用了DOSLogDir "/var/log/mod_evasive"参数,该目录的属主要是nobody:nobody,发生***时,将生成一个dos-127.0.0.1文件。如果没指定日志,默认在/tmp目录下。
还可以直接用浏览器测试:
访问apache,不断刷新,达到一定的阀值就会出现下面提示:
Forbidden

  You don't have permission to access / on this server.





页: [1]
查看完整版本: Apache 实现http协议自动转成https协议,Apache 防DDOS*** 使用mod_rpaf模块 mod_evasive模块