v-add-fastcgi-cache 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/bin/bash
  2. # info: Enable FastCGI cache for nginx
  3. # options: USER DOMAIN [DURATION] [DEBUG] [RESTART]
  4. # labels: hestia web
  5. #
  6. # example: v-add-fastcgi-cache user domain.tld 30m
  7. #
  8. # The function enables FastCGI cache for nginx
  9. # Acceptable values for duration is time in seconds (10s) minutes (10m) or days (10d)
  10. # Add "yes" as last parameter to append debug information to response headers
  11. #----------------------------------------------------------#
  12. # Variable&Function #
  13. #----------------------------------------------------------#
  14. # Argument definition
  15. user=$1
  16. domain=$2
  17. duration=${3-30m}
  18. debug=${4-no}
  19. restart=${5-no}
  20. # Includes
  21. # shellcheck source=/usr/local/hestia/func/main.sh
  22. source $HESTIA/func/main.sh
  23. # shellcheck source=/usr/local/hestia/conf/hestia.conf
  24. source $HESTIA/conf/hestia.conf
  25. #----------------------------------------------------------#
  26. # Verifications #
  27. #----------------------------------------------------------#
  28. check_args '2' "$#" 'USER DOMAIN DEBUG'
  29. is_format_valid 'user' 'domain'
  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. # Perform verification if read-only mode is enabled
  39. check_hestia_demo_mode
  40. #----------------------------------------------------------#
  41. # Action #
  42. #----------------------------------------------------------#
  43. # Load domain data
  44. parse_object_kv_list $(grep "DOMAIN='$domain'" $USER_DATA/web.conf)
  45. # Check that nginx is not in proxy mode
  46. if [ "$WEB_SYSTEM" != 'nginx' ]; then
  47. echo "Error: nginx is in proxy mode"
  48. exit $E_NOTEXIST
  49. fi
  50. if ! grep --quiet "forcessl" $HESTIA/data/templates/web/nginx/default.tpl; then
  51. $BIN/v-update-web-templates
  52. fi
  53. fastcgi="$HOMEDIR/$user/conf/web/$domain/$WEB_SYSTEM.fastcgi_cache.conf"
  54. no_cache='$no_cache'
  55. cookie_session='$cookie_session'
  56. http_x_update='$http_x_update'
  57. status='$upstream_cache_status'
  58. cat << EOF > $fastcgi
  59. fastcgi_cache $domain;
  60. fastcgi_cache_valid 200 2m;
  61. fastcgi_cache_valid 301 302 10m;
  62. fastcgi_cache_valid 404 10m;
  63. fastcgi_cache_bypass $no_cache;
  64. fastcgi_no_cache $no_cache;
  65. EOF
  66. if [ ! -z "$debug" ]; then
  67. echo " add_header \"X-STATUS\" \"$status\";" >> $fastcgi
  68. fi
  69. chown root:$user $fastcgi
  70. chmod 640 $fastcgi
  71. str="fastcgi_cache_path /var/cache/nginx/micro/$domain levels=1:2"
  72. str="$str keys_zone=$domain:10m max_size=512m inactive=30m;"
  73. conf='/etc/nginx/conf.d/fastcgi_cache_pool.conf'
  74. if [ -f "$conf" ]; then
  75. if [ -z "$(grep "=${domain}:" $conf)" ]; then
  76. echo "$str" >> $conf
  77. fi
  78. else
  79. echo "$str" >> $conf
  80. fi
  81. mkdir -p /var/cache/nginx/micro/$domain
  82. #----------------------------------------------------------#
  83. # Hestia #
  84. #----------------------------------------------------------#
  85. if [ -z "$FASTCGI_CACHE" ]; then
  86. add_object_key "web" 'DOMAIN' "$domain" 'FASTCGI_CACHE' 'ALIAS'
  87. fi
  88. if [ -z "$FASTCGI_DURATION" ]; then
  89. add_object_key "web" 'DOMAIN' "$domain" 'FASTCGI_DURATION' 'ALIAS'
  90. fi
  91. # Set FastCGI cache flag to enabled
  92. update_object_value 'web' 'DOMAIN' "$domain" '$FASTCGI_CACHE' 'yes'
  93. update_object_value 'web' 'DOMAIN' "$domain" 'FASTCGI_DURATION' "$duration"
  94. if [ "$restart" = "yes" ]; then
  95. # Restart web server
  96. $BIN/v-restart-web
  97. check_result $? "Web server restart failed" > /dev/null
  98. fi
  99. # Logging
  100. log_history "Enabled FastCGI cache for $domain"
  101. log_event "$OK" "$ARGUMENTS"
  102. exit