Dev.to WebDev 🛠 Dev 👁 0 📖 8 min read

Subdomain or Subpath: Hosting a Product Beside a Blog on One Domain

Subdomain or Subpath: Hosting a Product Beside a Blog on One Domain A blog on GitHub Pages and a Node product that needs a server cannot share a host. One is static files; the other is a process with a database. So the

Subdomain or Subpath: Hosting a Product Beside a Blog on One Domain

A blog on GitHub Pages and a Node product that needs a server cannot share a host. One is static files; the other is a process with a database. So the product has to live somewhere else and the question becomes how it appears to readers: app.yourdomain.com or yourdomain.com/app.

I have run both shapes for the same application. The subdomain version took an afternoon. The subpath version took an afternoon plus a week of finding places where a path prefix leaked through assumptions, and it taught me more about how a web app is wired than the first version did.

This article compares them honestly: what each costs in DNS, TLS, reverse proxy config, application code, and search visibility, and where the breaking points hide.

The two shapes

Subdomain                          Subpath
─────────────────────────────      ─────────────────────────────
app.example.com                    example.com/app
   → 203.0.113.10:443                  → 203.0.113.10:443
   → Express :3000                     → Express :3000 (with /app prefix)

example.com                        example.com
   → GitHub Pages                      → GitHub Pages (root stays)

The subdomain leaves the blog completely alone. The subpath puts the product inside the blog's domain, which means the domain's DNS now points at the server, and the blog has to be proxied back to Pages.

What the subdomain costs

  1. One DNS record:
app    A    203.0.113.10    TTL 600
  1. One TLS certificate for that hostname:
certbot certonly --webroot -w /var/www/html -d app.example.com
  1. One Nginx server block:
server {
  listen 443 ssl http2;
  server_name app.example.com;

  # point these at the fullchain and key certbot wrote for this host
  ssl_certificate     /etc/letsencrypt/live/app.example.com/fullchain;
  ssl_certificate_key /etc/letsencrypt/live/app.example.com/privkey;

  location / {
    proxy_pass http://127.0.0.1:3000;
    proxy_set_header Host              $host;
    proxy_set_header X-Real-IP         $remote_addr;
    proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
  }
}

That is it. The application does not know it is on a subdomain. Every app.get('/api/me') still matches. Static assets at /js/app.js still resolve. Cookies default to that host and stay there.

Two things worth doing anyway:

  • Do not expose the Node port. The firewall allows 80 and 443, not 3000. If curl http://203.0.113.10:3000 works from outside, that is a finding.
  • Forward the protocol header. Without X-Forwarded-Proto, the app cannot tell HTTPS from HTTP and will either set insecure cookies or loop on redirects.

What the subpath costs

The reverse proxy part is one line with one easily missed character:

location ^~ /app/ {
  proxy_pass http://127.0.0.1:3000/;   # trailing slash strips /app
}

location = /app {
  return 301 /app/;
}

With the trailing slash, /app/api/me reaches Express as /api/me. Without it, Express sees /app/api/me and every route 404s. The location = /app redirect exists because /app without a slash is a different location match and produces a page whose relative links all resolve one level too high.

Then the application code has to cooperate. Four places, in the order they break:

1. Server routes. Every mount needs the prefix:

const BASE = config.basePath;   // '/app' in subpath mode, '' otherwise

app.use(`${BASE}/api`, apiRouter);
app.use(BASE || '/', express.static(path.join(__dirname, 'public')));

Mounting once at the top is not enough if any handler builds absolute URLs by hand. Grep for string literals starting with /api and you will find them.

2. Client-side API prefix. The browser does not know the mount point unless you tell it:

const BASE = location.pathname.startsWith('/app') ? '/app' : '';
const API = `${BASE}/api`;

Deriving it from location.pathname is fragile but adequate when the app lives at exactly one prefix. The safer version injects the value at render time.

3. Static asset URLs. <script src="/js/app.js"> is absolute and now wrong. Either prefix every reference or use relative paths and a <base href>. In this app I prefixed references, because relative paths break on nested routes.

4. The "is this my own origin" check. The app had a branch that called its own API directly when the page was served by Express, and went through a CORS proxy otherwise. In subpath mode that check compared location.hostname against the API host and misclassified the page, so requests went to the wrong place. This was the bug that took the longest to see, because it only appeared on the deployed site, not locally.

A quick smoke list for subpath deploys:

