forked from hestiacp/hestiacp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathv-add-fastcgi-cache
More file actions
executable file
·124 lines (100 loc) · 3.48 KB
/
v-add-fastcgi-cache
File metadata and controls
executable file
·124 lines (100 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/bin/bash
# info: Add FastCGI nginx support
# options: USER DOMAIN [LENTGH] [DEBUG] [RESTART]
# labels: hestia web
#
# example: v-add-fastcgi-cache user domain.tld 30m
#
# The function enables FastCGI cache for nginx
# For Length only use time in seconds (10s) minutes (10m) or days (10d)
# Add "yes" as last parameter to append debug information to response headers
#----------------------------------------------------------#
# Variable&Function #
#----------------------------------------------------------#
# Argument definition
user=$1
domain=$2
length=${3-30m}
debug=${4-no}
restart=${5-no}
# Includes
source $HESTIA/func/main.sh
source $HESTIA/conf/hestia.conf
#----------------------------------------------------------#
# Verifications #
#----------------------------------------------------------#
check_args '2' "$#" 'USER DOMAIN DEBUG'
is_format_valid 'user' 'domain'
is_object_valid 'user' 'USER' "$user"
is_object_unsuspended 'user' 'USER' "$user"
is_object_valid 'web' 'DOMAIN' "$domain"
is_object_unsuspended 'web' 'DOMAIN' "$domain"
if ! [[ "$length" =~ ^[0-9].*[s|m|d]$ ]]; then
echo "Invalid length";
exit 2;
fi
# Perform verification if read-only mode is enabled
check_hestia_demo_mode
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
# Load domain data
parse_object_kv_list $(grep "DOMAIN='$domain'" $USER_DATA/web.conf)
# Check if nginx is not in proxy mode
if [ "$WEB_SYSTEM" != 'nginx' ]; then
echo "Error: nginx is in proxy mode"
exit $E_NOTEXIST
fi
if ! grep --quiet "forcessl" $HESTIA/data/templates/web/nginx/default.tpl; then
$BIN/v-update-web-templates
fi
fastcgi="$HOMEDIR/$user/conf/web/$domain/$WEB_SYSTEM.fastcgi_cache.conf"
no_cache='$no_cache'
cookie_session='$cookie_session'
http_x_update='$http_x_update'
status='$upstream_cache_status'
cat << EOF > $fastcgi
fastcgi_cache $domain;
fastcgi_cache_valid 200 2m;
fastcgi_cache_valid 301 302 10m;
fastcgi_cache_valid 404 10m;
fastcgi_cache_bypass $no_cache;
fastcgi_no_cache $no_cache;
EOF
if [ ! -z "$debug" ]; then
echo " add_header \"X-STATUS\" \"$status\";" >> $fastcgi
fi
chown root:$user $fastcgi
chmod 640 $fastcgi
str="fastcgi_cache_path /var/cache/nginx/micro/$domain levels=1:2"
str="$str keys_zone=$domain:10m max_size=512m inactive=30m;"
conf='/etc/nginx/conf.d/fastcgi_cache_pool.conf'
if [ -f "$conf" ]; then
if [ -z "$(grep "=${domain}:" $conf)" ]; then
echo "$str" >> $conf
fi
else
echo "$str" >> $conf
fi
mkdir -p /var/cache/nginx/micro/$domain
#----------------------------------------------------------#
# Hestia #
#----------------------------------------------------------#
if [ -z "$FASTCGI_CACHE" ]; then
add_object_key "web" 'DOMAIN' "$domain" 'FASTCGI_CACHE' 'ALIAS'
fi
if [ -z "$FASTCGI_LENGTH" ]; then
add_object_key "web" 'DOMAIN' "$domain" 'FASTCGI_LENGTH' 'ALIAS'
fi
# Set FastCGI cache flag to enabled
update_object_value 'web' 'DOMAIN' "$domain" '$FASTCGI_CACHE' 'yes'
update_object_value 'web' 'DOMAIN' "$domain" '$FASTCGI_LENGTH' "$length"
if [ "$restart" = "yes" ]; then
# Restart web server
$BIN/v-restart-web
check_result $? "Web server restart failed" > /dev/null
fi
# Logging
log_history "Enabled FastCGI cache for $domain"
log_event "$OK" "$ARGUMENTS"
exit