On this page
An IPv6 address in a URL must be enclosed in square brackets when it is used as a literal host. If the URL includes a port, put the port after the closing bracket. This guide shows the syntax, common errors, and the limitations of zone identifiers.
Key Takeaways
- Write an IPv6 literal as
http://[address]/, nothttp://address/. - Append a port outside the brackets:
http://[address]:port/. - Brackets are URL syntax; they are not part of the IPv6 address itself.
- Zone identifiers can be useful for link-local addresses, but support varies across URL parsers and clients.
IPv6 address in URL syntax
An IPv6 address contains colons, which would otherwise be ambiguous with the colon that separates a host from a port. Enclose the entire literal host in brackets:
scheme://[IPv6-address]:port/path
The port and path are optional. For example:
https://[2001:db8::1]/
https://[2001:db8::1]:8443/api
http://[::1]:8080/
Here, 2001:db8::1 is a documentation address, and ::1 is the IPv6 loopback address. The brackets make the boundary between the host and any port explicit.
| Valid | Invalid | Problem |
|---|---|---|
http://[::1]/ | http://::1/ | The literal is not bracketed. |
http://[::1]:8080/ | http://::1:8080/ | The address and port are ambiguous. |
https://[2001:db8::1]:8443/ | https://[2001:db8::1:8443]/ | The latter does not specify port 8443; it places those digits inside the address. |
The last invalid example may still be a syntactically valid IPv6 URL, but it represents a different host without an explicit port. It is invalid for the intended address and port.
Advertisement
Add a port after the closing bracket
Put a colon and the decimal port number immediately after ]:
http://[::1]:8080/status
The host is ::1, the port is 8080, and the path is /status. Do not write http://[::1:8080]/status when you mean port 8080: that notation puts 8080 inside the literal address instead.
If you omit the port, the client uses the scheme's default, such as port 80 for HTTP or 443 for HTTPS. A syntactically correct URL does not guarantee that a service is listening on the chosen address and port.
Try a loopback URL
On a machine with IPv6 loopback enabled, you can start a temporary HTTP server bound to ::1. From a directory containing only files you are comfortable serving, run:
python3 -m http.server 8080 --bind ::1
In another terminal, request it with curl:
curl --globoff --noproxy '*' --connect-timeout 5 'http://[::1]:8080/'
--globoff prevents curl from treating the brackets as URL globbing syntax. --noproxy '*' avoids sending this local request through a configured proxy. If the server starts and the connection succeeds, curl displays the server's response, normally a directory listing. Stop the server with Ctrl+C.
If it fails, check that IPv6 loopback is enabled, port 8080 is free, and your Python and curl installations support IPv6. This example has not been executed for this article.
Advertisement
Zone identifiers and interoperability
A link-local address such as fe80::1 may need a zone identifier to select the local interface on which it is reachable. The zone is local to the machine interpreting it; an interface name on one host is not necessarily meaningful on another.
You may encounter a URI representation such as:
http://[fe80::1%25eth0]/
Here %25 represents the percent sign separating the address from the zone identifier eth0. This is not a portable URL to copy into every client: browser URL parsers, command-line tools, operating systems, and libraries can differ in whether and how they accept zone identifiers. Interface names also vary. Check the documentation for the specific client and environment, and avoid relying on zone-qualified URLs as general-purpose links.
The general URI syntax in RFC 3986 specifies bracketed IP literals but does not itself define zone identifiers. This article was prepared without access to the standard or an execution environment, so consult the relevant standards and your implementation's current documentation before relying on zone-identifier behavior.
Common Mistakes
- Omitting brackets:
http://2001:db8::1/is not the correct URL form for that IPv6 literal. - Putting the port inside the brackets:
http://[::1:8080]/does not mean::1on port 8080. - Adding brackets to a DNS name: Use
https://example.com/, nothttps://[example.com]/. - Assuming syntax proves reachability: A valid literal still needs a reachable IPv6 route, a listening service, and appropriate firewall rules.
- Assuming HTTPS will authenticate any literal: The server's certificate must be valid for the IP address used by the client; a certificate for a DNS name alone is not enough.
Best Practices
- Use a URL parser appropriate to your language rather than splitting an authority on colons yourself.
- Keep address literals and port numbers in separate configuration fields where practical, and construct the URL with a suitable library.
- Test the actual client and deployment environment, particularly when using link-local addresses or zone identifiers.
- Treat parsing and validation as separate tasks. A successfully parsed URL is not necessarily safe to fetch or permitted by your application's network policy.
Conclusion
Wrap an IPv6 literal in square brackets and place any port after the closing bracket. For zone-qualified addresses, verify support in the specific client rather than assuming the same URL works everywhere.
What would your domain sell for?
A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.