v-add-fastcgi-cache 3.0 KB

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