Kaynağa Gözat

Merge pull request #1707 from hestiacp/fastcgi-add-valid-length

Fastcgi Cache duration
Kristan Kenney 5 yıl önce
ebeveyn
işleme
ece53bc5de

+ 29 - 11
bin/v-add-fastcgi-cache

@@ -1,14 +1,14 @@
 #!/bin/bash
-# info: Add FastCGI nginx support
-# options: USER DOMAIN [DEBUG]
+# info: Enable FastCGI cache for nginx
+# options: USER DOMAIN [DURATION] [DEBUG] [RESTART]
 # labels: hestia web
 #
-# example: v-add-fastcgi-cache user domain.tld
+# example: v-add-fastcgi-cache user domain.tld 30m
 #
 # The function enables FastCGI cache for nginx
+# Acceptable values for duration is time in seconds (10s) minutes (10m) or days (10d)
 # Add "yes" as last parameter to append debug information to response headers
 
-
 #----------------------------------------------------------#
 #                    Variable&Function                     #
 #----------------------------------------------------------#
@@ -16,7 +16,9 @@
 # Argument definition
 user=$1
 domain=$2
-debug=$3
+duration=${3-2m}
+debug=${4-no}
+restart=${5-no}
 
 # Includes
 # shellcheck source=/usr/local/hestia/func/main.sh
@@ -36,6 +38,16 @@ is_object_unsuspended 'user' 'USER' "$user"
 is_object_valid 'web' 'DOMAIN' "$domain"
 is_object_unsuspended 'web' 'DOMAIN' "$domain"
 
+if ! [[ "$duration" =~ ^[0-9].*[s|m|d]$ ]]; then
+   echo "Invalid duration";
+   exit 2;
+fi
+
+if [[ "$duration" =~ ^[0].*[s|m|d]$ ]]; then
+   echo "Invalid duration";
+   exit 2;
+fi
+
 # Perform verification if read-only mode is enabled
 check_hestia_demo_mode
 
@@ -47,7 +59,7 @@ check_hestia_demo_mode
 # Load domain data
 parse_object_kv_list $(grep "DOMAIN='$domain'" $USER_DATA/web.conf)
 
-# Check if nginx is not in proxy mode
+# Check that nginx is not in proxy mode
 if [ "$WEB_SYSTEM" != 'nginx' ]; then
     echo "Error: nginx is in proxy mode"
     exit $E_NOTEXIST
@@ -65,7 +77,7 @@ status='$upstream_cache_status'
 
 cat << EOF > $fastcgi
     fastcgi_cache $domain;
-    fastcgi_cache_valid 200 2m;
+    fastcgi_cache_valid 200 $duration;
     fastcgi_cache_valid 301 302 10m;
     fastcgi_cache_valid 404 10m;
     fastcgi_cache_bypass $no_cache;
@@ -96,16 +108,22 @@ mkdir -p /var/cache/nginx/micro/$domain
 #                       Hestia                             #
 #----------------------------------------------------------#
 
-if [ -z "$FASTCGI" ]; then
+if [ -z "$FASTCGI_CACHE" ]; then
     add_object_key "web" 'DOMAIN' "$domain" 'FASTCGI_CACHE' 'ALIAS'
 fi
+if [ -z "$FASTCGI_DURATION" ]; then
+    add_object_key "web" 'DOMAIN' "$domain" 'FASTCGI_DURATION' 'ALIAS'
+fi
 
 # Set FastCGI cache flag to enabled
 update_object_value 'web' 'DOMAIN' "$domain" '$FASTCGI_CACHE' 'yes'
+update_object_value 'web' 'DOMAIN' "$domain" '$FASTCGI_DURATION' "$duration"
 
-# Restart web server
-$BIN/v-restart-web
-check_result $? "Web server restart failed" > /dev/null
+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"

+ 10 - 6
bin/v-delete-fastcgi-cache

@@ -1,11 +1,11 @@
 #!/bin/bash
-# info: Remove FastCGI nginx support
+# info: Disable FastCGI cache for nginx
 # options: USER DOMAIN [RESTART]
 # labels: hestia web
 #
 # example: v-delete-fastcgi-cache user domain.tld
 #
-# The function enables FastCGI cache for nginx
+# The function disables FastCGI cache for nginx
 
 
 #----------------------------------------------------------#
@@ -51,7 +51,7 @@ fi
 
 conf='/etc/nginx/conf.d/fastcgi_cache_pool.conf'
 if [ -f "$conf" ]; then
-    sed -i "/fastcgi_cache $domain;/d" $conf
+    sed -i "/ keys_zone=$domain/d" $conf
     if [ ! -s "$conf" ]; then
         rm -rf $conf
     fi
@@ -68,11 +68,15 @@ fi
 #----------------------------------------------------------#
 
 if [ -z "$FASTCGI_CACHE" ]; then
