server {
		listen 127.0.0.1:8880;
		server_name plesk-site-preview.local;

		location ~ ^/plesk-site-preview/(?P<internal_host>.*?)/(?P<internal_ip>.*?)/(?P<internal_uri>.*)$ {
			set $dst_path "/plesk-site-preview/$internal_host/$internal_ip/";
			access_by_lua '
				for trusted_ip in io.lines("/etc/sw-cp-server/conf.d/trusted_ips.inc") do
					if trusted_ip == ngx.var.internal_ip then
						return
					end
				end
				ngx.log(ngx.ERR, "Forbidden for IP: " .. ngx.var.internal_ip)
				ngx.exit(ngx.HTTP_FORBIDDEN)
			';
			rewrite ^/(.*)$ /$internal_uri break;
			proxy_pass http://$internal_ip;
			proxy_redirect http://$internal_host/ http://$internal_ip$dst_path;
			proxy_redirect http://www.$internal_host/ http://$internal_ip/plesk-site-preview/www.$internal_host/$internal_ip/;
			proxy_cookie_domain ~^.*$ $host;
			proxy_cookie_path / $dst_path;
			proxy_set_header Host $internal_host;
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_set_header Accept-Encoding "";
			header_filter_by_lua 'ngx.header.content_length = nil';
			body_filter_by_lua '
				local response_body = ngx.arg[1]
				local src_address = "https?://" .. ngx.var.internal_host:gsub("[%-%.%+%[%]%(%)%$%^%%%?%*]", "%%%1") .. "/?"
				local dst_address = "http://" .. ngx.var.internal_ip .. ngx.var.dst_path
				response_body = response_body:gsub(src_address, dst_address)
				response_body = response_body:gsub("(src%s*=%s*[\\"\']?)/([^/])", "%1" .. ngx.var.dst_path .. "%2")
				response_body = response_body:gsub("(href%s*=%s*[\\"\']?)/([^/])", "%1" .. ngx.var.dst_path .. "%2")
				response_body = response_body:gsub("(action%s*=%s*[\\"\']?)/([^/])", "%1" .. ngx.var.dst_path .. "%2")
				ngx.arg[1] = response_body
			';
		}
}