server {
    server_name loba.kupidonia.ru;

    listen 127.0.0.1:8081;
    charset utf-8;
	
	http2 on;

    gzip on;
    gzip_proxied expired no-cache no-store private auth;
    gzip_types text/css text/xml application/javascript text/plain application/json image/svg+xml image/x-icon;
    gzip_comp_level 1;

    set $root_path /var/www/kupidonia/data/www/loba.kupidonia.ru;
    root $root_path;
    disable_symlinks if_not_owner from=$root_path;
	
	#Блокировка выполнения скриптов в папках не под ревизией git =================================
	
	#/logs +
	#/content +
	#/files +
	#/fairy/cache +
	#/cache +
	#/moduls/speech/files +
	#/rss +
	#/tmp +
	#/crumbs +
	#/sitemap +
	#/nbproject +
	#/moduls/RSS/feeds +
	#/maria-test/ +
	#/fairy/services/pinterest/js/pin_editor/node_modules +
	#/core/service_scripts/files + 
	#/core/plugins/SxGeo/bd +
	#/core/plugins/SypexDumper/backup +
	#/services/teacher-office/js/node_modules +
	#/yatuchka/logs + 
	
	location ~* ^/(/yatuchka/logs|/services/teacher-office/js/node_modules|/core/service_scripts/files|/core/plugins/SxGeo/bd|/core/plugins/SypexDumper/backup|/fairy/services/pinterest/js/pin_editor/node_modules|tmp|nbproject|sitemap|/moduls/RSS/feeds|crumbs|files|content|logs|fairy/cache|cache|rss|moduls/speech/files|maria-test)/.*\.php$ {
		return 403;
	}
	
	#Блокировка ботов =================================
	
	if ($block_facebook) {
		return 444;
	}
	
	# Блокировка ботов с дзен
	# Задаем переменную, которая по умолчанию 0
	set $block_dzen 0;

	# Если реферер Дзен, ставим 1
	if ($http_referer ~* "dzen\.ru") {
		set $block_dzen 1;
	}

	# Если это разрешенные страницы, сбрасываем обратно на 0
	if ($request_uri ~* "(/otvety-|/ezhednevnyj-test|for-zen|for-dzen)") {
		set $block_dzen 0;
	}

	# Если это статика (картинки, стили), тоже сбрасываем на 0
	if ($request_uri ~* "\.(css|js|jpg|jpeg|png|gif|webp|svg|ico|woff|woff2)$") {
		set $block_dzen 0;
	}

	# Если в итоге осталась 1 — отдаем 444
	if ($block_dzen = 1) {
		return 444;
	}
	
	#=================================

    location / {
        index index.php;
		try_files $uri $uri/ @handler;
			location ~ [^/]\.ph(p\d*|tml)$ {
				try_files /does_not_exists @php;
			}
    }

    location ^~ /content/pdf/textbooks/ {
        auth_basic            "Restricted";
        auth_basic_user_file  /var/www/kupidonia/data/www/loba.kupidonia.ru/content/pdf/password/.htpasswd;
    }
	
	location @handler {
		rewrite ^(.*) /index.php?action=$1 last;
	}
	
	location ^~ /files/ {
		location ~ \.(?:jpg|png|gif)$ {}
		return 403; # or 404
	}
	
	location @php {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/var/run/loba.kupidonia.ru.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
		
		# Решение проблемы с FastCGI sent in stderr: "the log buffer is full (1024)
		fastcgi_buffers 16 16k;
		fastcgi_buffer_size 32k;
		fastcgi_busy_buffers_size 64k;
		fastcgi_connect_timeout 60s;
		fastcgi_send_timeout 300s;
		fastcgi_read_timeout 300s;
		
		# Кэширование PHP
        fastcgi_cache PHP_CACHE;
        fastcgi_cache_valid 200 301 302 10m;
        fastcgi_cache_valid 404 1m;
        fastcgi_cache_methods GET HEAD;
        fastcgi_cache_revalidate on;
		
		# Автоматическое очищение кэша при изменении
		fastcgi_cache_bypass $http_pragma $http_cache_purge;
		fastcgi_no_cache $http_cache_purge;
		
        add_header X-Cache $upstream_cache_status;
	}

    location @fallback {
        fastcgi_pass unix:/var/run/loba.kupidonia.ru.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
    }

    include "/etc/nginx/fastpanel2-sites/kupidonia/kupidonia.ru.includes";
    include /etc/nginx/fastpanel2-includes/*.conf;

    error_log /var/www/kupidonia/data/logs/kupidonia.ru-frontend.error.log;
    access_log /var/www/kupidonia/data/logs/kupidonia.ru-frontend.access.log;
}