Skip to content

Commit

Permalink
fix: 修复error日志文件名等bug
Browse files Browse the repository at this point in the history
  • Loading branch information
WGrape committed Jan 19, 2023
1 parent e5b9766 commit 6b9ede7
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 7 deletions.
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
13 changes: 13 additions & 0 deletions bin/ngxway.sh
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions conf/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion lua/check_sign.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Empty file removed lua/ip_check.lua
Empty file.
38 changes: 35 additions & 3 deletions lua/limit_traffic.lua
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions stop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion vhosts/ngxway.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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/;
Expand Down

0 comments on commit 6b9ede7

Please sign in to comment.