-    add_object_key "web" 'DOMAIN' "$domain" 'FASTCGI_CACHE' 'ALIAS'
+add_object_key "web" 'DOMAIN' "$domain" 'FASTCGI_CACHE' 'ALIAS'
+fi
+if [ -z "$FASTCGI_DURATION" ]; then
+add_object_key "web" 'DOMAIN' "$domain" 'FASTCGI_DURATION' 'ALIAS'
 fi
 
-# Set FASTCGI flag to disabled
-update_object_value 'web' 'DOMAIN' "$domain" '$FASTCGI_CACHE' ''
+# Set FastCGI cache flag to disabled
+update_object_value 'web' 'DOMAIN' "$domain" '$FASTCGI_CACHE' 'no'
+update_object_value 'web' 'DOMAIN' "$domain" '$FASTCGI_DURATION' '0s'
 
 # Restart web server
 if [ ! -z "$restart" ]; then

+ 5 - 1
bin/v-delete-web-domain

@@ -81,6 +81,11 @@ if [ "$SSL" = 'yes' ]; then
     rm -f $USER_DATA/ssl/$domain.*
 fi
 
+if [ "$FASTCGI_CACHE" = "yes" ]; then
+    # make sure no trails left behind 
+    $BIN/v-delete-fastcgi-cache $user $domain
+fi
+
 # Deleting domain from web.conf
 sed -i "/DOMAIN='$domain'/ d" $USER_DATA/web.conf
 
@@ -111,7 +116,6 @@ rm -f /var/log/$WEB_SYSTEM/domains/$domain.error*
 rm -rf $HOMEDIR/$user/web/$domain
 rm -rf $HOMEDIR/$user/conf/web/$domain
 
-
 #----------------------------------------------------------#
 #                       Hestia                             #
 #----------------------------------------------------------#

+ 1 - 0
bin/v-list-web-domain

