limit_req_zone $binary_remote_addr zone=catalogapi:10m rate=3r/s;
limit_req_zone $binary_remote_addr zone=characterapi:10m rate=3r/s;

# Automatic HTTPS Redirect
# (cloudflare's encryption mode MUST be set to Full for this to work properly)
server 
{
	listen 80;
	server_name polygoncdn.pizzaboxer.xyz;
	return 301 https://$host$request_uri;
}

server 
{
	listen 80;
	listen 443 ssl http2;
	server_name chef.pizzaboxer.xyz polygon.pizzaboxer.xyz polygon.local pizzaboxer.xyz www.pizzaboxer.xyz;
	#server_name polygon.local;
	
	add_header imagien-if "nickster v was bibster v";
	add_header X-UA-Compatible "IE=Edge" always;

	ssl_certificate ssl/pizzaboxer.xyz.pem;
	ssl_certificate_key ssl/pizzaboxer.xyz.key;

	root /var/www/pizzaboxer.xyz/polygon;
	
	error_page 403 =404 /error.php?code=404;
	error_page 404 /error.php?code=404;

	index index.php index.html index.htm;
    
    ########################################
    ########### Client Rewrites ############
    ########################################
        
    rewrite (?i)^/Landing.aspx$                       /games redirect;
        
    rewrite (?i)^/Asset$                              /rbxclient/asset/fetch.php last;
    rewrite (?i)^/Asset/$                             /rbxclient/asset/fetch.php last;
    rewrite (?i)^/Asset/Default.ashx$                 /rbxclient/asset/fetch.php last;
    rewrite (?i)^/Asset/CharacterFetch.ashx$          /rbxclient/asset/characterfetch.php last;
    rewrite (?i)^/Asset/BodyColors.ashx$              /rbxclient/asset/bodycolors.php last;
    rewrite (?i)^/Asset/GetScriptState.ashx$          /rbxclient/asset/getscriptstate.html last;

    rewrite (?i)^/Experience/(.*)$                    /Game/$1;

    rewrite (?i)^/Thumbs/Avatar.ashx$                 /thumbs/avatar.php last;
    rewrite (?i)^/Thumbs/Asset.ashx$                  /thumbs/asset.php last;

    rewrite (?i)^/Game/PlaceLauncher.ashx$            /api/games/placelauncher.php last;    
    rewrite (?i)^/Game/Studio.ashx$                   /rbxclient/game/studio.php last;
    rewrite (?i)^/Game/Edit.ashx$                     /rbxclient/game/edit.php last;
    rewrite (?i)^/Game/Visit.ashx$                    /rbxclient/game/visit.php last;
    rewrite (?i)^/Game/Join.ashx$                     /rbxclient/game/join.php last;
    rewrite (?i)^/Game/GameServer.ashx$               /rbxclient/game/gameserver.php last;
    rewrite (?i)^/Game/ClientPresence.ashx$           /rbxclient/game/clientpresence.php last;
    rewrite (?i)^/Game/PlaceVisit.ashx$               /rbxclient/game/placevisit.php last;
    rewrite (?i)^/Game/PlaceSpecificScript.ashx$      /rbxclient/game/placespecificscript.php last;
    rewrite (?i)^/Game/LoadPlaceInfo.ashx$            /rbxclient/game/loadplaceinfo.php last;
    rewrite (?i)^/Game/Help.aspx$                     /rbxclient/game/help.php last;
    rewrite (?i)^/Game/KeepAlivePinger.ashx$          /rbxclient/game/keepalivepinger.php last;
    rewrite (?i)^/Game/MachineConfiguration.ashx$     /rbxclient/game/machineconfiguration.php last;
    rewrite (?i)^/Game/Knockouts.ashx$                /rbxclient/game/knockouts.php last;
    rewrite (?i)^/Game/Wipeouts.ashx$                 /rbxclient/game/wipeouts.php last;
    rewrite (?i)^/Game/GetAuthTicket$                 /rbxclient/game/getauthticket.php last;

    rewrite (?i)^/Game/Tools/ThumbnailAsset.ashx$     /thumbs/asset.php last;
    rewrite (?i)^/Game/Tools/InsertAsset.ashx$        /rbxclient/game/tools/insertasset.php last;

    rewrite (?i)^/Game/Badge/AwardBadge.ashx$         /rbxclient/game/badge/awardbadge.php last;
    rewrite (?i)^/Game/Badge/HasBadge.ashx$           /rbxclient/game/badge/hasbadge.php last;
    rewrite (?i)^/Game/Badge/IsBadgeDisabled.ashx$    /rbxclient/game/badge/isbadgedisabled.php last;

    rewrite (?i)^/Game/GamePass/GamePassHandler.ashx$ /rbxclient/game/gamepass/gamepasshandler.php last;
    rewrite (?i)^/Game/LuaWebService/HandleSocialRequest.ashx$ /rbxclient/game/luawebservice/handlesocialrequest.php last;

    rewrite (?i)^/Game/(.*)$                          /game/$1;

    rewrite (?i)^/Friend/CreateFriend$                /rbxclient/friend/createfriend.php last;
    rewrite (?i)^/Friend/BreakFriend$                 /rbxclient/friend/breakfriend.php last;
    rewrite (?i)^/Friend/AreFriends$                  /rbxclient/friend/arefriends.php last;

    rewrite (?i)^/Analytics/GamePerfMonitor.ashx$     /rbxclient/analytics/gameperfmonitor.php last;
    rewrite (?i)^/Analytics/Measurement.ashx$         /rbxclient/analytics/measurement.php last;

    rewrite (?i)^/Login/Negotiate.ashx$               /rbxclient/login/negotiate.php last;
        
    rewrite (?i)^/Build/$                             /develop redirect;
    rewrite (?i)^/Build/Default.aspx$                 /develop redirect;

    rewrite (?i)^/IDE/Landing.aspx$                   /ide/welcome redirect;
    rewrite (?i)^/IDE/ClientToolbox.aspx$             /rbxclient/studio/toolbox.php last;
    rewrite (?i)^/IDE/Upload.aspx$                    /rbxclient/studio/publish-place.php last;
    rewrite (?i)^/IDE/Welcome$                        /rbxclient/studio/welcome.php last;
    rewrite (?i)^/UI/Save.aspx$                       /rbxclient/studio/publish-model.php last;
        
    rewrite (?i)^/UploadMedia/PostImage.aspx$         /rbxclient/uploadmedia/screenshot.php last;
    rewrite (?i)^/UploadMedia/UploadVideo.aspx$       /rbxclient/uploadmedia/video.php last;
        
    rewrite (?i)^/Error/Dmp.ashx$                     /rbxclient/error/dump.php;
        
    ########################################
    ########### Website Rewrites ###########
    ########################################
        
    rewrite ^/(.*)-item(.*)$                 /item$2;
    rewrite ^/(.*)-place(.*)$                /placeitem$2;

    rewrite ^/forum/$                        /forum.php last;
    rewrite ^/admin/$                        /admin.php last;

    rewrite ^/login/(.*)$                    /directory_login/$1.php last;
    rewrite ^/games/(.*)$                    /directory_games/$1.php last;
    rewrite ^/forum/(.*)$                    /directory_forum/$1.php last;
    rewrite ^/admin/(.*)$                    /directory_admin/$1.php last;

    rewrite /places/create$                  /directory_places/create.php last;
    rewrite /places/(.*)/update$             /directory_places/update.php?PlaceID=$1 last;

    rewrite /asset-thumbnail-3d/json$        /thumbs/asset3d.php last;
    rewrite /avatar-thumbnail-3d/json$       /thumbs/avatar3d.php last;
    rewrite /thumbnail/resolve-hash/(.*)$    /thumbs/resolvehash.php?filename=$1 last;

    location / 
	{ 
		try_files $uri $uri/ @extensionless-php; 
	}

    location /api/catalog
    {
    	limit_req zone=catalogapi;
		try_files $uri $uri/ @extensionless-php; 
    }

    location /api/character
    {
    	limit_req zone=characterapi;
		try_files $uri $uri/ @extensionless-php; 
    }

    location ~ /api/private 
    {
        deny all;
        return 404;
    }

    location ~ /\. 
    {
		deny all;
        return 404;
    }

    location @extensionless-php 
	{ 
		rewrite ^(.*)$ $1.php last; 
	}

	location ~ \.php$ 
	{
		include snippets/fastcgi-php.conf;
	}
}

