Skip to content

Commit c374942

Browse files
author
Kristan Kenney
authored
Merge pull request hestiacp#1695 from hestiacp/fastcgi-cache
Improvements to FastCGI cache
2 parents b543ea2 + fc4fbaf commit c374942

File tree

7 files changed

+71
-58
lines changed

7 files changed

+71
-58
lines changed
Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#!/bin/bash
2-
# info: Adding fast cgi nginx support
2+
# info: Add FastCGI nginx support
33
# options: USER DOMAIN [DEBUG]
44
# labels: hestia web
55
#
6-
# example: v-add-web-domain-fast-cgi-cache user domain.tld
6+
# example: v-add-fastcgi-cache user domain.tld
77
#
8-
# Function enables fast cgi support for Nginx
9-
# Add "yes" as last parameter append debug information to response headers
8+
# The function enables FastCGI cache for nginx
9+
# Add "yes" as last parameter to append debug information to response headers
1010

1111

1212
#----------------------------------------------------------#
@@ -45,16 +45,16 @@ check_hestia_demo_mode
4545
# Load domain data
4646
parse_object_kv_list $(grep "DOMAIN='$domain'" $USER_DATA/web.conf)
4747

48-
# Check if web server is NGINX standalone
48+
# Check if nginx is not in proxy mode
4949
if [ "$WEB_SYSTEM" != 'nginx' ]; then
50-
echo "Error: NGINX not in Stand Alone mode"
50+
echo "Error: nginx is in proxy mode"
5151
exit $E_NOTEXIST
5252
fi
5353

54-
5554
if ! grep --quiet "forcessl" $HESTIA/data/templates/web/nginx/default.tpl; then
5655
$BIN/v-update-web-templates
5756
fi
57+
5858
fastcgi="$HOMEDIR/$user/conf/web/$domain/$WEB_SYSTEM.fastcgi_cache.conf"
5959
no_cache='$no_cache'
6060
cookie_session='$cookie_session'
@@ -63,9 +63,11 @@ status='$upstream_cache_status'
6363

6464
cat << EOF > $fastcgi
6565
fastcgi_cache $domain;
66-
fastcgi_no_cache $no_cache;
66+
fastcgi_cache_valid 200 2m;
67+
fastcgi_cache_valid 301 302 10m;
68+
fastcgi_cache_valid 404 10m;
6769
fastcgi_cache_bypass $no_cache;
68-
fastcgi_cache_bypass $cookie_session $http_x_update;
70+
fastcgi_no_cache $no_cache;
6971
EOF
7072

7173
if [ ! -z "$debug" ]; then
@@ -75,18 +77,18 @@ fi
7577
chown root:$user $fastcgi
7678
chmod 640 $fastcgi
7779

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
80+
str="fastcgi_cache_path /var/cache/nginx/micro/$domain levels=1:2"
81+
str="$str keys_zone=$domain:10m max_size=512m inactive=30m;"
82+
conf='/etc/nginx/conf.d/fastcgi_cache_pool.conf'
83+
if [ -f "$conf" ]; then
8284
if [ -z "$(grep "=${domain}:" $conf)" ]; then
8385
echo "$str" >> $conf
8486
fi
8587
else
8688
echo "$str" >> $conf
8789
fi
8890

89-
mkdir -p /var/cache/nginx/php-fpm/$domain
91+
mkdir -p /var/cache/nginx/micro/$domain
9092

9193
#----------------------------------------------------------#
9294
# Hestia #
@@ -96,15 +98,15 @@ if [ -z "$FASTCGI" ]; then
9698
add_object_key "web" 'DOMAIN' "$domain" 'FASTCGI_CACHE' 'ALIAS'
9799
fi
98100

99-
# Set FASTCGI flag to enabled
101+
# Set FastCGI cache flag to enabled
100102
update_object_value 'web' 'DOMAIN' "$domain" '$FASTCGI_CACHE' 'yes'
101103

