Skip to content

Commit ece53bc

Browse files
author
Kristan Kenney
authored
Merge pull request hestiacp#1707 from hestiacp/fastcgi-add-valid-length
Fastcgi Cache duration
2 parents 5d82dc8 + 0186114 commit ece53bc

File tree

11 files changed

+94
-34
lines changed

11 files changed

+94
-34
lines changed

bin/v-add-fastcgi-cache

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
#!/bin/bash
2-
# info: Add FastCGI nginx support
3-
# options: USER DOMAIN [DEBUG]
2+
# info: Enable FastCGI cache for nginx
3+
# options: USER DOMAIN [DURATION] [DEBUG] [RESTART]
44
# labels: hestia web
55
#
6-
# example: v-add-fastcgi-cache user domain.tld
6+
# example: v-add-fastcgi-cache user domain.tld 30m
77
#
88
# The function enables FastCGI cache for nginx
9+
# Acceptable values for duration is time in seconds (10s) minutes (10m) or days (10d)
910
# Add "yes" as last parameter to append debug information to response headers
1011

11-
1212
#----------------------------------------------------------#
1313
# Variable&Function #
1414
#----------------------------------------------------------#
1515

1616
# Argument definition
1717
user=$1
1818
domain=$2
19-
debug=$3
19+
duration=${3-2m}
20+
debug=${4-no}
21+
restart=${5-no}
2022

2123
# Includes
2224
# shellcheck source=/usr/local/hestia/func/main.sh
@@ -36,6 +38,16 @@ is_object_unsuspended 'user' 'USER' "$user"
3638
is_object_valid 'web' 'DOMAIN' "$domain"
3739
is_object_unsuspended 'web' 'DOMAIN' "$domain"
3840

41+
if ! [[ "$duration" =~ ^[0-9].*[s|m|d]$ ]]; then
42+
echo "Invalid duration";
43+
exit 2;
44+
fi
45+
46+
if [[ "$duration" =~ ^[0].*[s|m|d]$ ]]; then
47+
echo "Invalid duration";
48+
exit 2;
49+
fi
50+
3951
# Perform verification if read-only mode is enabled
4052
check_hestia_demo_mode
4153

@@ -47,7 +59,7 @@ check_hestia_demo_mode
4759
# Load domain data
4860
parse_object_kv_list $(grep "DOMAIN='$domain'" $USER_DATA/web.conf)
4961

50-
# Check if nginx is not in proxy mode
62+
# Check that nginx is not in proxy mode
5163
if [ "$WEB_SYSTEM" != 'nginx' ]; then
5264
echo "Error: nginx is in proxy mode"
5365
exit $E_NOTEXIST
@@ -65,7 +77,7 @@ status='$upstream_cache_status'
6577

6678
cat << EOF > $fastcgi
6779
fastcgi_cache $domain;
68-
fastcgi_cache_valid 200 2m;
80+
fastcgi_cache_valid 200 $duration;
6981
fastcgi_cache_valid 301 302 10m;
7082
fastcgi_cache_valid 404 10m;
7183
fastcgi_cache_bypass $no_cache;
@@ -96,16 +108,22 @@ mkdir -p /var/cache/nginx/micro/$domain
96108
# Hestia #
97109
#----------------------------------------------------------#
98110

99-
if [ -z "$FASTCGI" ]; then
111+
if [ -z "$FASTCGI_CACHE" ]; then
100112
add_object_key "web" 'DOMAIN' "$domain" 'FASTCGI_CACHE' 'ALIAS'
101113
fi
114+
if [ -z "$FASTCGI_DURATION" ]; then
115+
add_object_key "web" 'DOMAIN' "$domain" 'FASTCGI_DURATION' 'ALIAS'
116+
fi
102117

103118
# Set FastCGI cache flag to enabled
104119
update_object_value 'web' 'DOMAIN' "$domain" '$FASTCGI_CACHE' 'yes'
120+
update_object_value 'web' 'DOMAIN' "$domain" '$FASTCGI_DURATION' "$duration"
105121

