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
1717user=$1
1818domain=$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"
3638is_object_valid ' web' ' DOMAIN' " $domain "
3739is_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
4052check_hestia_demo_mode
4153
@@ -47,7 +59,7 @@ check_hestia_demo_mode
4759# Load domain data
4860parse_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
5163if [ " $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
6678cat << 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'
101113fi
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
104119update_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
111129log_history " Enabled FastCGI cache for $domain "
0 commit comments