server
{
	listen 80;
	listen 443 ssl http2;
	server_name stub;
	#server_name polygon.pizzaboxer.xyz;
	
	add_header imagien-if "nickster v was bibster v";
	add_header X-UA-Compatible "IE=Edge" always;

	ssl_certificate ssl/pizzaboxer.xyz.pem;
	ssl_certificate_key ssl/pizzaboxer.xyz.key;

	root /var/www/pizzaboxer.xyz/polygonmaintenance;
	
	error_page 403 =404 index.html;
	error_page 404 index.html;

	index index.html;
}

server 
{
	listen 80;
	listen 443 ssl http2;
	server_name polygondev.pizzaboxer.xyz vednogylop.pizzaboxer.xyz dev.polygon.local;
	
	add_header X-UA-Compatible "IE=Edge" always;

	ssl_certificate ssl/pizzaboxer.xyz.pem;
	ssl_certificate_key ssl/pizzaboxer.xyz.key;

	root /var/www/pizzaboxer.xyz/polygondev;
	
	error_page 403 =404 /error.php?code=404;
	error_page 404 /error.php?code=404;

	index index.php index.html index.htm;
    	
    ########################################
    ########### Client Rewrites ############
    ########################################
        
    rewrite (?i)^/Landing.aspx$                       /games redirect;
        
    rewrite (?i)^/Asset$                              /rbxclient/asset/fetch.php last;
    rewrite (?i)^/Asset/$                             /rbxclient/asset/fetch.php last;
    rewrite (?i)^/Asset/Default.ashx$                 /rbxclient/asset/fetch.php last;
    rewrite (?i)^/Asset/CharacterFetch.ashx$          /rbxclient/asset/characterfetch.php last;
    rewrite (?i)^/Asset/BodyColors.ashx$              /rbxclient/asset/bodycolors.php last;
    rewrite (?i)^/Asset/GetScriptState.ashx$          /rbxclient/asset/getscriptstate.html last;

    rewrite (?i)^/Experience/(.*)$                    /Game/$1;

    rewrite (?i)^/Thumbs/Avatar.ashx$                 /thumbs/avatar.php last;
    rewrite (?i)^/Thumbs/Asset.ashx$                  /thumbs/asset.php last;

    rewrite (?i)^/Game/PlaceLauncher.ashx$            /api/games/placelauncher.php last;    
    rewrite (?i)^/Game/Studio.ashx$                   /rbxclient/game/studio.php last;
    rewrite (?i)^/Game/Edit.ashx$                     /rbxclient/game/edit.php last;
    rewrite (?i)^/Game/Visit.ashx$                    /rbxclient/game/visit.php last;
    rewrite (?i)^/Game/Join.ashx$                     /rbxclient/game/join.php last;
    rewrite (?i)^/Game/GameServer.ashx$               /rbxclient/game/gameserver.php last;
    rewrite (?i)^/Game/ClientPresence.ashx$           /rbxclient/game/clientpresence.php last;
    rewrite (?i)^/Game/PlaceVisit.ashx$               /rbxclient/game/placevisit.php last;
    rewrite (?i)^/Game/PlaceSpecificScript.ashx$      /rbxclient/game/placespecificscript.php last;
    rewrite (?i)^/Game/LoadPlaceInfo.ashx$            /rbxclient/game/loadplaceinfo.php last;
    rewrite (?i)^/Game/Help.aspx$                     /rbxclient/game/help.php last;
    rewrite (?i)^/Game/KeepAlivePinger.ashx$          /rbxclient/game/keepalivepinger.php last;
    rewrite (?i)^/Game/MachineConfiguration.ashx$     /rbxclient/game/machineconfiguration.php last;
    rewrite (?i)^/Game/Knockouts.ashx$                /rbxclient/game/knockouts.php last;
    rewrite (?i)^/Game/Wipeouts.ashx$                 /rbxclient/game/wipeouts.php last;
    rewrite (?i)^/Game/GetAuthTicket$                 /rbxclient/game/getauthticket.php last;

    rewrite (?i)^/Game/Tools/ThumbnailAsset.ashx$     /thumbs/asset.php last;
    rewrite (?i)^/Game/Tools/InsertAsset.ashx$        /rbxclient/game/tools/insertasset.php last;

    rewrite (?i)^/Game/Badge/AwardBadge.ashx$         /rbxclient/game/badge/awardbadge.php last;
    rewrite (?i)^/Game/Badge/HasBadge.ashx$           /rbxclient/game/badge/hasbadge.php last;
    rewrite (?i)^/Game/Badge/IsBadgeDisabled.ashx$    /rbxclient/game/badge/isbadgedisabled.php last;

    rewrite (?i)^/Game/GamePass/GamePassHandler.ashx$ /rbxclient/game/gamepass/gamepasshandler.php last;
    rewrite (?i)^/Game/LuaWebService/HandleSocialRequest.ashx$ /rbxclient/game/luawebservice/handlesocialrequest.php last;

    rewrite (?i)^/Game/(.*)$                          /game/$1;

    rewrite (?i)^/Friend/CreateFriend$                /rbxclient/friend/createfriend.php last;
    rewrite (?i)^/Friend/BreakFriend$                 /rbxclient/friend/breakfriend.php last;
    rewrite (?i)^/Friend/AreFriends$                  /rbxclient/friend/arefriends.php last;

    rewrite (?i)^/Analytics/GamePerfMonitor.ashx$     /rbxclient/analytics/gameperfmonitor.php last;
    rewrite (?i)^/Analytics/Measurement.ashx$         /rbxclient/analytics/measurement.php last;

    rewrite (?i)^/Login/Negotiate.ashx$               /rbxclient/login/negotiate.php last;
        
    rewrite (?i)^/Build/$				              /develop redirect;
    rewrite (?i)^/Build/Default.aspx$				  /develop redirect;

    rewrite (?i)^/IDE/Landing.aspx$                   /ide/welcome redirect;
    rewrite (?i)^/IDE/ClientToolbox.aspx$             /rbxclient/studio/toolbox.php last;
    rewrite (?i)^/IDE/Upload.aspx$                    /rbxclient/studio/publish-place.php last;
    rewrite (?i)^/IDE/Welcome$                        /rbxclient/studio/welcome.php last;
    rewrite (?i)^/UI/Save.aspx$                       /rbxclient/studio/publish-model.php last;
        
    rewrite (?i)^/UploadMedia/PostImage.aspx$         /rbxclient/uploadmedia/screenshot.php last;
    rewrite (?i)^/UploadMedia/UploadVideo.aspx$       /rbxclient/uploadmedia/video.php last;
        
    rewrite (?i)^/Error/Dmp.ashx$                     /rbxclient/error/dump.php;
        
    ########################################
    ########### Website Rewrites ###########
    ########################################
    	
    rewrite ^/(.*)-item(.*)$                 /item$2;
    rewrite ^/(.*)-place(.*)$                /placeitem$2;

    rewrite ^/forum/$						 /forum.php last;
    rewrite ^/admin/$						 /admin.php last;

    rewrite ^/login/(.*)$                    /directory_login/$1.php last;
    rewrite ^/games/(.*)$                    /directory_games/$1.php last;
    rewrite ^/forum/(.*)$                    /directory_forum/$1.php last;
    rewrite ^/admin/(.*)$                    /directory_admin/$1.php last;

    rewrite /places/create$                  /directory_places/create.php last;
    rewrite /places/(.*)/update$             /directory_places/update.php?PlaceID=$1 last;

    rewrite /asset-thumbnail-3d/json$        /thumbs/asset3d.php last;
    rewrite /avatar-thumbnail-3d/json$       /thumbs/avatar3d.php last;
    rewrite /thumbnail/resolve-hash/(.*)$    /thumbs/resolvehash.php?filename=$1 last;

    location / 
	{ 
		try_files $uri $uri/ @extensionless-php; 
	}

    location /api/catalog
    {
    	limit_req zone=catalogapi;
		try_files $uri $uri/ @extensionless-php; 
    }

    location /api/character
    {
    	limit_req zone=characterapi;
		try_files $uri $uri/ @extensionless-php; 
    }

    location ~ /api/private 
    {
        deny all;
        return 404;
    }

    location ~ /\. 
    {
		deny all;
        return 404;
    }

    location @extensionless-php 
	{ 
		rewrite ^(.*)$ $1.php last; 
	}

	location ~ \.php$ 
	{
		include snippets/fastcgi-php.conf;
	}
}

