Repository navigation
Add magebox expose command for sharing projects via Cloudflare Tunnels - #25
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
magebox exposecommand that creates a public*.trycloudflare.comURL pointing to the local project using Cloudflare quick tunnels (no account required)web/unsecure/base_urlandweb/secure/base_urlto the tunnel URL on start, and reverts them on stopmagebox expose stop(from a separate terminal) andmagebox expose statusCommands
magebox expose [domain]magebox expose stopmagebox expose statusRequirements
cloudflaredmust be installed (brew install cloudflared)