Prerequisites #
Our guides are written with the expectation that the following requirements are met:
- Server: VPS/Dedicated Vultr / Contabo
- Ubuntu operating system 22.04 configured
- Docker/Docker Compose and Nginx installed
- SSH client with SFTP capabilities
Create a Docker Compose File for Supabase #
First, create a directory for your Supabase project and navigate to it:
mkdir supabase-with-nginx
cd supabase-with-nginx
Next, create a docker-compose.yml file:
version: '3.8'
services:
db:
image: postgres:13
volumes:
- db_data:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: yourpassword
POSTGRES_DB: postgres
supabase:
image: supabase/postgres:latest
environment:
POSTGRES_PASSWORD: yourpassword
ports:
- "54321:5432"
depends_on:
- db
volumes:
- supabase_data:/var/lib/postgresql/data
api:
image: supabase/gotrue:latest
environment:
GOTRUE_API_URL: http://localhost:9999
GOTRUE_DB_DRIVER: "postgres"
GOTRUE_DB_DATABASE_URL: "postgresql://postgres:yourpassword@db:5432/postgres"
GOTRUE_SITE_URL: "http://localhost"
ports:
- "9999:9999"
depends_on:
- supabase
rest:
image: supabase/rest:latest
environment:
PGRST_DB_URI: "postgresql://postgres:yourpassword@db:5432/postgres"
ports:
- "3000:3000"
depends_on:
- supabase
realtime:
image: supabase/realtime:latest
environment:
DB_HOST: db
DB_PORT: 5432
DB_USER: postgres
DB_PASSWORD: yourpassword
DB_NAME: postgres
PORT: 4000
ports:
- "4000:4000"
depends_on:
- db
volumes:
db_data:
supabase_data:
Add Reverse Proxy confs HTTP #
Open Nginx config with the following command:
nano /etc/nginx/nginx.conf
Hint: You can save with CRTL + O
and exit with CRTL + X
on Mac Command + O / Command + X
This is an example to reverse Proxy HTTP traffic:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
client_max_body_size 10M;
include /etc/nginx/mime.types;
default_type application/octet-stream;
gzip on;
gzip_disable "msie6";
server {
listen 80;
server_name selfhost.guru;
location / {
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:1234;
proxy_redirect off;
}
}
server {
listen 80;
server_name subdomain.selfhost.guru;
location / {
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:12345;
proxy_redirect off;
}
}
server {
listen 80;
server_name subdomain2.selfhost.guru, subdomain3.selfhost.guru;
location / {
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:5678;
proxy_redirect off;
}
}
}
Add Reverse Proxy confs Websocket #
For Websocket connections the following configuration needed:
server {
listen 80;
server_name websocket.selfhost.guru;
location / {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:123456/;
}
}
Bring Up the Services #
Run the following command to bring up the services:
docker-compose up -d
This command will start the PostgreSQL database, Supabase services, and Nginx.
Accessing Supabase #
Your Supabase services should now be accessible via Nginx on the following endpoints:
- Auth API:
http://yourdomain.com/auth
- REST API:
http://yourdomain.com/rest
- Realtime API:
http://yourdomain.com/realtime
Replace yourdomain.com
with your actual domain or IP address. If you're running locally, you can use localhost
.
Optional: Set Up SSL #
If you're using this setup in a production environment, it's crucial to secure the Nginx server with SSL. You can use Let's Encrypt to obtain a free SSL certificate. To do this, you can use Certbot with Nginx, but that's outside the scope of this guide.
Summary #
ou now have a Supabase setup running behind an Nginx reverse proxy using Docker Compose. Nginx will handle incoming requests and forward them to the appropriate Supabase service. This setup allows you to manage and scale your services efficiently.