<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh-Hans-CN">
	<id>https://wiki.linuxsa.org/index.php?action=history&amp;feed=atom&amp;title=Nginx_Lua_Redis%E9%98%B2%E6%AD%A2CC%E6%94%BB%E5%87%BB</id>
	<title>Nginx Lua Redis防止CC攻击 - 版本历史</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.linuxsa.org/index.php?action=history&amp;feed=atom&amp;title=Nginx_Lua_Redis%E9%98%B2%E6%AD%A2CC%E6%94%BB%E5%87%BB"/>
	<link rel="alternate" type="text/html" href="https://wiki.linuxsa.org/index.php?title=Nginx_Lua_Redis%E9%98%B2%E6%AD%A2CC%E6%94%BB%E5%87%BB&amp;action=history"/>
	<updated>2026-04-23T09:30:18Z</updated>
	<subtitle>本wiki上该页面的版本历史</subtitle>
	<generator>MediaWiki 1.43.1</generator>
	<entry>
		<id>https://wiki.linuxsa.org/index.php?title=Nginx_Lua_Redis%E9%98%B2%E6%AD%A2CC%E6%94%BB%E5%87%BB&amp;diff=733&amp;oldid=prev</id>
		<title>Evan：​导入1个版本</title>
		<link rel="alternate" type="text/html" href="https://wiki.linuxsa.org/index.php?title=Nginx_Lua_Redis%E9%98%B2%E6%AD%A2CC%E6%94%BB%E5%87%BB&amp;diff=733&amp;oldid=prev"/>
		<updated>2019-10-14T13:52:28Z</updated>

		<summary type="html">&lt;p&gt;导入1个版本&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新页面&lt;/b&gt;&lt;/p&gt;&lt;div&gt;=原理=&lt;br /&gt;
