diff --git a/Dockerfile b/Dockerfile index beff63c..caa5c96 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,7 @@ -# FROM openresty/openresty:centos # this will pull lastest version +# this will pull lastest version # https://hub.docker.com/r/openresty/openresty/tags?page=1&name=centos -FROM openresty/openresty:1.21.4.1-centos +# FROM openresty/openresty:centos +FROM openresty/openresty:1.21.4.1-4-centos7 # not ENV TZ "Asia/Shanghai" ENV TZ Asia/Shanghai diff --git a/bin/ngxway.sh b/bin/ngxway.sh index 02ae048..ab03adb 100644 --- a/bin/ngxway.sh +++ b/bin/ngxway.sh @@ -1,3 +1,16 @@ cp /dist/vhosts/* /etc/nginx/conf.d/ cp /dist/conf/* /usr/local/openresty/nginx/conf/ nginx -s reload + +# Write start info to ngxway.start.log and fix this bug : https://github.com/WGrape/ngxway/issues/4 +time=$(date "+%Y-%m-%d %H:%M:%S") +if [ $? -ne 0 ]; then + log="[${time}] >>>>>>>>failed to start ngxway<<<<<<<<" + echo -e $log + echo $log >> /dist/logs/ngxway.start.log + exit 1 +else + log="[${time}] start ngxway success" + echo -e $log + echo $log >> /dist/logs/ngxway.start.log +fi diff --git a/conf/nginx.conf b/conf/nginx.conf index b44d52d..5d96dec 100644 --- a/conf/nginx.conf +++ b/conf/nginx.conf @@ -40,5 +40,12 @@ http { sendfile on; keepalive_timeout 65; + # Declare a shared memory region + lua_shared_dict my_limit_req_store 30m; + + # here is lua lib path and search lua keyword : https://hub.docker.com/layers/openresty/openresty/1.21.4.1-centos/images/sha256-43b77448f355d7266a13e6a8f2e495de7b29b7d7e8b872e420d835f04120fb44?context=explore + # https://github.com/openresty/lua-nginx-module#lua_package_path + lua_package_path "/usr/local/openresty/site/lualib/?.ljbc;/usr/local/openresty/site/lualib/?/init.ljbc;/usr/local/openresty/lualib/?.ljbc;/usr/local/openresty/lualib/?/init.ljbc;/usr/local/openresty/site/lualib/?.lua;/usr/local/openresty/site/lualib/?/init.lua;/usr/local/openresty/lualib/?.lua;/usr/local/openresty/lualib/?/init.lua;./?.lua;/usr/local/openresty/luajit/share/luajit-2.1.0-beta3/?.lua;/usr/local/share/lua/5.1/?.lua;/usr/local/share/lua/5.1/?/init.lua;/usr/local/openresty/luajit/share/lua/5.1/?.lua;/usr/local/openresty/luajit/share/lua/5.1/?/init.lua;"; + include /etc/nginx/conf.d/*.conf; } diff --git a/lua/check_sign.lua b/lua/check_sign.lua index 88f4aa3..073d009 100644 --- a/lua/check_sign.lua +++ b/lua/check_sign.lua @@ -47,7 +47,7 @@ function checkTimestamp() -- 如果时间戳相差在1小时内,则视为正常请求,否则视为恶意请求 now = os.time() - if math.abs(now - timestamp) > 3600 then + if math.abs(now - tonumber(timestamp)) > 3600 then return false end diff --git a/lua/ip_check.lua b/lua/ip_check.lua deleted file mode 100644 index e69de29..0000000 diff --git a/lua/limit_traffic.lua b/lua/limit_traffic.lua index 9531efb..94a6c27 100644 --- a/lua/limit_traffic.lua +++ b/lua/limit_traffic.lua @@ -1,4 +1,36 @@ --- https://blog.csdn.net/carlos__z/article/details/112208972 --- https://blog.csdn.net/knight_zhou/article/details/122495234 --- https://blog.openresty.com.cn/cn/edge-cache-rules/ +-- https://github.com/openresty/lua-resty-limit-traffic +local limit_req = require "resty.limit.req" +-- limit the requests under 200 req/sec with a burst of 100 req/sec, +-- that is, we delay requests under 300 req/sec and above 200 +-- req/sec, and reject any requests exceeding 300 req/sec. +local lim, err = limit_req.new("my_limit_req_store", 200, 100) +if not lim then + ngx.log(ngx.ERR, "failed to instantiate a resty.limit.req object: ", err) + return ngx.exit(500) +end + +-- the following call must be per-request. +-- here we use the remote (IP) address as the limiting key +local key = ngx.var.binary_remote_addr +local delay, err = lim:incoming(key, true) +if not delay then + if err == "rejected" then + return ngx.exit(503) + end + ngx.log(ngx.ERR, "failed to limit req: ", err) + return ngx.exit(500) +end + +if delay >= 0.001 then + -- the 2nd return value holds the number of excess requests + -- per second for the specified key. for example, number 31 + -- means the current request rate is at 231 req/sec for the + -- specified key. + local excess = err + + -- the request exceeding the 200 req/sec but below 300 req/sec, + -- so we intentionally delay it here a bit to conform to the + -- 200 req/sec rate. + ngx.sleep(delay) +end diff --git a/start.sh b/start.sh index d260d2b..4710a77 100644 --- a/start.sh +++ b/start.sh @@ -27,6 +27,9 @@ if [ $? -ne 0 ]; then exit 1 fi +# Mapping to local disk storage +mkdir -p /tmp/logs/ && chmod -R 777 /tmp/logs/ + # Run container docker run --name ngxwayContainer -d -p 127.0.0.1:8090:8090 -v /tmp/logs:/dist/logs/ ngxway if [ $? -ne 0 ]; then diff --git a/stop.sh b/stop.sh index 7c44d3a..de2a75c 100644 --- a/stop.sh +++ b/stop.sh @@ -7,6 +7,9 @@ docker container rm ngxwayContainer # Remove image docker rmi ngxway +# Remove local volume data +rm -rf /tmp/logs + if [ $? -ne 0 ]; then echo -e ">>>>>>>>Stop failure<<<<<<<<" exit 1 diff --git a/vhosts/ngxway.conf b/vhosts/ngxway.conf index 37ecc08..d0d7a79 100644 --- a/vhosts/ngxway.conf +++ b/vhosts/ngxway.conf @@ -14,9 +14,10 @@ server { set $date $1$2$3$4; } access_log /dist/logs/ngxway.access.$date.log; - error_log /dist/logs/ngxway.error.$date.log; + error_log /dist/logs/ngxway.error.log error; # error_log not support variable, such as /dist/logs/ngxway.error.$date.log access_by_lua_file /dist/lua/check_sign.lua; + # access_by_lua_file /dist/lua/limit_traffic.lua; location / { root /dist/html/;