Serve Ploy Behind Your Own Proxy

Hosting

Last updated

Keep your own proxy, such as a Cloudflare Worker, Vercel rewrites or Nginx, in front of your domain and fetch some paths from your Ploy site.

For most sites, the recommended setup is the other way around: point your domain at Ploy and send everything else to your existing site with routing rules. Use this page only when your own proxy has to stay in front.


Fetch from your site's *.ploy.build address

Have your proxy fetch Ploy pages from your site's *.ploy.build address, such as https://your-site.ploy.build.

Do not fetch through a custom domain connected to Ploy. Ploy then treats your proxy as a bot, so A/B tests enroll no one and analytics events are dropped.


Forward these paths

Your Ploy pages load files and link to other pages from the root of your domain, not from under their own path. Keep the same paths on both addresses, such as /guides on your public domain and on your *.ploy.build address. Forward all of these:

PathWhat it serves
Your page paths, such as /guidesThe pages built in Ploy
/_ploy_static/Styles, scripts and the favicon
/_ploy/Form submissions and analytics events
/_p/Ploy's analytics script
Your site's own file folders, such as /images and /fontsImages and other files your pages use

To find your site's own folders, open a page through your proxy and look for files that fail to load in your browser's network tab.


Fix redirects and SEO URLs

  • Ploy's trailing-slash redirects use a relative address, so they stay on your public domain. If a redirect points to a full URL on your *.ploy.build address, have your proxy rewrite the Location header to your public domain.
  • Your proxy does not change links inside the page. Make sure canonical, hreflang and Open Graph URLs and sitemap entries use your public domain and path, such as https://www.example.com/guides. Ask Ploy's Agent to set them. Your *.ploy.build address is public too, so correct canonical URLs tell search engines to prefer your public domain.
  • Your proxy decides which robots.txt and sitemap visitors get at the root of your domain. Add your Ploy pages to the sitemap you serve there.

Caching

Ploy chooses each visitor's A/B test version when it serves the page. If your proxy caches page HTML and shares it across visitors, everyone sees the same version. Skip your cache for Ploy pages. Forward the visitor's Cookie header and pass Set-Cookie headers through unchanged, so each visitor keeps their version. You can cache files under /_ploy_static/ by following the cache headers Ploy sends.


Forms, analytics and A/B tests

Ploy drops form submissions and analytics events without an error when their Origin header does not match the address it served the page on. Behind your proxy, that header names your public domain. Remove the Origin header from every request your proxy forwards to Ploy.

FeatureBehind your own proxy
PagesWork
FormsWork when your proxy removes Origin
Ploy analyticsWork when your proxy removes Origin
A/B testsWork when your proxy fetches from your *.ploy.build address and removes Origin. Each visitor keeps the same version, and exposures and conversions are recorded.

Want to use Experiments? Experiments is ready to use and available on request. Email sales@ploy.ai to turn it on for your workspace.

Ploy sees your proxy's IP address, not the visitor's. Visitors served by the same proxy location share one rate limit, and location and other details based on IP address reflect your proxy.

After setup, send a test form through your public domain and check that it appears in Form submissions.


Example: Cloudflare Worker

This Worker serves /guides from your *.ploy.build address, forwards the paths above, removes the Origin header, and rewrites redirects. Everything else goes to your existing site.

// Your site's *.ploy.build address, not a custom domain.
const ORIGIN = 'https://your-site.ploy.build';
// Add your site's own file folders, such as /images and /fonts.
const PLOY_PATHS = ['/guides', '/images', '/fonts', '/_ploy_static', '/_ploy', '/_p'];

function isPloyPath(pathname) {
  return PLOY_PATHS.some((p) => pathname === p || pathname.startsWith(p + '/'));
}

export default {
  async fetch(request) {
    const url = new URL(request.url);
    if (!isPloyPath(url.pathname)) return fetch(request);

    // Copies the visitor's headers, including Cookie.
    const upstream = new Request(ORIGIN + url.pathname + url.search, request);
    upstream.headers.delete('Origin');

    const response = await fetch(upstream, { redirect: 'manual' });
    const location = response.headers.get('Location');
    if (!location || !location.startsWith(ORIGIN + '/')) return response;

    const headers = new Headers(response.headers);
    headers.set('Location', location.slice(ORIGIN.length) || '/');
    return new Response(response.body, { status: response.status, headers });
  },
};

Troubleshooting

  • A/B tests enroll no one and analytics events are dropped. Your proxy is fetching through a custom domain. Fetch from your site's *.ploy.build address instead.
  • A form shows success, but nothing is saved, or analytics stays empty. Remove the Origin header from requests your proxy forwards.
  • Pages load without styles or images. Forward /_ploy_static/ and your site's own file folders.
  • Visitors end up on your *.ploy.build address. Rewrite the Location header on redirects that use a full URL.
  • Every visitor sees the same A/B test version. Your proxy is caching page HTML. Skip the cache for Ploy pages.