mirror of
https://github.com/Ysurac/openmptcprouter-feeds.git
synced 2025-03-09 15:40:03 +00:00
Add basic IPv6 support and replace haproxy by nginx for VPS failover
This commit is contained in:
parent
2f4e19176c
commit
715d53300d
23 changed files with 1419 additions and 9 deletions
8
luci-app-nginx-ha/root/etc/config/nginx-ha
Normal file
8
luci-app-nginx-ha/root/etc/config/nginx-ha
Normal file
|
@ -0,0 +1,8 @@
|
|||
|
||||
config general 'general'
|
||||
option enable '0'
|
||||
option retries '1'
|
||||
option timeout '1000'
|
||||
option listen '0.0.0.0:65101'
|
||||
option startup_delay '5'
|
||||
list upstreams '1.2.3.4:65101 weight=1 max_fails=3 fail_timeout=30s'
|
115
luci-app-nginx-ha/root/etc/init.d/nginx-ha
Executable file
115
luci-app-nginx-ha/root/etc/init.d/nginx-ha
Executable file
|
@ -0,0 +1,115 @@
|
|||
#!/bin/sh /etc/rc.common
|
||||
# Copyright (C) 2016 chenhw2 <chenhw2@github.com>
|
||||
# Copyright (C) 2018 Ycarus (Yannick Chabanois) <ycarus@zugaina.org>
|
||||
|
||||
START=85
|
||||
|
||||
USE_PROCD=1
|
||||
PROG_NAME=nginx
|
||||
PROG=/usr/sbin/${PROG_NAME}
|
||||
NAME=nginx-ha
|
||||
|
||||
PIDCOUNT=0
|
||||
|
||||
_log() {
|
||||
logger -p daemon.info -t ${PROG_NAME} "$@"
|
||||
}
|
||||
|
||||
_err() {
|
||||
logger -p daemon.err -t ${PROG_NAME} "$@"
|
||||
}
|
||||
|
||||
validate_section() {
|
||||
uci_validate_section nginx-ha general "${1}" \
|
||||
'enable:bool:0' \
|
||||
'retries:uinteger:3' \
|
||||
'timeout:uinteger:4000' \
|
||||
'startup_delay:uinteger:5' \
|
||||
'listen:string' \
|
||||
'upstreams:list(string)'
|
||||
}
|
||||
|
||||
genline_srv(){
|
||||
echo " server $1;"
|
||||
}
|
||||
|
||||
boot() {
|
||||
local delay=$(uci -q get $NAME.general.startup_delay)
|
||||
(sleep ${delay:-0} && start >/dev/null 2>&1) &
|
||||
return 0
|
||||
}
|
||||
|
||||
start_instance() {
|
||||
local enable retries timeout startup_delay listen upstreams
|
||||
|
||||
validate_section "${1}" || {
|
||||
_err "validation failed"
|
||||
return 1
|
||||
}
|
||||
|
||||
[ "$enable" = 1 ] || return 1
|
||||
|
||||
mkdir -p /var/etc
|
||||
cat <<-EOF > /var/etc/$PROG_NAME.cfg
|
||||
user nobody nogroup;
|
||||
worker_processes $(grep -c '^processor' /proc/cpuinfo | tr -d "\n");
|
||||
worker_rlimit_nofile 300000;
|
||||
|
||||
events {
|
||||
worker_connections 15000;
|
||||
multi_accept on;
|
||||
use epoll;
|
||||
}
|
||||
|
||||
stream {
|
||||
upstream allservers {
|
||||
zone dynamic 64k;
|
||||
$(config_list_foreach "${1}" "upstreams" genline_srv)
|
||||
}
|
||||
|
||||
server {
|
||||
listen ${listen:-0.0.0.0:6666} udp;
|
||||
proxy_pass allservers;
|
||||
}
|
||||
server {
|
||||
listen ${listen:-0.0.0.0:6666};
|
||||
proxy_pass allservers;
|
||||
}
|
||||
}
|
||||
EOF
|
||||
|
||||
procd_open_instance "nginx-ha"
|
||||
procd_set_param command /usr/sbin/nginx -c /var/etc/$PROG_NAME.cfg -g 'daemon off;'
|
||||
procd_set_param file /var/etc/$PROG_NAME.cfg
|
||||
procd_set_param respawn
|
||||
procd_close_instance
|
||||
}
|
||||
|
||||
start_service() {
|
||||
config_load nginx-ha
|
||||
config_foreach start_instance general
|
||||
}
|
||||
|
||||
reload_service() {
|
||||
stop
|
||||
start
|
||||
}
|
||||
|
||||
stop_service() {
|
||||
local _PID=$(cat /var/run/nginx.pid 2>/dev/null)
|
||||
kill -15 $_PID 2>/dev/null
|
||||
sleep 1 # give time to shutdown
|
||||
local _tmp=$(pgrep nginx | tr "\n" " ")
|
||||
if [ -z "$_tmp" ]; then
|
||||
logger -p daemon.notice -t "nginx-ha[$_PID]" "Shutdown successfully"
|
||||
else
|
||||
kill -9 $_tmp # Normally never come here
|
||||
logger -p daemon.warn -t "nginx-ha[$_tmp]" "Shutdown forced by KILL"
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
|
||||
service_triggers() {
|
||||
procd_add_reload_trigger nginx-ha
|
||||
}
|
13
luci-app-nginx-ha/root/etc/uci-defaults/42_luci-nginx-ha
Normal file
13
luci-app-nginx-ha/root/etc/uci-defaults/42_luci-nginx-ha
Normal file
|
@ -0,0 +1,13 @@
|
|||
#!/bin/sh
|
||||
|
||||
uci -q batch <<-EOF >/dev/null
|
||||
delete ucitrack.@nginx-ha[-1]
|
||||
add ucitrack nginx-ha
|
||||
set ucitrack.@nginx-ha[-1].init=nginx-ha
|
||||
commit ucitrack
|
||||
EOF
|
||||
|
||||
/etc/init.d/nginx-ha enable >/dev/null 2>&1
|
||||
|
||||
rm -f /tmp/luci-indexcache
|
||||
exit 0
|
Loading…
Add table
Add a link
Reference in a new issue