106-
# Restart web server
107-
$BIN/v-restart-web
108-
check_result $? "Web server restart failed" > /dev/null
122+
if [ "$restart" = "yes" ]; then
123+
# Restart web server
124+
$BIN/v-restart-web
125+
check_result $? "Web server restart failed" > /dev/null
126+
fi
109127

110128
# Logging
111129
log_history "Enabled FastCGI cache for $domain"

bin/v-delete-fastcgi-cache

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#!/bin/bash
2-
# info: Remove FastCGI nginx support
2+
# info: Disable FastCGI cache for nginx
33
# options: USER DOMAIN [RESTART]
44
# labels: hestia web
55
#
66
# example: v-delete-fastcgi-cache user domain.tld
77
#
8-
# The function enables FastCGI cache for nginx
8+
# The function disables FastCGI cache for nginx
99

1010

1111
#----------------------------------------------------------#
@@ -51,7 +51,7 @@ fi
5151

5252
conf='/etc/nginx/conf.d/fastcgi_cache_pool.conf'
5353
if [ -f "$conf" ]; then
54-
sed -i "/fastcgi_cache $domain;/d" $conf
54+
sed -i "/ keys_zone=$domain/d" $conf
5555
if [ ! -s "$conf" ]; then
5656
rm -rf $conf
5757
fi
@@ -68,11 +68,15 @@ fi
6868
#----------------------------------------------------------#
6969

7070
if [ -z "$FASTCGI_CACHE" ]; then
71-
add_object_key "web" 'DOMAIN' "$domain" 'FASTCGI_CACHE' 'ALIAS'
71+
add_object_key "web" 'DOMAIN' "$domain" 'FASTCGI_CACHE' 'ALIAS'
72+
fi
73+
if [ -z "$FASTCGI_DURATION" ]; then
74+
add_object_key "web" 'DOMAIN' "$domain" 'FASTCGI_DURATION' 'ALIAS'
7275
fi
7376

74-
# Set FASTCGI flag to disabled
75-
update_object_value 'web' 'DOMAIN' "$domain" '$FASTCGI_CACHE' ''
77+
# Set FastCGI cache flag to disabled
78+
update_object_value 'web' 'DOMAIN' "$domain" '$FASTCGI_CACHE' 'no'
79+
update_object_value 'web' 'DOMAIN' "$domain" '$FASTCGI_DURATION' '0s'
7680

7781
# Restart web server
7882
if [ ! -z "$restart" ]; then

bin/v-delete-web-domain

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ if [ "$SSL" = 'yes' ]; then
8181
rm -f $USER_DATA/ssl/$domain.*
8282
fi
8383

84+
if [ "$FASTCGI_CACHE" = "yes" ]; then
85+
# make sure no trails left behind
86+
$BIN/v-delete-fastcgi-cache $user $domain
87+
fi
88+
8489
# Deleting domain from web.conf
8590
sed -i "/DOMAIN='$domain'/ d" $USER_DATA/web.conf
8691

@@ -111,7 +116,6 @@ rm -f /var/log/$WEB_SYSTEM/domains/$domain.error*
111116
rm -rf $HOMEDIR/$user/web/$domain
112117
rm -rf $HOMEDIR/$user/conf/web/$domain
113118

114-
115119
#----------------------------------------------------------#
116120
# Hestia #
117121
#----------------------------------------------------------#

