On this page
When a URL omits its port, the scheme determines which port a browser normally uses. An explicit default port can disappear when the browser serializes the URL, while a non-default port remains part of its origin. This distinction matters when debugging origin checks, reverse proxies, and deployments.
Key Takeaways
https://example.comandhttps://example.com:443have the same origin;https://example.com:8443does not.- Browser URL parsing removes an explicit default port from the serialized URL. The
URL.portproperty is then an empty string, not"443"or"80". - A non-default port can change both the destination and the origin, even when the hostname is unchanged.
- An external URL, a proxy’s upstream address, and a server’s listening socket describe different parts of a deployment. Their ports do not have to match.
Default ports in URLs explained
For these common schemes, the URL parser recognizes the following default ports:
| Scheme | Default port | Example with an omitted port |
|---|---|---|
http | 80 | http://example.com/ |
https | 443 | https://example.com/ |
ws | 80 | ws://example.com/ |
wss | 443 | wss://example.com/ |
ftp | 21 | ftp://example.com/ |
Recognizing a URL scheme or its default port does not guarantee that a browser supports opening it or that a server is listening there. In particular, do not assume FTP access is available in a browser.
For an HTTP URL, the browser normally connects to port 80 when no port is specified; for HTTPS, it normally connects to port 443. An explicit non-default port changes the connection destination. For example, https://example.com:8443/ requests HTTPS on port 8443; the service listening there still needs to support TLS.
Advertisement
How URL serialization handles default ports
Run this in a browser developer console or a recent Node.js runtime:
const url = new URL('https://example.com:443/docs');
console.log(url.href); // https://example.com/docs
console.log(url.host); // example.com
console.log(url.port); // "" (empty string)
console.log(url.origin); // https://example.com
The parser knows that 443 is the HTTPS default, so its serialized representation omits the port. This does not mean the connection has no port; it means the default is implicit. Similarly, parsing http://example.com:80/ produces a URL whose port is empty.
A non-default port stays visible:
const url = new URL('https://example.com:8443/docs');
console.log(url.href); // https://example.com:8443/docs
console.log(url.host); // example.com:8443
console.log(url.port); // 8443
console.log(url.origin); // https://example.com:8443
Do not interpret an empty URL.port as proof that the input contained no port. If the original spelling matters for logging or diagnostics, retain the original input separately. Also avoid splitting host on a colon to extract a port: IPv6 addresses contain colons. Use the URL parser’s hostname and port properties instead.
Ports and same-origin comparisons
For ordinary HTTP and HTTPS URLs, an origin consists of the scheme, hostname, and effective port. Paths, query strings, and fragments are not part of the origin.
| First URL | Second URL | Same origin? |
|---|---|---|
https://example.com/ | https://example.com:443/account | Yes |
http://example.com/ | http://example.com:80/ | Yes |
https://example.com/ | https://example.com:8443/ | No: different port |
http://example.com/ | https://example.com/ | No: different scheme and effective port |
http://example.com:443/ | https://example.com/ | No: different scheme |
A different origin can affect browser behavior, including whether a cross-origin HTTP request needs a successful CORS response before JavaScript can read it. CORS does not make two URLs the same origin. WebSocket handshakes have their own origin-related behavior; do not assume HTTP CORS rules apply to them identically.
Compare parsed origins, not raw strings
The following example accepts absolute HTTP(S) URLs and compares their normalized origins. It rejects invalid URLs and other schemes rather than treating an opaque null origin as a trustworthy match.
function httpOrigin(input) {
try {
const url = new URL(input);
if (url.protocol !== 'http:' && url.protocol !== 'https:') {
return null;
}
return url.origin;
} catch {
return null;
}
}
function sameHttpOrigin(first, second) {
const a = httpOrigin(first);
const b = httpOrigin(second);
return a !== null && b !== null && a === b;
}
console.log(sameHttpOrigin(
'https://EXAMPLE.com:443/a',
'https://example.com/b'
)); // true
console.log(sameHttpOrigin(
'https://example.com/',
'https://example.com:8443/'
)); // false
This compares origins only; it does not check paths, authenticate a caller, or establish that a destination is safe to fetch. In a server-side allowlist, compare against explicitly configured, trusted origins. An Origin header is useful in browser-oriented request handling but is not a substitute for authentication: non-browser clients can construct their own headers.
Advertisement
Why deployment ports may differ
Consider this deployment:
Browser: https://example.com/ (external port 443)
Reverse proxy: accepts TLS on port 443
Application: listens on 127.0.0.1:3000
The browser’s origin is https://example.com, not http://example.com:3000. The proxy can terminate TLS and forward the request to the application over a separate connection. Port 3000 might be accessible only locally, or network policy might expose it; the URL alone cannot establish that.
When an application needs to reconstruct its public URL, configure the public origin explicitly where possible. If it relies on forwarded scheme or host information, configure trusted proxies carefully and reject or ignore such headers from untrusted clients. Otherwise, an application may generate incorrect links or accept attacker-controlled host information.
A firewall, proxy, and application listener each enforce different boundaries. Opening port 443 at a firewall does not prove that a proxy is listening there, that it forwards to the right upstream, or that the application authorizes the request. Conversely, an application listening on port 3000 does not prove that the port is publicly reachable.
Debug a mismatch
- Record the exact URL in the browser, including its scheme and any visible port. In the console, inspect
new URL(location.href).originandnew URL(location.href).port. - Compare the browser-visible origin with the configured CORS or origin allowlist. Check scheme and port separately, not just the hostname.
- Trace the request through each proxy and note the external listener and upstream address. Inspect proxy and application logs you are authorized to access.
- Check the application’s public-origin and trusted-proxy settings. Confirm that forwarded headers are accepted only from trusted infrastructure.
- Check listener and network rules independently. A correct origin comparison does not establish network reachability or authorization.
Common Mistakes
- Comparing complete URL strings as origins. Paths and default-port spelling can differ while the origins match.
- Assuming an empty
URL.portmeans no network port. It commonly means the scheme’s default port is implicit. - Treating
https://example.com:80as HTTP. The scheme remains HTTPS; the client attempts TLS on port 80. - Assuming a port implies a protocol. A port number alone does not guarantee that an HTTP or TLS service is available there.
- Trusting arbitrary forwarded headers. They can misrepresent the public scheme or host unless a trusted proxy controls them.
Best Practices
Use a standards-based URL parser for URL components and compare normalized origins for origin-specific checks. Keep a configured list of expected public origins, including any intentional non-default ports. Test the browser-facing URL as well as the proxy-to-application connection, and treat network exposure, CORS, authentication, and authorization as separate concerns.
Conclusion
Default ports make URLs shorter, but they remain relevant to connections and origin checks. Parse URLs before comparing origins, and investigate public and upstream ports separately when debugging a deployment.
The WHATWG URL Standard and browser same-origin documentation are the primary references for these behaviors. Live source retrieval was unavailable for this article, so their current text and any version-specific browser behavior were not independently checked.
What would your domain sell for?
A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.