102104
# Restart web server
103105
$BIN/v-restart-web
104-
check_result $? "Web restart failed" > /dev/null
106+
check_result $? "Web server restart failed" > /dev/null
105107

106108
# Logging
107-
log_history "enabled fast cgi support for $domain"
109+
log_history "Enabled FastCGI cache for $domain"
108110
log_event "$OK" "$ARGUMENTS"
109111

110112
exit
Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#!/bin/bash
2-
# info: remove fast cgi nginx support
2+
# info: Remove FastCGI nginx support
33
# options: USER DOMAIN [RESTART]
44
# labels: hestia web
55
#
6-
# example: v-delete-web-domain-fast-cgi-cache user domain.tld
6+
# example: v-delete-fastcgi-cache user domain.tld
77
#
8-
# The function removes fast cgi cache.
8+
# The function enables FastCGI cache for nginx
9+
910

1011
#----------------------------------------------------------#
1112
# Variable&Function #
@@ -41,16 +42,25 @@ check_hestia_demo_mode
4142
# Load domain data
4243
parse_object_kv_list $(grep "DOMAIN='$domain'" $USER_DATA/web.conf)
4344

44-
# Remove fast cgi configs
45+
# Remove FastCGI cache configuration
4546
if [ -f $HOMEDIR/$user/conf/web/$domain/$WEB_SYSTEM.fastcgi_cache.conf ]; then
46-
rm -f $HOMEDIR/$user/conf/web/$domain/$WEB_SYSTEM.fastcgi_cache.conf
47+
rm -rf $HOMEDIR/$user/conf/web/$domain/$WEB_SYSTEM.fastcgi_cache.conf
48+
fi
49+
50+
conf='/etc/nginx/conf.d/fastcgi_cache_pool.conf'
51+
if [ -f "$conf" ]; then
52+
sed -i "/fastcgi_cache $domain;/d" $conf
53+
if [ ! -s "$conf" ]; then
54+
rm -rf $conf
55+
fi
4756
fi
4857

49-
# Delete cache folder on disabling
50-
if [ -d /var/cache/nginx/php-fpm/$domain ]; then
51-
rm -rf /var/cache/nginx/php-fpm/$domain
58+
# Delete FastCGI cache folder
59+
if [ -d /var/cache/nginx/micro/$domain ]; then
60+
rm -rf /var/cache/nginx/micro/$domain
5261
fi
5362

63+
5464
#----------------------------------------------------------#
5565
# Hestia #
5666
#----------------------------------------------------------#
@@ -62,14 +72,14 @@ fi
6272
# Set FASTCGI flag to disabled
6373
update_object_value 'web' 'DOMAIN' "$domain" '$FASTCGI_CACHE' ''
6474

65-
# Restart services if requested
75+
# Restart web server
6676
if [ ! -z "$restart" ]; then
6777
$BIN/v-restart-web
68-
check_result $? "Web restart failed" >/dev/null
78+
check_result $? "Web server restart failed" > /dev/null
6979
fi
7080

7181
# Logging
72-
log_history "disabled fast cgi support for $domain"
82+
log_history "Disabled FastCGI cache for $domain"
7383
log_event "$OK" "$ARGUMENTS"
7484

7585
exit
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#!/bin/bash
2-
# info: Empty nginx cache
2+
# info: Purge nginx cache
33
# options: USER DOMAIN MODE
44
# labels: hestia web
55
#
6-
# example: v-purge-web-domain-nginx-cache user domain.tld proxy
6+
# example: v-purge-nginx-cache user domain.tld proxy
77
#
8-
# The function clears Nginx cache.
8+
# The function purges nginx cache.
99

1010
#----------------------------------------------------------#
1111
# Variable&Function #
@@ -34,34 +34,35 @@ is_object_valid 'web' 'DOMAIN' "$domain" "$FASTCGI_CACHE"
3434
# Perform verification if read-only mode is enabled
3535
check_hestia_demo_mode
3636

