v-add-fastcgi-cache 3.1 KB

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