Skip to content

Commit b4fabfc

Browse files
authored
[Feature] Nginx Fast CGI Cache support (hestiacp#1378)
* Revert "Revert "Merge branch 'cmstew-cache-improvements' into staging/fixes"" This reverts commit d323e99. * Fixes duplicate key_zones * Add webui support for nginx-cache feature * Add “Delete” cache support * Fix minor issues update layout * - Fixed issues with javascript - Allow removal of cache via command line (Please not not optimal preformance) - Added support for debugging cache prefomance via debug flag. * Update templates + js * Update all templates to include %home%/%user%/conf/web/%domain%/nginx.fastcgi_cache.conf; * - Added update support - Delete cache * Update permsions + changed button + Allowed “users” level to do the same * Delete cache script * Add “info” icon for more information * Renamed v-delete-web-domain-nginx-cache to v-purge-xxx * Update PHP script * Updated upgrade script to 1.3.3
1 parent de7a08e commit b4fabfc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+474
-47
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#!/bin/bash
2+
# info: Adding fast cgi nginx support
3+
# options: USER DOMAIN [DEBUG]
4+
# labels: hestia web
5+
#
6+
# example: v-add-web-domain-fast-cgi-cache user domain.tld
7+
#
8+
# Function enables fast cgi support for Nginx
9+
# Add "yes" as last parameter append debug information to response headers
10+
11+
12+
#----------------------------------------------------------#
13+
# Variable&Function #
14+
#----------------------------------------------------------#
15+
16+
# Argument definition
17+
user=$1
18+
domain=$2
19+
debug=$3
20+
21+
# Includes
22+
source $HESTIA/func/main.sh
23+
source $HESTIA/conf/hestia.conf
24+
25+
26+
#----------------------------------------------------------#
27+
# Verifications #
28+
#----------------------------------------------------------#
29+
30+
check_args '2' "$#" 'USER DOMAIN DEBUG'
31+
is_format_valid 'user' 'domain'
32+
is_object_valid 'user' 'USER' "$user"
33+
is_object_unsuspended 'user' 'USER' "$user"
34+
is_object_valid 'web' 'DOMAIN' "$domain"
35+
is_object_unsuspended 'web' 'DOMAIN' "$domain"
36+
37+
# Perform verification if read-only mode is enabled
38+
check_hestia_demo_mode
39+
40+
41+
#----------------------------------------------------------#
42+
# Action #
43+
#----------------------------------------------------------#
44+
45+
# Load domain data
46+
parse_object_kv_list $(grep "DOMAIN='$domain'" $USER_DATA/web.conf)
47+
48+
# Check if web server is NGINX standalone
49+
if [ "$WEB_SYSTEM" != 'nginx' ]; then
50+
echo "Error: NGINX not in Stand Alone mode"
51+
exit $E_NOTEXIST
52+
fi
53+
54+
55+
if ! grep --quiet "forcessl" $HESTIA/data/templates/web/nginx/default.tpl; then
56+
$BIN/v-update-web-templates
57+
fi
58+
fastcgi="$HOMEDIR/$user/conf/web/$domain/$WEB_SYSTEM.fastcgi_cache.conf"
59+
no_cache='$no_cache'
60+
cookie_session='$cookie_session'
61+
http_x_update='$http_x_update'
62+
status='$upstream_cache_status'
63+
64+
cat << EOF > $fastcgi
65+
fastcgi_cache $domain;
66+
fastcgi_no_cache $no_cache;
67+
fastcgi_cache_bypass $no_cache;
68+
fastcgi_cache_bypass $cookie_session $http_x_update;
69+
EOF
70+
71+
if [ ! -z "$debug" ]; then
72+
echo " add_header \"X-STATUS\" \"$status\";" >> $fastcgi
73+
fi
74+
75+
chown root:$user $fastcgi
76+
chmod 640 $fastcgi
77+
78+
str="fastcgi_cache_path /var/cache/nginx/php-fpm/$domain levels=2"
79+
str="$str keys_zone=$domain:10m inactive=60m max_size=512m;"
80+
conf='/etc/nginx/conf.d/01_fast_cgi_caching_pool.conf'
81+
if [ -e "$conf" ]; then
82+
if [ -z "$(grep "=${domain}:" $conf)" ]; then
83+
echo "$str" >> $conf
84+
fi
85+
else
86+
echo "$str" >> $conf
87+
fi
88+
89+
mkdir -p /var/cache/nginx/php-fpm/$domain
90+
91+
#----------------------------------------------------------#
92+
# Hestia #
93+
#----------------------------------------------------------#
94+
95+
if [ -z "$FASTCGI" ]; then
96+
add_object_key "web" 'DOMAIN' "$domain" 'FASTCGI_CACHE' 'ALIAS'
97+
fi
98+
99+
# Set FASTCGI flag to enabled
100+
update_object_value 'web' 'DOMAIN' "$domain" '$FASTCGI_CACHE' 'yes'
101+
102+
# Restart web server
103+
$BIN/v-restart-web
104+
check_result $? "Web restart failed" > /dev/null
105+
106+
# Logging
107+
log_history "enabled fast cgi support for $domain"
108+
log_event "$OK" "$ARGUMENTS"
109+
110+
exit

bin/v-add-web-domain-ssl-preset

100644100755
File mode changed.

bin/v-change-user-role

100644100755
File mode changed.

bin/v-change-web-domain-docroot

100644100755
File mode changed.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/bin/bash
2+
# info: remove fast cgi nginx support
3+
# options: USER DOMAIN [RESTART]
4+
# labels: hestia web
5+
#
6+
# example: v-delete-web-domain-fast-cgi-cache user domain.tld
7+
#
8+
# The function removes fast cgi cache.
9+
10+
#----------------------------------------------------------#
11+
# Variable&Function #
12+
#----------------------------------------------------------#
13+
14+
# Argument definition
15+
user=$1
16+
domain=$2
17+
restart=$3
18+
19+
# Includes
20+
source $HESTIA/func/main.sh
21+
source $HESTIA/conf/hestia.conf
22+
23+
#----------------------------------------------------------#
24+
# Verifications #
25+
#----------------------------------------------------------#
26+
27+
check_args '2' "$#" 'USER DOMAIN'
28+
is_format_valid 'user' 'domain'
29+
is_object_valid 'user' 'USER' "$user"
30+
is_object_unsuspended 'user' 'USER' "$user"
31+
is_object_valid 'web' 'DOMAIN' "$domain"
32+
is_object_unsuspended 'web' 'DOMAIN' "$domain"
33+
is_object_valid 'web' 'DOMAIN' "$domain" "$FASTCGI_CACHE"
34+
35+
# Perform verification if read-only mode is enabled
36+
check_hestia_demo_mode
37+
38+
39+
#----------------------------------------------------------#
40+
# Action #
41+
#----------------------------------------------------------#
42+
43+
# Load domain data
44+
parse_object_kv_list $(grep "DOMAIN='$domain'" $USER_DATA/web.conf)
45+
46+
# Remove fast cgi configs
47+
if [ -f $HOMEDIR/$user/conf/web/$domain/$WEB_SYSTEM.fastcgi_cache.conf ]; then
48+
rm -f $HOMEDIR/$user/conf/web/$domain/$WEB_SYSTEM.fastcgi_cache.conf
49+
fi
50+
51+
# Delete cache folder on disabling
52+
if [ -d /var/cache/nginx/php-fpm/$domain ]; then
53+
rm -f /var/cache/nginx/php-fpm/$domain
54+
fi
55+
56+
#----------------------------------------------------------#
57+
# Hestia #
58+
#----------------------------------------------------------#
59+
60+
if [ -z "$FASTCGI_CACHE" ]; then
61+
add_object_key "web" 'DOMAIN' "$domain" 'FASTCGI_CACHE' 'ALIAS'
62+
fi
63+
64+
# Set FASTCGI flag to disabled
65+
update_object_value 'web' 'DOMAIN' "$domain" '$FASTCGI_CACHE' ''
66+
67+
# Restart services if requested
68+
if [ ! -z "$restart" ]; then
69+
$BIN/v-restart-web
70+
check_result $? "Web restart failed" >/dev/null
71+
fi
72+
73+
# Logging
74+
log_history "disabled fast cgi support for $domain"
75+
log_event "$OK" "$ARGUMENTS"
76+
77+
exit

bin/v-list-web-domain

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ json_list() {
4444
"BACKEND": "'$BACKEND'",
4545
"PROXY": "'$PROXY'",
4646
"PROXY_EXT": "'$PROXY_EXT'",
47+
"FASTCGI_CACHE": "'$FASTCGI_CACHE'",
4748
"CUSTOM_DOCROOT": "'$CUSTOM_DOCROOT'",
4849
"SUSPENDED": "'$SUSPENDED'",
4950
"TIME": "'$TIME'",

bin/v-purge-web-domain-nginx-cache

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/bin/bash
2+
# info: Empty nginx cache
3+
# options: USER DOMAIN MODE
4+
# labels: hestia web
5+
#
6+
# example: v-purge-web-domain-nginx-cache user domain.tld proxy
7+
#
8+
# The function clears Nginx cache.
9+
10+
#----------------------------------------------------------#
11+
# Variable&Function #
12+
#----------------------------------------------------------#
13+
14+
# Argument definition
15+
user=$1
16+
domain=$2
17+
18+
# Includes
19+
source $HESTIA/func/main.sh
20+
source $HESTIA/conf/hestia.conf
21+
22+
#----------------------------------------------------------#
23+
# Verifications #
24+
#----------------------------------------------------------#
25+
26+
check_args '2' "$#" 'USER DOMAIN'
27+
is_format_valid 'user' 'domain'
28+
is_object_valid 'user' 'USER' "$user"
29+
is_object_unsuspended 'user' 'USER' "$user"
30+
is_object_valid 'web' 'DOMAIN' "$domain"
31+
is_object_unsuspended 'web' 'DOMAIN' "$domain"
32+
is_object_valid 'web' 'DOMAIN' "$domain" "$FASTCGI_CACHE"
33+
34+
# Perform verification if read-only mode is enabled
35+
check_hestia_demo_mode
36+
37+
38+
#----------------------------------------------------------#
39+
# Action #
40+
#----------------------------------------------------------#
41+
42+
# Load domain data
43+
parse_object_kv_list $(grep "DOMAIN='$domain'" $USER_DATA/web.conf)
44+
45+
# Empty Fast CGI Cache
46+
if [ -d /var/cache/nginx/php-fpm/$domain ]; then
47+
rm -fr /var/cache/nginx/php-fpm/$domain/*
48+
fi
49+
# Empty Proxy Cache
50+
if [ -d /var/cache/nginx/$domain ]; then
51+
rm -fr /var/cache/nginx/$domain/*
52+
fi
53+
#----------------------------------------------------------#
54+
# Hestia #
55+
#----------------------------------------------------------#
56+
57+
# Restart services if requested
58+
if [ ! -z "$restart" ]; then
59+
$BIN/v-restart-web
60+
check_result $? "Web restart failed" >/dev/null
61+
fi
62+
63+
# Logging
64+
log_history "purged nginx cache for $domain"
65+
log_event "$OK" "$ARGUMENTS"
66+
67+
exit

install/deb/nginx/nginx.conf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ http {
131131
proxy_cache_use_stale error timeout invalid_header http_502;
132132
proxy_cache_valid any 1d;
133133

134+
# FastCGI Cache settings
135+
fastcgi_cache_path /var/cache/nginx/php-fpm levels=2 keys_zone=fcgi_cache:10m inactive=60m max_size=1024m;
136+
fastcgi_cache_key "$host$request_uri $cookie_user";
137+
fastcgi_temp_path /var/cache/nginx/temp;
138+
fastcgi_ignore_headers Expires Cache-Control;
139+
fastcgi_cache_use_stale error timeout invalid_header;
140+
fastcgi_cache_valid any 1d;
141+
134142
# Cache bypass
135143
map $http_cookie $no_cache {
136144
default 0;

install/deb/templates/web/nginx/caching.stpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ server {
1717
location / {
1818
proxy_pass https://%ip%:%web_ssl_port%;
1919

20-
proxy_cache cache;
20+
proxy_cache %domain%;
2121
proxy_cache_valid 15m;
2222
proxy_cache_valid 404 1m;
2323
proxy_no_cache $no_cache;

install/deb/templates/web/nginx/caching.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ server {
1212
location / {
1313
proxy_pass http://%ip%:%web_port%;
1414
15-
proxy_cache cache;
15+
proxy_cache %domain%;
1616
proxy_cache_valid 15m;
1717
proxy_cache_valid 404 1m;
1818
proxy_no_cache $no_cache;

0 commit comments

Comments
 (0)