v-add-fastcgi-cache 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/bin/bash
  2. # info: Enable FastCGI cache for nginx
  3. # options: USER DOMAIN [DURATION] [RESTART]
  4. #
  5. # example: v-add-fastcgi-cache user domain.tld 30m
  6. #
  7. # This function enables FastCGI cache for nginx
  8. # Acceptable values for duration is time in seconds (10s) minutes (10m) or days (10d)
  9. # Add "yes" as last parameter to restart nginx
  10. #----------------------------------------------------------#
  11. # Variables & Functions #
  12. #----------------------------------------------------------#
  13. # Argument definition
  14. user=$1
  15. domain=$2
  16. duration=${3-2m}
  17. restart=${4-no}
  18. # Includes
  19. # shellcheck source=/etc/hestiacp/hestia.conf
  20. source /etc/hestiacp/hestia.conf
  21. # shellcheck source=/usr/local/hestia/func/main.sh
  22. source $HESTIA/func/main.sh
  23. # load config file
  24. source_conf "$HESTIA/conf/hestia.conf"
  25. #----------------------------------------------------------#
  26. # Verifications #
  27. #----------------------------------------------------------#
  28. check_args '2' "$#" 'USER DOMAIN [DURATION] [RESTART]'
  29. is_format_valid 'user' 'domain' 'restart'
  30. is_object_valid 'user' 'USER' "$user"
  31. is_object_unsuspended 'user' 'USER' "$user"
  32. is_object_valid 'web' 'DOMAIN' "$domain"
  33. is_object_unsuspended 'web' 'DOMAIN' "$domain"
  34. if ! [[ "$duration" =~ ^[0-9].*[s|m|d]$ ]]; then
  35. echo "Invalid duration";
  36. exit 2;
  37. fi
  38. if [[ "$duration" =~ ^[0].*[s|m|d]$ ]]; then
  39. echo "Invalid duration";
  40. exit 2;
  41. fi
  42. # Perform verification if read-only mode is enabled
  43. check_hestia_demo_mode
  44. #----------------------------------------------------------#
  45. # Action #
  46. #----------------------------------------------------------#
  47. # Load domain data
  48. parse_object_kv_list $(grep "DOMAIN='$domain'" $USER_DATA/web.conf)
  49. # Check that nginx is not in proxy mode
  50. if [ "$WEB_SYSTEM" != 'nginx' ]; then
  51. echo "Error: nginx is in proxy mode"
  52. exit "$E_NOTEXIST"
  53. fi
  54. fastcgi="$HOMEDIR/$user/conf/web/$domain/$WEB_SYSTEM.fastcgi_cache.conf"
  55. no_cache='$no_cache'
  56. cat << EOF > $fastcgi
  57. fastcgi_cache $domain;
  58. fastcgi_cache_valid 200 $duration;
  59. fastcgi_cache_valid 301 302 10m;
  60. fastcgi_cache_valid 404 10m;
  61. fastcgi_cache_bypass $no_cache;
  62. fastcgi_no_cache $no_cache;
  63. set $no_cache 0;
  64. EOF
  65. chown root:$user $fastcgi
  66. chmod 640 $fastcgi
  67. str="fastcgi_cache_path /var/cache/nginx/micro/$domain levels=1:2"
  68. str="$str keys_zone=$domain:10m max_size=512m inactive=30m;"
  69. conf='/etc/nginx/conf.d/fastcgi_cache_pool.conf'
  70. if [ -f "$conf" ]; then
  71. if [ -z "$(grep "=${domain}:" $conf)" ]; then
  72. echo "$str" >> $conf
  73. fi
  74. else
  75. echo "$str" >> $conf
  76. fi
  77. mkdir -p /var/cache/nginx/micro/$domain
  78. #----------------------------------------------------------#
  79. # Hestia #
  80. #----------------------------------------------------------#
  81. if [ -z "$FASTCGI_CACHE" ]; then
  82. add_object_key "web" 'DOMAIN' "$domain" 'FASTCGI_CACHE' 'ALIAS'
  83. fi
  84. if [ -z "$FASTCGI_DURATION" ]; then
  85. add_object_key "web" 'DOMAIN' "$domain" 'FASTCGI_DURATION' 'ALIAS'
  86. fi
  87. # Set FastCGI cache flag to enabled
  88. update_object_value 'web' 'DOMAIN' "$domain" '$FASTCGI_CACHE' 'yes'
  89. update_object_value 'web' 'DOMAIN' "$domain" '$FASTCGI_DURATION' "$duration"
  90. # Restart web server
  91. $BIN/v-restart-web "$restart"
  92. check_result $? "Web server restart failed" > /dev/null
  93. # Logging
  94. $BIN/v-log-action "$user" "Info" "Web" "FastCGI cache enabled (Domain: $domain)."
  95. log_event "$OK" "$ARGUMENTS"
  96. exit