Using n8n from a reverse proxy

Hello I think I just found how use n8n from a reverse proxy without having the connection lost message when going in the workflow editor.

I’ve searching for this everywhere but there only was questions about how to solve problems due to some error caused by n8n when detects it’s runing behind a reverse proxy. I hope this help someone.

Here you have the files I used ot make it possible, also remember to enable the necessary apache2 modules to use it as a proxy with ssl and all of that. Please let me know if you think it could be better, if I missed something.

sudo a2enmod ssl
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_wstunnel
sudo a2enmod headers
sudo systemctl reload apache2

docker-compose.yml

services:
  n8n:
    image: docker.n8n.io/n8nio/n8n
    restart: always
    ports:
      - "127.0.0.1:5678:5678"
    environment:
      - N8N_HOST=localhost
      - N8N_PORT=5678
      - N8N_PROTOCOL=http
      - NODE_ENV=production
      - WEBHOOK_URL=https://n8n.example.es/
      - GENERIC_TIMEZONE=Europe/Madrid
      - EXPRESS_TRUST_PROXY=true
      - N8N_RUNNERS_ENABLED=true
      - N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS=true
    volumes:
      - n8n_data:/home/node/.n8n

volumes:
  n8n_data:

n8n.conf

<VirtualHost *:443>
    ServerName n8n.example.es

    ProxyPreserveHost On

    ProxyPass "/rest/push" "ws://127.0.0.1:5678/rest/push"
    ProxyPassReverse "/rest/push" "ws://127.0.0.1:5678/rest/push"

    ProxyPass / http://127.0.0.1:5678/
    ProxyPassReverse / http://127.0.0.1:5678/

    RequestHeader set X-Forwarded-Proto "https" early

    ErrorLog ${APACHE_LOG_DIR}/n8n.example.com.error.log
    CustomLog ${APACHE_LOG_DIR}/n8n.example.com.access.log combined
</VirtualHost>