bin/v-list-web-domain

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ json_list() {
4646
"PROXY": "'$PROXY'",
4747
"PROXY_EXT": "'$PROXY_EXT'",
4848
"FASTCGI_CACHE": "'$FASTCGI_CACHE'",
49+
"FASTCGI_DURATION": "'$FASTCGI_DURATION'",
4950
"REDIRECT": "'$REDIRECT'",
5051
"REDIRECT_CODE": "'$REDIRECT_CODE'",
5152
"CUSTOM_DOCROOT": "'$CUSTOM_DOCROOT'",

bin/v-restore-user

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ if [ "$web" != 'no' ] && [ ! -z "$WEB_SYSTEM" ]; then
314314
str="DOMAIN='$domain' IP='$IP' IP6='$IP6' ALIAS='$ALIAS'"
315315
str="$str CUSTOM_DOCROOT='$CUSTOM_DOCROOT' CUSTOM_PHPROOT='$CUSTOM_PHPROOT'"
316316
str="$str REDIRECT='$REDIRECT' REDIRECT_CODE='$REDIRECT_CODE'"
317-
str="$str FASTCGI_CACHE='$FASTCGI_CACHE' FASTCGI_CACHE_LENGTH='$FASTCGI_CACHE_LENGTH'"
317+
str="$str FASTCGI_CACHE='$FASTCGI_CACHE' FASTCGI_DURATION='$FASTCGI_DURATION'"
318318
str="$str TPL='$TPL' SSL='$SSL' SSL_HOME='$SSL_HOME'"
319319
str="$str LETSENCRYPT='$LETSENCRYPT' FTP_USER='$FTP_USER'"
320320
str="$str FTP_MD5='$FTP_MD5' BACKEND='$BACKEND' PROXY='$PROXY'"

func/rebuild.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,10 @@ rebuild_web_domain_conf() {
289289
$BIN/v-delete-web-domain-ssl-hsts $user $domain no
290290
$BIN/v-add-web-domain-ssl-hsts $user $domain yes
291291
fi
292+
if [ "$FASTCGI_CACHE" = 'yes' ]; then
293+
$BIN/v-delete-fastcgi-cache $user $domain
294+
$BIN/v-add-fastcgi-cache $user $domain "$FASTCGI_DURATION"
295+
fi
292296

293297
# Adding proxy configuration
294298
if [ ! -z "$PROXY_SYSTEM" ] && [ ! -z "$PROXY" ]; then

func/syshealth.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function syshealth_update_web_config_format() {
4545
# WEB DOMAINS
4646
# Create array of known keys in configuration file
4747
system="web"
48-
known_keys=(DOMAIN IP IP6 CUSTOM_DOCROOT CUSTOM_PHPROOT FASTCGI_CACHE FASTCGI_LENGTH ALIAS TPL SSL SSL_FORCE SSL_HOME LETSENCRYPT FTP_USER FTP_MD5 FTP_PATH BACKEND PROXY PROXY_EXT STATS STATS_USER STATS_CRYPT SUSPENDED TIME DATE)
48+
known_keys=(DOMAIN IP IP6 CUSTOM_DOCROOT CUSTOM_PHPROOT FASTCGI_CACHE FASTCGI_DURATION ALIAS TPL SSL SSL_FORCE SSL_HOME LETSENCRYPT FTP_USER FTP_MD5 FTP_PATH BACKEND PROXY PROXY_EXT STATS STATS_USER STATS_CRYPT SUSPENDED TIME DATE)
4949
write_kv_config_file
5050
unset system
5151
unset known_keys

web/edit/web/index.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@
6868
$v_ssl_home = $data[$v_domain]['SSL_HOME'];
6969
$v_backend_template = $data[$v_domain]['BACKEND'];
7070
$v_nginx_cache = $data[$v_domain]['FASTCGI_CACHE'];
71+
$v_nginx_cache_duration = $data[$v_domain]['FASTCGI_DURATION'];
72+
$v_nginx_cache_check = '';
73+
if(empty($v_nginx_cache_duration)){
74+
$v_nginx_cache_duration = '2m';
75+
$v_nginx_cache_check = '';
76+
}else{
77+
$v_nginx_cache_check = 'on';
78+
}
7179
$v_proxy = $data[$v_domain]['PROXY'];
7280
$v_proxy_template = $data[$v_domain]['PROXY'];
7381
$v_proxy_ext = str_replace(',', ', ', $data[$v_domain]['PROXY_EXT']);
@@ -329,17 +337,20 @@
329337
}
330338

331339
// Enable/Disable nginx cache
332-
if (($_SESSION['WEB_SYSTEM'] == 'nginx') && ($v_nginx_cache != $_POST['v_nginx_cache'] ) && (empty($_SESSION['error_msg']))) {
333-
if ( $_POST['v_nginx_cache'] == 'yes' ) {
334-
exec (HESTIA_CMD."v-add-fastcgi-cache ".$v_username." ".escapeshellarg($v_domain), $output, $return_var);
335-
check_return_code($return_var,$output);
336-
unset($output);
340+
if (($_SESSION['WEB_SYSTEM'] == 'nginx') && ($v_nginx_cache_check != $_POST['v_nginx_cache_check'] ) || ($v_nginx_cache_duration != $_POST['v_nginx_cache_duration'] && $_POST['v_nginx_cache'] = "yes" ) && (empty($_SESSION['error_msg']))) {
341+
if ( $_POST['v_nginx_cache_check'] == 'on' ) {
342+
if (empty ($_POST['v_nginx_cache_duration'])){
343+
echo $_POST['v_nginx_cache_duration'] = "2m";
344+
}
345+
exec (HESTIA_CMD."v-add-fastcgi-cache ".$v_username." ".escapeshellarg($v_domain).' '. escapeshellarg($_POST['v_nginx_cache_duration']) , $output, $return_var);
346+
check_return_code($return_var,$output);
347+
unset($output);
337348
} else {
338349
exec (HESTIA_CMD."v-delete-fastcgi-cache ".$v_username." ".escapeshellarg($v_domain), $output, $return_var);
339350
check_return_code($return_var,$output);
340351
unset($output);
341352
}
342-
$restart_proxy = 'yes';
353+
$restart_web = 'yes';
343354
}
344355

345356
// Delete proxy support
@@ -348,7 +359,7 @@
348359
check_return_code($return_var,$output);
349360
unset($output);
350361
unset($v_proxy);
351-
$restart_proxy = 'yes';
362+
$restart_web = 'yes';
352363
}
353364

354365
// Change proxy template / Update extension list

web/js/pages/edit_web.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,10 @@ $(function() {
241241

242242
if(select.val() != 'yes'){
243243
$('#v-clear-cache').hide();
244+
$('#v_nginx_cache_length').hide();
244245
} else {
245246
$('#v-clear-cache').show();
247+
$('#v_nginx_cache_length').show();
246248
}
247249
});
248250

web/templates/admin/edit_web.html

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -381,16 +381,20 @@
381381
</tr>
382382
<?php if($_SESSION['WEB_SYSTEM'] == 'nginx'){?>
383383
<tr>
384-
<td class="vst-text input-label">
385-
<?php print _('Enable Fast CGI Cache'); ?> <a href="https://docs.hestiacp.com/admin_docs/nginx_caching.html" target="_blank"><i class="fas fa-question-circle"></i></a>
384+
<td class="vst-text">
385+
<label><input type="checkbox" size="20" class="vst-checkbox" name="v_nginx_cache_check" <?php if (!empty($v_nginx_cache)) echo "checked=yes" ?> onclick="javascript:elementHideShow('v_nginx_duration');"><?php print _('Enable FastCGI Cache'); ?> <a href="https://docs.hestiacp.com/admin_docs/nginx_caching.html" target="_blank"><i class="fas fa-question-circle"></i></a></label>
386386
</td>
387387
</tr>
388388
<tr>
389-
<td>
390-
<select class="vst-list" name="v_nginx_cache">
391-
<option value="no"><?php echo _('No');?></option>
392-
<option value="yes" <?php if ( $v_nginx_cache == "yes") { echo 'selected'; }?>><?php echo _('Yes');?></option>
393-
</select>
389+
<td class="step-left">
390+
<table id="v_nginx_duration" style="display:<?php if ($v_nginx_cache != 'yes' ) { echo 'none';} else {echo 'block';}?> ;" >
391+
<td class="vst-text input-label">
392+
<?php print _('Cache Duration'); ?> <span class="optional"><?=_('For example: 30s, 10m or 1d');?>
393+
</td>
394+
<tr>
395+
<td><input type="text" size="20" class="vst-input" name="v_nginx_cache_duration" value="<?=htmlentities(trim($v_nginx_cache_duration, "'"))?>"></td>
396+
</tr>
397+
</table>
394398
</td>
395399
</tr>
396400
<?php } ?>

0 commit comments

Comments
 (0)