####################################################################################################
####################################################################################################
####################################################################################################
### 		                  NGINX CONFIGURATION FOR ZEN CART
###
### NOTES: 
###    - Copy and paste into Zen Cart handling ‘server’ section of nginx conf file
###    - Location Block order is important
###    - Include before any other location blocks if any
###
###
####################################################################################################
####################################################################################################
####################################################################################################
	
	
	#############################################################################################
	# 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)\.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;
	}
	
	
