server { listen 127.0.0.1:8880; server_name plesk-site-preview.local; location ~ ^/plesk-site-preview/(?P.*?)/(?P.*?)/(?P.*)$ { set $dst_path "/plesk-site-preview/$internal_host/$internal_ip/"; set $public_ip ''; access_by_lua ' local ip_map = io.open("/etc/sw-cp-server/conf.d/ip_mapping.inc") if (ip_map == nil) then ip_map = io.open("/etc/sw-cp-server/conf.d/trusted_ips.inc") end if (ip_map == nil) then ngx.log(ngx.ERR, "Could not find IP map file") ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR) end for ips in ip_map:lines() do local internal_ip, public_ip = ips:match("([^,]*),?([^,]*)") if internal_ip == ngx.var.internal_ip then ngx.var.public_ip = public_ip ~= "" and public_ip or internal_ip ip_map:close() return end end ip_map:close() 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://$public_ip$dst_path; proxy_redirect http://www.$internal_host/ http://$public_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 ""; add_header X-Robots-Tag "noindex,nofollow"; 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.public_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 '; } }