Using nginx as a reverse proxy

Last updated:

|
Warning
  1. The following self-hosted proxy isn't provided by PostHog, so we can't take responsibility for it! If unsure, we recommend using our managed reverse proxy.

  2. If you are using the EU cloud then use eu instead of us in all domains (e.g. us.i.posthog.com -> eu.i.posthog.com).

  3. Avoid using generic or common path names like /analytics, /tracking, /ingest, or /posthog for your reverse proxy. They will most likely be blocked. Instead, use a non-obvious path name or something random and unique to your application that's unlikely to appear in a filter list.

You can use nginx as a reverse proxy. To do this, first create a Dockerfile to build a nginx container:

dockerfile
FROM nginx:alpine
COPY nginx.conf /etc/nginx/nginx.conf

Next, create the nginx.conf file with the reverse proxy configuration. Change us to eu to proxy to our EU Cloud.

nginx.conf
events {
worker_connections 1024;
}
http {
server {
listen 8080; # Set the port to 8080
server_name _; # Accept requests for any domain
# Specify DNS resolver for variables, this is Google Public DNS
# You should adjust this to a DNS resolver of your choice
resolver 8.8.8.8 8.8.4.4 [2001:4860:4860::8888] [2001:4860:4860::8844] valid=300s;
resolver_timeout 5s;
proxy_http_version 1.1;
proxy_ssl_server_name on;
set $posthog_main_host "us.i.posthog.com"; # Change to "eu.i.posthog.com" for EU
set $posthog_assets_host "us-assets.i.posthog.com"; # Change to "eu-assets.i.posthog.com" for EU
location ~ ^/static/(.*)$ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass https://$posthog_assets_host/static/$1$is_args$args;
proxy_set_header Host $posthog_assets_host;
}
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass https://$posthog_main_host/$1$is_args$args;
proxy_set_header Host $posthog_main_host;
}
}
}

Questions? Ask Max AI.

It's easier than reading through 817 pages of documentation

Community questions

Was this page useful?

Next article

Using node:http as a reverse proxy

You can use Node's built-in http module to proxy requests to PostHog. This is a simple and lightweight way to proxy requests. To use this proxy, you can create a server like this: Once done, you can initialize the PostHog client with the proxy path: Thank you to SimonSimCity for this method.

Read next article