Nginx Lua Redis防止CC攻击实现原理：同一个外网IP、同一个网址(ngx.var.request_uri)、同一个客户端(http_user_agent)在某一段时间(CCseconds)内访问某个网址(ngx.var.request_uri)超过指定次数(CCcount)，则禁止这个外网IP+同一个客户端(md5(IP+ngx.var.http_user_agent)访问这个网址(ngx.var.request_uri)一段时间(blackseconds)。&lt;br /&gt;
&lt;br /&gt;
该脚本使用lua编写(依赖nginx+lua)，将信息写到redis(依赖redis.lua)。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Nginx lua模块安装=&lt;br /&gt;
重新编译nginx，安装lua模块，或者直接使用《OneinStack》安装OpenResty自带改模块&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pushd /root/oneinstack/src&lt;br /&gt;
wget -c http://nginx.org/download/nginx-1.12.1.tar.gz&lt;br /&gt;
wget -c http://mirrors.linuxeye.com/oneinstack/src/openssl-1.0.2l.tar.gz&lt;br /&gt;
wget -c http://mirrors.linuxeye.com/oneinstack/src/pcre-8.41.tar.gz&lt;br /&gt;
wget -c http://luajit.org/download/LuaJIT-2.0.5.tar.gz&lt;br /&gt;
git clone https://github.com/simpl/ngx_devel_kit.git&lt;br /&gt;
git clone https://github.com/openresty/lua-nginx-module.git&lt;br /&gt;
tar xzf nginx-1.12.1.tar.gz&lt;br /&gt;
tar xzf openssl-1.0.2l.tar.gz&lt;br /&gt;
tar xzf pcre-8.41.tar.gz&lt;br /&gt;
tar xzf LuaJIT-2.0.5.tar.gz&lt;br /&gt;
pushd LuaJIT-2.0.5&lt;br /&gt;
make &amp;amp;&amp;amp; make install&lt;br /&gt;
popd&lt;br /&gt;
pushd nginx-1.12.1&lt;br /&gt;
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-openssl=../openssl-1.0.2l --with-pcre=../pcre-8.41 --with-pcre-jit --with-ld-opt=-ljemalloc --add-module=../lua-nginx-module --add-module=../ngx_devel_kit&lt;br /&gt;
make&lt;br /&gt;
mv /usr/local/nginx/sbin/nginx{,_bk}&lt;br /&gt;
cp objs/nginx /usr/local/nginx/sbin&lt;br /&gt;
nginx -t #检查语法&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
加载redis.lua&lt;br /&gt;
&amp;lt;pre&amp;gt;mkdir /usr/local/nginx/conf/lua&lt;br /&gt;
cd /usr/local/nginx/conf/lua&lt;br /&gt;
wget https://github.com/openresty/lua-resty-redis/raw/master/lib/resty/redis.lua&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
在/usr/local/nginx/conf/nginx.conf http { }中添加：&lt;br /&gt;
&lt;br /&gt;
 the Nginx bundle:&lt;br /&gt;
 lua_package_path &amp;quot;/usr/local/nginx/conf/lua/redis.lua;;&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
防止CC规则waf.lua&lt;br /&gt;
&amp;lt;pre&amp;gt;#将下面内容保存在/usr/local/nginx/conf/lua/waf.lua&lt;br /&gt;
&lt;br /&gt;
local get_headers = ngx.req.get_headers&lt;br /&gt;
local ua = ngx.var.http_user_agent&lt;br /&gt;
local uri = ngx.var.request_uri&lt;br /&gt;
local url = ngx.var.host .. uri&lt;br /&gt;
local redis = require &amp;#039;redis&amp;#039;&lt;br /&gt;
local red = redis.new()&lt;br /&gt;
local CCcount = 20&lt;br /&gt;
local CCseconds = 60&lt;br /&gt;
local RedisIP = &amp;#039;127.0.0.1&amp;#039;&lt;br /&gt;
local RedisPORT = 6379&lt;br /&gt;
local blackseconds = 7200&lt;br /&gt;
if ua == nil then&lt;br /&gt;
    ua = &amp;quot;unknown&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if (uri == &amp;quot;/wp-admin.php&amp;quot;) then&lt;br /&gt;
    CCcount=20&lt;br /&gt;
    CCseconds=60&lt;br /&gt;
end&lt;br /&gt;
red:set_timeout(100)&lt;br /&gt;
local ok, err = red.connect(red, RedisIP, RedisPORT)&lt;br /&gt;
if ok then&lt;br /&gt;
    red.connect(red, RedisIP, RedisPORT)&lt;br /&gt;
    function getClientIp()&lt;br /&gt;
        IP = ngx.req.get_headers()[&amp;quot;X-Real-IP&amp;quot;]&lt;br /&gt;
        if IP == nil then&lt;br /&gt;
            IP = ngx.req.get_headers()[&amp;quot;x_forwarded_for&amp;quot;]&lt;br /&gt;
        end&lt;br /&gt;
        if IP == nil then&lt;br /&gt;
            IP  = ngx.var.remote_addr&lt;br /&gt;
        end&lt;br /&gt;
        if IP == nil then&lt;br /&gt;
            IP  = &amp;quot;unknown&amp;quot;&lt;br /&gt;
        end&lt;br /&gt;
        return IP&lt;br /&gt;
    end&lt;br /&gt;
    local token = getClientIp() .. &amp;quot;.&amp;quot; .. ngx.md5(url .. ua)&lt;br /&gt;
    local req = red:exists(token)&lt;br /&gt;
    if req == 0 then&lt;br /&gt;
        red:incr(token)&lt;br /&gt;
        red:expire(token,CCseconds)&lt;br /&gt;
    else&lt;br /&gt;
        local times = tonumber(red:get(token))&lt;br /&gt;
        if times &amp;gt;= CCcount then&lt;br /&gt;
            local blackReq = red:exists(&amp;quot;black.&amp;quot; .. token)&lt;br /&gt;
            if (blackReq == 0) then&lt;br /&gt;
                red:set(&amp;quot;black.&amp;quot; .. token,1)&lt;br /&gt;
                red:expire(&amp;quot;black.&amp;quot; .. token,blackseconds)&lt;br /&gt;
                red:expire(token,blackseconds)&lt;br /&gt;
                ngx.exit(580)&lt;br /&gt;
            else&lt;br /&gt;
                ngx.exit(580)&lt;br /&gt;
            end&lt;br /&gt;
            return&lt;br /&gt;
        else&lt;br /&gt;
            red:incr(token)&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    return&lt;br /&gt;
end&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Nginx虚拟主机加载waf.lua&lt;br /&gt;
在虚拟主机配置文件/usr/local/nginx/conf/vhost/oneinstack.com.conf&lt;br /&gt;
&lt;br /&gt;
 access_by_lua_file &amp;quot;/usr/local/nginx/conf/lua/waf.lua&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
=测试=&lt;br /&gt;
&lt;br /&gt;
=see also=&lt;br /&gt;
[https://blog.linuxeye.cn/453.html Nginx Lua Redis防止CC攻击]&lt;br /&gt;
&lt;br /&gt;
 [[category:ops]]  [[category:nginx]]&lt;/div&gt;</summary>
		<author><name>Evan</name></author>
	</entry>
</feed>