@@ -46,6 +46,7 @@ json_list() {
         "PROXY": "'$PROXY'",
         "PROXY_EXT": "'$PROXY_EXT'",
         "FASTCGI_CACHE": "'$FASTCGI_CACHE'",
+        "FASTCGI_DURATION": "'$FASTCGI_DURATION'",
         "REDIRECT": "'$REDIRECT'",
         "REDIRECT_CODE": "'$REDIRECT_CODE'",
         "CUSTOM_DOCROOT": "'$CUSTOM_DOCROOT'",

+ 1 - 1
bin/v-restore-user

@@ -314,7 +314,7 @@ if [ "$web" != 'no' ] && [ ! -z "$WEB_SYSTEM" ]; then
             str="DOMAIN='$domain' IP='$IP' IP6='$IP6' ALIAS='$ALIAS'"
             str="$str CUSTOM_DOCROOT='$CUSTOM_DOCROOT' CUSTOM_PHPROOT='$CUSTOM_PHPROOT'"
             str="$str REDIRECT='$REDIRECT' REDIRECT_CODE='$REDIRECT_CODE'"
-            str="$str FASTCGI_CACHE='$FASTCGI_CACHE' FASTCGI_CACHE_LENGTH='$FASTCGI_CACHE_LENGTH'"
+            str="$str FASTCGI_CACHE='$FASTCGI_CACHE' FASTCGI_DURATION='$FASTCGI_DURATION'"
             str="$str TPL='$TPL' SSL='$SSL' SSL_HOME='$SSL_HOME'"
             str="$str LETSENCRYPT='$LETSENCRYPT' FTP_USER='$FTP_USER'"
             str="$str FTP_MD5='$FTP_MD5' BACKEND='$BACKEND' PROXY='$PROXY'"

+ 4 - 0
func/rebuild.sh

@@ -289,6 +289,10 @@ rebuild_web_domain_conf() {
         $BIN/v-delete-web-domain-ssl-hsts $user $domain no
         $BIN/v-add-web-domain-ssl-hsts $user $domain yes
     fi
+    if [ "$FASTCGI_CACHE" = 'yes' ]; then
+        $BIN/v-delete-fastcgi-cache $user $domain
+        $BIN/v-add-fastcgi-cache $user $domain "$FASTCGI_DURATION"
+    fi
 
     # Adding proxy configuration
     if [ ! -z "$PROXY_SYSTEM" ] && [ ! -z "$PROXY" ]; then

+ 1 - 1
func/syshealth.sh

@@ -45,7 +45,7 @@ function syshealth_update_web_config_format() {
     # WEB DOMAINS
     # Create array of known keys in configuration file
     system="web"
-    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)
+    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)
     write_kv_config_file
     unset system
     unset known_keys

+ 18 - 7
web/edit/web/index.php

@@ -68,6 +68,14 @@ if (empty($v_letsencrypt)) $v_letsencrypt = 'no';
 $v_ssl_home = $data[$v_domain]['SSL_HOME'];
 $v_backend_template = $data[$v_domain]['BACKEND'];
 $v_nginx_cache = $data[$v_domain]['FASTCGI_CACHE'];
+$v_nginx_cache_duration = $data[$v_domain]['FASTCGI_DURATION'];
+$v_nginx_cache_check = '';
+if(empty($v_nginx_cache_duration)){
+    $v_nginx_cache_duration = '2m';
+    $v_nginx_cache_check = '';
+}else{
+    $v_nginx_cache_check = 'on';
+}
 $v_proxy = $data[$v_domain]['PROXY'];
 $v_proxy_template = $data[$v_domain]['PROXY'];
 $v_proxy_ext = str_replace(',', ', ', $data[$v_domain]['PROXY_EXT']);
@@ -329,17 +337,20 @@ if (!empty($_POST['save'])) {
     }
 
     // Enable/Disable nginx cache
-    if (($_SESSION['WEB_SYSTEM'] == 'nginx') && ($v_nginx_cache != $_POST['v_nginx_cache'] ) && (empty($_SESSION['error_msg']))) {
-        if ( $_POST['v_nginx_cache'] == 'yes' ) {
-           exec (HESTIA_CMD."v-add-fastcgi-cache ".$v_username." ".escapeshellarg($v_domain), $output, $return_var);
-           check_return_code($return_var,$output);
-           unset($output); 
+    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']))) {
+        if ( $_POST['v_nginx_cache_check'] == 'on' ) {
+            if (empty ($_POST['v_nginx_cache_duration'])){
+                echo $_POST['v_nginx_cache_duration'] = "2m";
+            }
+            exec (HESTIA_CMD."v-add-fastcgi-cache ".$v_username." ".escapeshellarg($v_domain).' '. escapeshellarg($_POST['v_nginx_cache_duration']) , $output, $return_var);
+            check_return_code($return_var,$output);
+            unset($output); 
         } else {
             exec (HESTIA_CMD."v-delete-fastcgi-cache ".$v_username." ".escapeshellarg($v_domain), $output, $return_var);
             check_return_code($return_var,$output);
             unset($output); 
         }
-        $restart_proxy = 'yes';
+        $restart_web = 'yes';
     }
 
     // Delete proxy support
@@ -348,7 +359,7 @@ if (!empty($_POST['save'])) {
         check_return_code($return_var,$output);
         unset($output);
         unset($v_proxy);
-        $restart_proxy = 'yes';
+        $restart_web = 'yes';
     }
 
     // Change proxy template / Update extension list

+ 2 - 0
web/js/pages/edit_web.js

@@ -241,8 +241,10 @@ $(function() {
     
         if(select.val() != 'yes'){
             $('#v-clear-cache').hide();
+            $('#v_nginx_cache_length').hide();
         } else {
             $('#v-clear-cache').show();
+            $('#v_nginx_cache_length').show();
         }
     });  
     

+ 11 - 7
web/templates/admin/edit_web.html

@@ -381,16 +381,20 @@
                                         </tr>
                                         <?php if($_SESSION['WEB_SYSTEM'] == 'nginx'){?>
                                         <tr>
-                                            <td class="vst-text input-label">
-                                                <?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>
+                                            <td class="vst-text">
+                                                <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>
                                             </td>
                                         </tr>
                                         <tr>
-                                            <td>
-                                                <select class="vst-list" name="v_nginx_cache">
-                                                    <option value="no"><?php echo _('No');?></option>
-                                                    <option value="yes" <?php if ( $v_nginx_cache == "yes") { echo 'selected'; }?>><?php echo _('Yes');?></option>
-                                                </select>
+                                            <td class="step-left">
+                                                <table id="v_nginx_duration" style="display:<?php if ($v_nginx_cache != 'yes' ) { echo 'none';} else {echo 'block';}?> ;" >
+                                                    <td class="vst-text input-label">
+                                                        <?php print _('Cache Duration'); ?> <span class="optional"><?=_('For example: 30s, 10m or 1d');?>
+                                                    </td>
+                                                    <tr>
+                                                        <td><input type="text" size="20" class="vst-input" name="v_nginx_cache_duration" value="<?=htmlentities(trim($v_nginx_cache_duration, "'"))?>"></td>
+                                                    </tr>
+                                                </table>
                                             </td>
                                         </tr>
                                         <?php } ?>                          

+ 12 - 0
web/templates/user/edit_web.html

@@ -397,6 +397,18 @@
                                         <option value="yes" <?php if ( $v_nginx_cache == "yes") { echo 'selected'; }?>><?php echo _('Yes');?></option>
                                     </select>
                                 </td>
+                            </tr>
+                            <tr>
+                                <td class="step-left">
+                                    <table id="v_nginx_cache_length" style="display:<?php if ($v_nginx_cache == 'no' ) { echo 'none';} else {echo 'block';}?> ;" >
+                                        <td class="vst-text input-label">
+                                            <?php print _('Cache length'); ?> <span class="optional"><?=_('For example: 30s, 10m or 1d');?>
+                                        </td>
+                                        <tr>
+                                            <td><input type="text" size="20" class="vst-input" name="v_nginx_cache_length" value="<?=htmlentities(trim($v_nginx_cache_length, "'"))?>"></td>
+                                        </tr>
+                                    </table>
+                                </td>
                             </tr>
                              <?php } ?>                          
                             <?php if (!empty($_SESSION['WEB_BACKEND'])) { ?>