####################################################################################################
####################################################################################################
####################################################################################################
### 		                  NGINX CONFIGURATION FOR ZEN CART
###
### NOTES: 
###    - Copy and paste into Zen Cart handling 'server' section of your site's nginx .conf file
###    - Location Block order is important
###    - Include before any other location blocks, if any.
###
###    - The original version of this template file has 3 placeholders in it, which zc_install will normally replace for you during an install.
###    - However, if you are manually applying this template after-the-fact and these placeholders are still present in the file, you will need to 
###    - replace the following placeholders as shown:
###    - %%store_folder%% - either replace it with blank if your store is NOT in a subdirectory, or use /store_dir (slash followed by dir to store's files)
###    - %%admin_folder%% - either replace it with blank if your store is NOT in a subdirectory, or use /store_dir/admin_dir (slash followed by dir to store's admin dir)
###    - %%slash_folder%% - either replace it with just a slash (/) if your store is NOT in a subdirectory, or use /store_dir/ (slash followed by dir to store's files followed by slash)
###
###    Quick Implementation:
###    1. Find DIR_WS_HTTP_CATALOG value from your /includes/configure.php file.
###       a) %%store_folder%%:
###         If DIR_WS_HTTP_CATALOG is blank, %%store_folder%% is blank; else, %%store_folder%% = "/" followed by the value of DIR_WS_HTTP_CATALOG 
###       
###       b) %%slash_folder%%:
###          If DIR_WS_HTTP_CATALOG is blank, %%slash_folder%% is "/" (without quotes); else, %%slash_folder%% = "/" followed by the value of DIR_WS_HTTP_CATALOG followed by "/" again.
###       
###    2. Find DIR_WS_ADMIN value from your /admin_dir/includes/configure.php file.
###       %%admin_folder%%:
###       %%admin_folder%% = %%store_folder%% (from 1a above) followed by "/", followed by the value of DIR_WS_ADMIN
###       
###
###    Also:
###    - 127.0.0.1:9000 - if PHP's FPM is running on a different port, set that port or socket accordingly.
###
####################################################################################################
####################################################################################################
####################################################################################################
	
	
	#############################################################################################
	# Zen Cart PHP Handler
	#############################################################################################	
	## The following block serves all PHP requests internally directed to it within this conf file
	## Edit to reflect your actual setup for serving PHP
	location @zencart_php_handler {
		if (!-f $document_root$fastcgi_script_name) { return 404; }

		fastcgi_index   index.php;
		fastcgi_pass    127.0.0.1:9000;
		
		fastcgi_param	SCRIPT_FILENAME     $document_root$fastcgi_script_name;
		fastcgi_param	QUERY_STRING        $query_string;
		fastcgi_param	REQUEST_METHOD      $request_method;
		fastcgi_param	CONTENT_TYPE        $content_type;
		fastcgi_param	CONTENT_LENGTH      $content_length;
		fastcgi_param	SCRIPT_NAME         $fastcgi_script_name;
		fastcgi_param	REQUEST_URI         $request_uri;
		fastcgi_param	DOCUMENT_URI        $document_uri;
		fastcgi_param	DOCUMENT_ROOT       $document_root;
		fastcgi_param	SERVER_PROTOCOL     $server_protocol;
		fastcgi_param	GATEWAY_INTERFACE   CGI/1.1;
		fastcgi_param	SERVER_SOFTWARE     nginx/$nginx_version;
		fastcgi_param	REMOTE_ADDR         $remote_addr;
		fastcgi_param	REMOTE_PORT         $remote_port;
		fastcgi_param	SERVER_ADDR         $server_addr;
		fastcgi_param	SERVER_PORT         $server_port;
		fastcgi_param	SERVER_NAME         $server_name;
		fastcgi_param	REDIRECT_STATUS     200;
		
		fastcgi_hide_header X-Powered-By;
	}
	
	
	#############################################################################################
	# Zen Cart Pretty URLs Handler
	#############################################################################################	
	## The following block handles Pretty URLs
	## Insert directives below as required
	#
	#
	#
	
	
	#############################################################################################
	# Zen Cart Root Folder
	#############################################################################################	
	## Allow all requests to specific PHP files directly under the Zen Cart ROOT folder
	location ~ %%store_folder%%/(ajax|index|page_not_found|ipn_main_handler|ppr_listener|ppr_webhook)\.php$ {
		error_page 418 = @zencart_php_handler;
		return 418;
	}
	## Allow all requests to specific static file types
	location ~ %%store_folder%%/[^/]+\.(md|ico|txt|html)$ {
		expires $zencart_expires;
	}
	
	#############################################################################################
	# Zen Cart 'includes' Folder
	#############################################################################################	
	## Allow all 'GET' and 'HEAD' requests to specific static file types in 'includes' and/or subfolders
	location ~ %%store_folder%%/includes {
		location ~ %%store_folder%%/includes.+\.(ico|jpe?g|gif|eot|otf|woff2?|ttf|webp|png|swf|flv|pdf|svgz?)$ {
			if ( $request_method !~ ^(GET|HEAD)$ ) { return 403; }
			add_header Cache-Control 'max-age=864000, public, must-revalidate';
			expires $zencart_expires;		
		}
		location ~ %%store_folder%%/includes/.+\.(html|htm|xml|txt|xsl)$ {
			if ( $request_method !~ ^(GET|HEAD)$ ) { return 403; }
			add_header Cache-Control 'max-age=7200, must-revalidate';
			expires $zencart_expires;		
		}
		location ~ %%store_folder%%/includes/.+\.(js|css)$ {
			if ( $request_method !~ ^(GET|HEAD)$ ) { return 403; }
			expires $zencart_expires;		
		}
		# Deny any other request to 'includes' and/or subfolders
		return 403;
	}
	
	
	#############################################################################################
	# Zen Cart 'images' Folder
	#############################################################################################	
	## Allow internal requests to 'images/uploads'
	location ~ %%store_folder%%/images/uploads {
		internal;
	}
	## Allow all requests to specific static file types in 'images' and/or subfolders
	location ~ %%store_folder%%/images/.+\.(jpe?g|gif|webp|png|swf)$ {
		add_header Cache-Control 'max-age=864000, public, must-revalidate';
		expires $zencart_expires;		
	}
	## Deny any other request to 'images' and/or subfolders
	location ~ %%store_folder%%/images {
		return 403;
	}
	
	
	#############################################################################################
	# Zen Cart 'pub' Folder
	#############################################################################################	
	## Allow and force downloads of all requests to specific static file types in 'pub' and/or subfolders
	location ~ %%store_folder%%/pub/.+\.(g?zip|pdf|mp3?4?|swf|wma|wmv|wav|epub|ogg|webm|m4v?a?)$ {
		types { }
		default_type application/octet-stream;
	}
	## Deny any other request to 'pub' and/or subfolders
	location ~ %%store_folder%%/pub {
		return 403;
	}
	
	
	#############################################################################################
	# Zen Cart 'download' Folder
	#############################################################################################	
	## Allow and force downloads of internal requests to specific static file types in 'download' and/or subfolders
	location ~ %%store_folder%%/download/.+\.(mpe?g?3?4?|swf|avi|wma?v?|wav|epub|flv|ogg|webm|m4v?a?|mov|g?zip|rar|t?gz|tar|pdf)$ {
		types { }
		default_type application/octet-stream;
		
		# Comment out the 'internal' line below if you wish to allow direct access to 'download' folder
		internal;
	}
	## Deny any other request to 'download' and/or subfolders
	location ~ %%store_folder%%/download {
		return 403;
	}
	
	
	#############################################################################################
	# Zen Cart 'media' Folder
	#############################################################################################	
	## Allow all requests to specific static file types in 'media' and/or subfolders
	location ~ %%store_folder%%/media/.+\.(mpe?g?3?4?|swf|avi|wma?v?|wav|epub|flv|ogg|webm|m4v?a?|mov|ra?m?)$ {
		add_header Cache-Control 'max-age=864000, public, must-revalidate';
		expires $zencart_expires;		
	}
	## Deny any other request to 'media' and/or subfolders
	location ~ %%store_folder%%/media {
		return 403;
	}
	
	
	#############################################################################################
	# Zen Cart 'extras' Folder
	#############################################################################################	
	## Allow all requests to html files in 'extras' and/or subfolders
	location ~ %%store_folder%%/extras/.+\.html$ {
		expires $zencart_expires;		
	}
	## Allow all requests to all PHP files
	location ~ %%store_folder%%/extras/.+\.php$ {
		error_page 418 = @zencart_php_handler;
		return 418;
	}
	## Deny any other request to 'extras' and/or subfolders
	location ~ %%store_folder%%/extras {
		return 403;
	}
	
	
	#############################################################################################
	# Zen Cart 'email' Folder
	#############################################################################################	
	## Allow all requests to specific static file types in 'email' and/or subfolders
	location ~ %%store_folder%%/email/.+\.(jpe?g|gif|png)$ {
		expires $zencart_expires;		
	}
	## Deny any other request to 'email' and/or subfolders
	location ~ %%store_folder%%/email {
		return 403;
	}
	
	
	#############################################################################################
	# Zen Cart 'editors' and 'docs' Folders
	#############################################################################################	
	## Allow all requests to specific static file types in 'editors' or 'docs' and/or subfolders
	location ~ %%store_folder%%/(editors|docs)/.+\.(js|css|jpe?g|gif|png|html?|xml|pdf)$ {
		expires $zencart_expires;		
	}
	## Deny any other request to 'editors' or 'docs' and/or subfolders
	location ~ %%store_folder%%/(editors|docs) {
		return 403;
	}
	
	
	#############################################################################################
	# Zen Cart ADMIN Folder
	#############################################################################################	
	## Allow all 'GET' and 'HEAD' requests to specific file types
	location ~ %%admin_folder%% {
		## Additionally allow all 'POST' requests to all PHP files in ADMIN folder and/or subfolders
		location ~ %%admin_folder%%/.+\.php$ {
			if ( $request_method !~ ^(GET|HEAD|POST)$ ) { return 403; }
			error_page 418 = @zencart_php_handler;
			return 418;
		}
		location ~ %%admin_folder%%/.+\.(ico|jpe?g|gif|eot|otf|woff2?|ttf|webp|png|swf|flv|pdf|svgz?)$ {
			if ( $request_method !~ ^(GET|HEAD)$ ) { return 403; }
			add_header Cache-Control 'max-age=864000, public, must-revalidate';
			expires $zencart_expires;		
		}
		location ~ %%admin_folder%%/.+\.(html|htm|xml|txt|xsl)$ {
			if ( $request_method !~ ^(GET|HEAD)$ ) { return 403; }
			add_header Cache-Control 'max-age=7200, must-revalidate';		
			expires $zencart_expires;		
		}
		location ~ %%admin_folder%%/.+\.(js|css)$ {
			if ( $request_method !~ ^(GET|HEAD)$ ) { return 403; }
			expires $zencart_expires;		
		}
		# Redirect all requests to ADMIN root to ADMIN/index.php 
		location ~ %%admin_folder%%(/?)$ {
			if ( $request_method !~ ^(GET|HEAD)$ ) { return 403; }
			rewrite ^ %%admin_folder%%/index.php? redirect;
		}
		# Deny any other request to ADMIN folder and/or subfolders
		return 403;
	}
	
	
	#############################################################################################
	# Zen Cart 'cache' Folder
	#############################################################################################	
	## Allow internal requests to 'cache' and/or subfolders
	location ~ %%store_folder%%/cache {
		internal;
	}
	
	
	#############################################################################################
	# Zen Cart 'logs' Folder
	#############################################################################################	
	## Allow internal requests to 'logs' and/or subfolders
	location ~ %%store_folder%%/logs {
		internal;
	}
	
	
	#############################################################################################
	# Zen Cart 'zc_install' Folder
	#############################################################################################	
	## Allow all requests to specific static file types in 'zc_install' and/or subfolders
	location ~ %%store_folder%%/zc_install/.+\.(ico|js|css|jpg|gif|png|html)$ {
		expires 5m;	
	}
	## Allow internal requests to PHP files in subdirectories under 'zc_install'
	location ~ %%store_folder%%/zc_install/.+/.+\.php$ {
		internal;
		error_page 418 = @zencart_php_handler;
		return 418;
	}
	## Allow all requests to index.php file directly under 'zc_install'
	location ~ %%store_folder%%/zc_install/index\.php$ {
		error_page 418 = @zencart_php_handler;
		return 418;
	}
	## Allow all requests to PHP files that start with 'ajax' directly under 'zc_install' 
	location ~ %%store_folder%%/zc_install/ajax.+\.php$ {
		error_page 418 = @zencart_php_handler;
		return 418;
	}
	## Allow internal requests to 'zc_install/sql' and/or subfolders
	location ~ %%store_folder%%/zc_install/sql(.*)$ {
		internal;
	}
	## Redirect all requests to 'zc_install' root to 'zc_install/index.php'
	location ~ %%store_folder%%/zc_install(/?)$ {
		rewrite ^ %%store_folder%%/zc_install/index.php? redirect;
	}
	## Deny any other request to 'zc_install' and/or subfolders
	location ~ %%store_folder%%/zc_install {
		return 403;
	}
	
	
	#############################################################################################
	# Zen Cart Catchall
	#############################################################################################	
	## Redirect any request not previously explicitly handled to main Zen Cart index.php file
	location ~ %%slash_folder%% {
		rewrite ^ %%store_folder%%/index.php? redirect;
	}
	
	
