Table of Contents
By the end of this guide, you will:
- Understand why OAuth redirect errors happen in cloud-hosted n8n
- Set up a public domain and map it to your EC2/VPS instance
- Use NGINX as a reverse proxy inside Docker
- Configure Let’s Encrypt SSL using Certbot and Docker volumes
- Run n8n with environment variables for correct redirect behavior
- Successfully authenticate Google integrations (like Google Sheets or Gmail)
- Automate SSL certificate renewal
This post is perfect for:
- Developers setting up n8n for the first time
- DevOps engineers automating workflows
- Startup teams using n8n in production
Anyone tired of fighting with localhost redirects!
Why “localhost” Breaks Google OAuth in n8n
When n8n generates an OAuth request, it includes a callback URL. By default, if not configured, it uses:
http://localhost:5678/rest/oauth2-credential/callback
So, after a user authenticates via Google, it tries to redirect to your laptop, not your server!
Google’s servers obviously can’t access your local machine, so it fails with:
“redirect_uri_mismatch” or “site can’t be reached”
💡 The Fix
Tell n8n to use your real domain (e.g., https://mydomain.com) instead of localhost. I will show how to do that with Docker.
Configure Your Domain and DNS for n8n
Let’s assume you own mydomain.com.
Steps:
- Log in to your DNS provider (e.g., Cloudflare, GoDaddy, Namecheap)
- Add an A Record:
- Host: @ or n8n
- Points to: your EC2 server’s public IP
- TTL: Automatic or 1 hour
This connects n8n.mydomain.com or mydomain.com to your server.
You can confirm it’s working with:
ping mydomain.com
curl http://mydomain.com
Using Docker to Run NGINX and Certbot for SSL
Instead of installing NGINX and Certbot manually, we’ll Dockerize both.
services:
nginx:
image: nginx:latest
container_name: nginx-proxy
ports:
– “80:80”
– “443:443”
volumes:
– ./nginx/nginx.conf:/etc/nginx/nginx.conf
– ./certbot/conf:/etc/letsencrypt
– ./certbot/www:/var/www/certbot
restart: always
networks:
– nginx-net
certbot:
image: certbot/certbot
container_name: certbot
volumes:
– ./certbot/conf:/etc/letsencrypt
– ./certbot/www:/var/www/certbot
entrypoint: >
/bin/sh -c “trap exit TERM; while :; do
certbot renew –webroot -w /var/www/certbot;
sleep 12h;
done”
networks:
– nginx-net
networks:
nginx-net:
external: true
Tip: Don’t forget to create the external network if it doesn’t exist:
docker network create nginx-net
Generate SSL Certificates with Certbot in Docker
Step 1: Serve the ACME challenge
listen 80;
server_name mydomain.com;
root /var/www/certbot;
location /.well-known/acme-challenge/ {
allow all;
}
location / {
return 404;
}
}
Run temporary NGINX container:
-p 80:80 \
-v $(pwd)/certbot/www:/var/www/certbot \
-v $(pwd)/nginx/default.conf:/etc/nginx/conf.d/default.conf \
nginx:alpine
Step 2: Request SSL certificate
-v $(pwd)/certbot/www:/var/www/certbot \
-v $(pwd)/certbot/conf:/etc/letsencrypt \
certbot/certbot certonly \
–webroot -w /var/www/certbot \
-d mydomain.com \
–agree-tos –no-eff-email \
-m you@example.com
Stop the temp server:
docker rm -f temp-nginx
Run n8n with Correct Domain and HTTPS Settings
Finally, run your n8n container with correct environment variables:
–name n8n \
-p 5678:5678 \
-e N8N_HOST=mydomain.com \
-e WEBHOOK_URL=https://mydomain.com \
-e VUE_APP_URL=https://mydomain.com \
-v n8n_data:/home/node/.n8n \
docker.n8n.io/n8nio/n8n
Fixing Google OAuth Redirect URI Errors
- Go to Google Cloud Console
- Edit your OAuth 2.0 Client ID
- Add the following to Authorized Redirect URIs:
If you don’t add this, Google will throw redirect_uri_mismatch errors.
Testing Your Setup
After completing all the above steps:
- Visit https://mydomain.com — you should see the n8n UI
- Try to connect a Google credential
Complete OAuth and see it redirect back to your domain (not localhost!)
Automating Everything with docker-compose
You can simplify the entire flow by putting your n8n instance into the same docker-compose.yml file and using the depends_on flag.
This allows you to:
- Restart all containers easily
- Manage all services in one place
- Mount certs directly from shared volumes
Let me know if you want a full compose setup including n8n.
Mistake | Fix |
localhostin OAuth callback |
Set WEBHOOK_URLand VUE_APP_URLto your domain |
Certbot 404 during challenge
|
Ensure nginx is serving /.well-known/…correctly |
Docker network errors
|
Use docker network create nginx-net |
Port already in use
|
Use docker psand docker rm -f <container>to clear |
🌟Conclusion and Deployment Tips
You’ve just:
- Set up n8n with a real public domain
- Secured it using free SSL certs
- Fixed Google OAuth integration
- Dockerized the full reverse proxy and automation stack
- Secure HTTPS hosting
- OAuth integrations
- Dockerized deployment