Skip to content

Add magebox expose command for sharing projects via Cloudflare Tunnels - #25

Merged
peterjaap merged 18 commits into
mainfrom
cloudflared
Mar 15, 2026
Merged

peterjaap merged 18 commits into
mainfrom
cloudflared

Conversation

@peterjaap

@peterjaap peterjaap commented Mar 15, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Adds magebox expose command that creates a public *.trycloudflare.com URL pointing to the local project using Cloudflare quick tunnels (no account required)
  • Automatically updates Magento's web/unsecure/base_url and web/secure/base_url to the tunnel URL on start, and reverts them on stop
  • Supports magebox expose stop (from a separate terminal) and magebox expose status

Commands

Command Description
magebox expose [domain] Start tunnel for the project (optionally specify which domain)
magebox expose stop Stop the tunnel and revert Magento base URLs
magebox expose status Show tunnel status and public URL

Requirements

  • cloudflared must be installed (brew install cloudflared)

Adds `magebox expose` which creates a public *.trycloudflare.com URL
pointing to the local project via cloudflared quick tunnels. Uses
--http-host-header to route traffic to the correct nginx vhost and
--no-tls-verify for local mkcert certificates. Supports expose stop,
expose status, and exposing specific domains.
When starting a tunnel, saves the current Magento base URLs, sets them
to the Cloudflare tunnel URL, and flushes cache. On Ctrl+C or expose
stop, reverts the URLs to their original values and flushes cache
again. Original URLs are persisted to a JSON file so expose stop can
revert them even from a separate process.
Capture combined output from bin/magento config:set to display the
actual error message instead of just the exit code. Also fix the
success message to only print when URLs were actually updated.
When Magento base URLs are locked in env.php, config:set fails. Now
detects the locked value error and retries with --lock-env to override
the value directly in env.php. Works for both setting and reverting.
The --http-host-header flag caused a redirect loop: Magento's base URL
pointed to the tunnel domain but the Host header said the local domain,
so Magento kept redirecting.

Now cloudflared connects to HTTP without Host rewriting. A temporary
nginx vhost accepts the tunnel hostname and proxies to the project's
local HTTPS vhost with X-Forwarded-Proto: https and proxy_ssl_verify
off. This lets Magento see the correct tunnel hostname while nginx
routes to the right PHP backend. The tunnel vhost is removed and nginx
reloaded on stop.
Instead of creating a standalone tunnel vhost, adds the Cloudflare
tunnel hostname as a domain entry in .magebox.yaml with SSL disabled.
This uses the existing vhost generation machinery so the tunnel domain
is served by the same nginx config as the local domain. On stop, the
tunnel domain is removed from .magebox.yaml and the vhost is cleaned up.
Cloudflare tunnel domains like *.trycloudflare.com exceed nginx's
default server_names_hash_bucket_size of 64. Adds EnsureHashBucketSize
to the nginx controller which sets the value to 128 in nginx.conf if
not already configured. Called during bootstrap and before expose
nginx reloads.
The default nginx.conf on many distros has a commented-out
server_names_hash_bucket_size directive. The previous check matched
the comment and skipped the update. Now scans line-by-line to
distinguish commented vs active directives, and uncomments/updates
the value to 128 when found commented out.
Magento has separate base URL settings for media and static assets.
Now updates all 6 URL paths (base, media, static × secure/unsecure)
when exposing and reverts them all on stop.
Ensures both the cache backend and storage are fully cleared when
switching base URLs, preventing stale blocks with old domain URLs
from being served.
Move the public URL display to after nginx vhost regeneration, Magento
base URL updates, and cache flush so users don't click it before
everything is ready.
Base URLs set at website or store scope weren't being updated because
config:set only targets the default scope. Now reads all base URL
entries from core_config_data (across all scopes) via direct SQL,
saves them for restoration, and updates them all to the tunnel URL.
Eliminates the bin/magento config:set dependency for URL management.
If base URLs in the database still contain .trycloudflare.com from a
previous run that wasn't cleanly reverted, they are now detected and
replaced with URLs derived from the local domain before being saved
as originals. The stale values are also immediately fixed in the
database to prevent redirect loops.
Base URLs locked in env.php via --lock-env override core_config_data
and prevent our SQL-based URL changes from taking effect. Now detects
and temporarily removes the web config section from env.php when
starting a tunnel, saving a backup. The section is restored on stop
or Ctrl+C. Uses PHP to safely manipulate the env.php array structure.
Base URLs locked in config.php (committed to git) override
core_config_data. SQL-only changes had no effect. Now uses
config:set --lock-env to write tunnel URLs to env.php which takes
priority over both config.php and the database. On revert, restores
original env.php values or removes the lock if it wasn't originally
there. Consolidated all state into a single state.json file.
Media and static URLs resolved from config.php were still pointing to
localhost because only base_url was locked in env.php. Now locks all 6
paths (base, media, static × secure/unsecure) via --lock-env to fully
override config.php. Saves and restores all 6 on revert.
Full-page cache stored in Redis may not be fully cleared by
bin/magento cache:flush alone. Now runs FLUSHALL on Redis after
the Magento cache flush to ensure no stale HTML with old domain
URLs is served.
@peterjaap
peterjaap merged commit d4e1de6 into main Mar 15, 2026
10 checks passed
@peterjaap
peterjaap deleted the cloudflared branch March 15, 2026 19:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant