MenuNginx

Proxy guides

Nginx

Serve the ProductLense script from your own domain with an Nginx location block, and why events must not go through a plain proxy_pass.

Nginx can serve the script from your domain with a few lines. It should not forward events with proxy_pass: ProductLense would see your server's IP address for every visitor. To forward events, use a small route in your application server instead, as shown in the proxy overview.

Serve the script

/etc/nginx/conf.d/example.conf
server {
    listen 443 ssl;
    server_name example.com;
    # ... your existing configuration

    location = /js/a.js {
        proxy_pass https://api.productlense.app/a.js;
        proxy_set_header Host api.productlense.app;
        proxy_ssl_server_name on;
        proxy_ssl_name api.productlense.app;
        proxy_hide_header Set-Cookie;
    }
}

proxy_ssl_server_name on sends the right hostname during the TLS handshake; without it the request to ProductLense fails. ProductLense's Cache-Control header passes through, so browsers cache the script for a day.

Then load the script from your path, with events going straight to ProductLense:

HTML
<script defer data-site="SITE_ID" data-api="https://api.productlense.app" src="/js/a.js"></script>

Forward events

If you are on the Scale plan and want events to be first-party too, send /m/api/e to your application and let it add the visitor's IP address, as in the Express and Go examples:

NGINX
location = /m/api/e {
    proxy_pass http://127.0.0.1:3000;   # your application, not ProductLense
    proxy_set_header X-Forwarded-For $remote_addr;
    client_max_body_size 8k;
}

Check it works

  1. Run nginx -t && nginx -s reload, then open https://example.com/js/a.js: it returns JavaScript.
  2. Load a page with the developer tools open. The Network tab shows a POST to api/e with status 204.
  3. Open the Live page in the ProductLense dashboard. Your visit appears within a few seconds.

    ↑ ↓ to move, Enter to open