server 
{
	listen 443 ssl http2;
	server_name polygonapi.pizzaboxer.xyz clientsettingsapi.pizzaboxer.xyz;

	ssl_certificate ssl/pizzaboxer.xyz.pem;
	ssl_certificate_key ssl/pizzaboxer.xyz.key;

	root /var/www/pizzaboxer.xyz/polygonapi;

	error_page 403 =404 /404.php;
	error_page 404 /404.php;

	index index.php index.html index.htm;

    rewrite (?i)^/currency/balance /api/currency/balance.php last;
    rewrite (?i)^/marketplace/productinfo /api/marketplace/productinfo.php last;
    rewrite (?i)^/ownership/hasasset /api/ownership/hasasset.php last;
    rewrite (?i)^/users/([0-9]+) /api/users/info.php?userId=$1 last;
    rewrite (?i)^/users/get-by-username /api/users/info.php last;

    location ~ /private 
    {
        deny all;
        return 404;
    }

	location ~ ^/(status|ping)$
	{
		include snippets/fastcgi-php.conf;
	}

	location ~ \.php$ 
	{
		include snippets/fastcgi-php.conf;
	}
}

server 
{
	listen 443 ssl http2;
	server_name polygoncdn.pizzaboxer.xyz;

	add_header Access-Control-Allow-Origin "*";
	add_header Access-Control-Allow-Headers "x-polygon-csrf";

	ssl_certificate ssl/pizzaboxer.xyz.pem;
	ssl_certificate_key ssl/pizzaboxer.xyz.key;

	error_page  405 =200 $uri;

	root /var/www/pizzaboxer.xyz/polygoncdn;
}

server 
{
	listen 80;
	listen 443 ssl http2;
	server_name ~^setup(?<year>.+)\.pizzaboxer\.xyz;
	
	ssl_certificate ssl/pizzaboxer.xyz.pem;
	ssl_certificate_key ssl/pizzaboxer.xyz.key;

	root /var/www/pizzaboxer.xyz/setup$year;

	rewrite /version-(\w+)-(.*)$ /version-$1/$2 last;
}