37-
3837
#----------------------------------------------------------#
3938
# Action #
4039
#----------------------------------------------------------#
4140

4241
# Load domain data
4342
parse_object_kv_list $(grep "DOMAIN='$domain'" $USER_DATA/web.conf)
4443

45-
# Empty Fast CGI Cache
46-
if [ -d /var/cache/nginx/php-fpm/$domain ]; then
47-
rm -fr /var/cache/nginx/php-fpm/$domain/*
44+
# Purge nginx FastCGI cache
45+
if [ -d /var/cache/nginx/micro/$domain ]; then
46+
rm -rf /var/cache/nginx/micro/$domain
4847
fi
49-
# Empty Proxy Cache
48+
49+
# Purge nginx proxy cache
5050
if [ -d /var/cache/nginx/$domain ]; then
51-
rm -fr /var/cache/nginx/$domain/*
51+
rm -rf /var/cache/nginx/$domain
5252
fi
53+
5354
#----------------------------------------------------------#
5455
# Hestia #
5556
#----------------------------------------------------------#
5657

5758
# Restart services if requested
5859
if [ ! -z "$restart" ]; then
5960
$BIN/v-restart-web
60-
check_result $? "Web restart failed" >/dev/null
61+
check_result $? "Web restart failed" > /dev/null
6162
fi
6263

6364
# Logging
64-
log_history "purged nginx cache for $domain"
65+
log_history "Purged nginx cache for $domain"
6566
log_event "$OK" "$ARGUMENTS"
6667

67-
exit
68+
exit

install/deb/nginx/nginx.conf

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,21 +123,21 @@ http {
123123
error_page 410 /error/410.html;
124124
error_page 500 501 502 503 504 505 /error/50x.html;
125125

126-
# Cache settings
126+
# Proxy cache
127127
proxy_cache_path /var/cache/nginx levels=2 keys_zone=cache:10m inactive=60m max_size=1024m;
128128
proxy_cache_key "$host$request_uri $cookie_user";
129129
proxy_temp_path /var/cache/nginx/temp;
130130
proxy_ignore_headers Expires Cache-Control;
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;
134+
# FastCGI cache
135+
fastcgi_cache_path /var/cache/nginx/micro levels=1:2 keys_zone=microcache:10m max_size=1024m inactive=30m;
136+
fastcgi_cache_key "$scheme$request_method$host$request_uri";
137+
fastcgi_cache_methods GET HEAD;
138+
fastcgi_cache_use_stale updating error timeout invalid_header http_500 http_503;
139+
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
140+
add_header X-FastCGI-Cache $upstream_cache_status;
141141

142142
# Cache bypass
143143
map $http_cookie $no_cache {

install/upgrade/versions/1.4.0.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
####### Place additional commands below. #######
77
#######################################################################################
88

9-
# Allow Fast CGI Cache to be enabled for Nginx Standalone
9+
# Add support for nginx FastCGI cache (standalone)
1010
if [ -e "/etc/nginx/nginx.conf" ]; then
1111
check=$(cat /etc/nginx/nginx.conf | grep 'fastcgi_cache_path');
1212
if [ -z "$check" ]; then
13-
echo "[ * ] Enabling Nginx FastCGI cache support..."
14-
sed -i 's/# Cache bypass/# FastCGI Cache settings\n fastcgi_cache_path \/var\/cache\/nginx\/php-fpm levels=2\n keys_zone=fcgi_cache:10m inactive=60m max_size=1024m;\n fastcgi_cache_key \"$host$request_uri $cookie_user\";\n fastcgi_temp_path \/var\/cache\/nginx\/temp;\n fastcgi_ignore_headers Expires Cache-Control;\n fastcgi_cache_use_stale error timeout invalid_header;\n fastcgi_cache_valid any 1d;\n\n # Cache bypass/g' /etc/nginx/nginx.conf
13+
echo "[ * ] Enabling nginx FastCGI cache support..."
14+
sed -i 's/# Cache bypass/# FastCGI cache\n fastcgi_cache_path \/var\/cache\/nginx\/micro levels=1:2 keys_zone=microcache:10m max_size=1024m inactive=30m;\n fastcgi_cache_key \"$scheme$request_method$host$request_uri\";\n fastcgi_cache_methods GET HEAD;\n fastcgi_cache_use_stale updating error timeout invalid_header http_500 http_503;\n fastcgi_ignore_headers Cache-Control Expires Set-Cookie;\n add_header X-FastCGI-Cache \$upstream_cache_status;\n\n # Cache bypass/g' /etc/nginx/nginx.conf
1515
fi
1616
fi
1717

18-
# Populating HELO/SMTP Banner for existing ip's
18+
# Populating HELO/SMTP Banner for existing IPs
1919
if [ "$MAIL_SYSTEM" == "exim4" ]; then
2020
source $HESTIA/func/ip.sh
2121

@@ -109,15 +109,15 @@ fi
109109
# New configuration value for enforcing subdomain ownership
110110
check=$(cat $HESTIA/conf/hestia.conf | grep 'ENFORCE_SUBDOMAIN_OWNERSHIP');
111111
if [ -z "$check" ]; then
112-
echo "[ * ] Setting ENFORCE_SUBDOMAIN_OWNERSHIP to no"
112+
echo "[ * ] Setting ENFORCE_SUBDOMAIN_OWNERSHIP to no..."
113113
echo "ENFORCE_SUBDOMAIN_OWNERSHIP='no'" >> $HESTIA/conf/hestia.conf
114114
fi
115115

116116
# New API feature to set allowed IPs
117117
if [ "$api" = "yes" ]; then
118118
check=$(cat $HESTIA/conf/hestia.conf | grep 'API_ALLOWED_IP');
119119
if [ -z "$check" ]; then
120-
echo "[ * ] Setting API_ALLOWED_IP to allow-all"
120+
echo "[ * ] Setting API_ALLOWED_IP to allow-all..."
121121
echo "API_ALLOWED_IP='allow-all'" >> $HESTIA/conf/hestia.conf
122122
fi
123123
else

web/delete/web/cache/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
if (!empty($_GET['domain'])) {
2020
$v_username = escapeshellarg($user);
2121
$v_domain = escapeshellarg($_GET['domain']);
22-
exec (HESTIA_CMD."v-purge-web-domain-nginx-cache ".$v_username." ".$v_domain, $output, $return_var);
22+
exec (HESTIA_CMD."v-purge-nginx-cache ".$v_username." ".$v_domain, $output, $return_var);
2323
check_return_code($return_var,$output);
2424
}
2525
$_SESSION['ok_msg'] = _('Nginx cache has been successfully purged');

web/edit/web/index.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,14 +328,14 @@
328328
unset($output);
329329
}
330330

331-
//Add / Delete caching support
331+
// Enable/Disable nginx cache
332332
if (($_SESSION['WEB_SYSTEM'] == 'nginx') && ($v_nginx_cache != $_POST['v_nginx_cache'] ) && (empty($_SESSION['error_msg']))) {
333333
if ( $_POST['v_nginx_cache'] == 'yes' ) {
334-
exec (HESTIA_CMD."v-add-web-domain-fast-cgi-cache ".$v_username." ".escapeshellarg($v_domain), $output, $return_var);
334+
exec (HESTIA_CMD."v-add-fastcgi-cache ".$v_username." ".escapeshellarg($v_domain), $output, $return_var);
335335
check_return_code($return_var,$output);
336336
unset($output);
337337
} else {
338-
exec (HESTIA_CMD."v-delete-web-domain-fast-cgi-cache ".$v_username." ".escapeshellarg($v_domain), $output, $return_var);
338+
exec (HESTIA_CMD."v-delete-fastcgi-cache ".$v_username." ".escapeshellarg($v_domain), $output, $return_var);
339339
check_return_code($return_var,$output);
340340
unset($output);
341341
}

0 commit comments

Comments
 (0)