curl -sI https://example.com/app/           | head -1   # 200
curl -s  https://example.com/app/api/me     | head -c80 # JSON or 401
curl -sI https://example.com/app/js/app.js  | head -1   # 200, correct type
curl -sI https://example.com/app             | head -1   # 301 → /app/

DNS, TLS, and the blog in subpath mode

Subpath mode requires the apex record to point at the server, which means the blog is no longer served by Pages directly. The server proxies it back:

location / {
  proxy_pass https://yourname.github.io/;
  proxy_set_header Host yourname.github.io;
  proxy_ssl_server_name on;
}

Note the Host override. Pages routes by hostname, so forwarding the reader's Host: example.com returns a 404 from Pages even though the origin is correct. proxy_ssl_server_name is required for SNI to work against the upstream.

The cost here is not the config, it is the interruption: the moment the apex record changes, the blog is down until the proxy is live and the certificate covers the apex plus www. In subdomain mode, none of this happens.

Certificates differ too. Subpath needs one certificate for example.com and www.example.com, and renewals must succeed while traffic is being proxied. Subdomain needs its own certificate for its own host, independent of the blog's.

Search visibility and analytics

This is the part that is not about infrastructure.

  • Subdomain is treated as a separate site by search engines. It earns its own links, its own indexing, and its own crawl budget. For a product that is a distinct entity, that is fine. For a product that is a section of one publication, it splits authority.
  • Subpath inherits the blog's authority and its existing links. A reader who links to example.com/app strengthens the same domain the articles live on. One Search Console property covers both, one sitemap can list both, and internal links pass value in the same origin.

If the blog is the thing you want to grow, subpath is the better SEO shape. If the product is the thing with its own audience, the subdomain is cleaner: separate analytics, separate indexing rules, and no risk that a product outage looks like a site-wide failure.

Two practical notes for either shape:

  • Set a canonical on the product pages. In subpath mode, example.com/app and example.com/app/ are different URLs; pick one.
  • Decide ad policy per host. If the blog runs display ads and the product has a paid tier, keeping them on separate hosts makes "no ads on authenticated pages" a config line rather than a template condition.

The cost of changing your mind

The choice is not permanent, but it is not free either, and the direction matters.

Subdomain to subpath is a DNS move and a prefix retrofit at the same time. Everything in the subpath section above has to be done at once, and every existing link to the subdomain has to keep working:

# keep old links alive on the subdomain host
server {
  listen 443 ssl http2;
  server_name app.example.com;
  return 301 https://example.com$request_uri;
}

Note $request_uri rather than $uri: it preserves the query string, so a link to app.example.com/pay/status?order=123 lands on the matching subpath URL instead of a bare page.

Subpath to subdomain is cheaper. The app already knows its prefix; you set it to empty, point a new host at the server, and add a redirect from the old prefix. The blog stays where it is throughout.

In both directions, three things need attention beyond the redirect:

  • Canonical tags. For a few weeks both URLs resolve. Every page should declare one canonical, or the two copies compete in the index.
  • Search Console. A subdomain is a separate property. Moving to subpath means the subdomain property's data stops accumulating, and the new URLs need to be crawled before the redirect is retired. Keep the redirect up for at least a quarter, not a fortnight.
  • Sitemaps and internal links. One sitemap listing both hosts is a mistake that is hard to see. Pick the canonical host and make the sitemap agree with it.

Sessions are the fourth, quieter item: cookies are host-scoped, so a reader who was logged in on app.example.com is logged out after the move. Either accept one re-login or migrate by setting a cookie on the new host during the redirect window.

Which one I would pick

Subdomain, unless the product genuinely belongs to the same publication.

The reason is not that subpath is unmaintainable; it is that the prefix becomes a second routing system that lives outside the router. Every hardcoded path, every absolute asset URL, every environment-specific base URL is a place where the two systems disagree, and those failures show up in production, not in tests. The subdomain version has none of them.

I use subpath only when one of these is true:

  • the product must share the blog's domain authority;
  • there is exactly one certificate to manage and the apex is already on the server;
  • the app is small and every URL is generated from one base constant;
  • a separate DNS zone is not available, for organizational rather than technical reasons.

If you do choose subpath, put the base path in one constant, derive everything from it, and test the four smoke URLs above on the deployed host before you announce anything. The trailing slash in proxy_pass will be the first thing to check when something 404s.

This article compares the two deployments I have run for the fund analysis product linked from wuhuajin.com: a subdomain with its own certificate, and a subpath behind the same domain as the blog. Hostnames, addresses and paths are placeholders; the failure modes are the ones that actually occurred.

Originally published on wuhuajin.com.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.