Sub topic 6
HTTP on ports 80 and 443
This chapter assumes you know the client-server model, can read short Python scripts, and have seen the TCP/UDP and socket material from earlier chapters. It covers how HTTP uses ports 80 and 443, what changes when TLS is added, and the mistakes that most often cause confusion in practice.
What HTTP is on the wire
HTTP (Hypertext Transfer Protocol) is an application protocol. A client sends a request, and the server sends back a response. Both are bytes sent over a transport connection. HTTP/1.1 is plain text, so you can type a request by hand.
The IANA registry assigns two default ports:
| Port | Service name | Transport | Meaning |
|---|---|---|---|
| 80 | http | TCP | HTTP without encryption |
| 443 | https | TCP and UDP | HTTP inside TLS (TCP), and HTTP/3 over QUIC (UDP) |
| 8080 | http-alt | TCP | Common alternative for proxies and application servers |
| 8443 | https-alt | TCP | Common alternative for HTTPS |
Browsers hide the port when it is the default for the scheme. http://example.com/ means port 80, and https://example.com/ means port 443. A URL such as http://example.com:8080/ states the port explicitly, so the browser connects there instead.
HTTP as bytes on a TCP connection
The following script opens a TCP connection to port 80, sends a minimal request, and prints the start of the response. HEAD asks for headers only, and Connection: close tells the server to close the connection after responding.
import socket
request = (
"HEAD / HTTP/1.1\r\n"
"Host: example.com\r\n"
"Connection: close\r\n\r\n"
).encode("ascii")
with socket.create_connection(("example.com", 80), timeout=5) as s:
s.sendall(request)
print(s.recv(1024).decode("latin-1"))The first line of the reply is the status line, for example HTTP/1.1 200 OK. Nothing in these bytes mentions port 80. The port only told the operating system which listening program on the server should receive the connection.
Three details matter here:
- Lines end with
\r\n, and a blank line ends the headers. Without it the server waits for more input and you see a timeout. - The
Hostheader is required in HTTP/1.1. One IP address and port can serve many sites, and the server usesHostto pick the right one. recv(1024)returns whatever has arrived, up to 1024 bytes. A real client must loop until the message is complete.
Seeing both endpoints of the connection
Every TCP connection has two endpoints. The server side is fixed at 80 or 443. The client side uses an ephemeral port, which the operating system picks from a high range.
import socket
with socket.create_connection(("example.com", 80), timeout=5) as s:
print("local :", s.getsockname())
print("remote:", s.getpeername())The output looks like local : ('192.168.1.20', 52144) and remote: ('93.184.216.34', 80). The local port changes on each run. A server tells clients apart by the full four values: client IP, client port, server IP and server port. Thousands of browsers can all connect to port 443 on one server because their source addresses and ports differ.
What changes on port 443
HTTPS is the same HTTP messages carried inside a TLS (Transport Layer Security) session. TLS provides three things:
- Encryption, so on-path observers cannot read the request or response.
- Integrity, so modification in transit is detected.
- Server authentication, through a certificate that proves the server controls the name the client asked for.
The client performs the TLS handshake first. Only then does it send HTTP.
You can run the TLS step yourself with Python's ssl module:
import socket
import ssl
host = "example.com"
context = ssl.create_default_context()
with socket.create_connection((host, 443), timeout=5) as raw:
with context.wrap_socket(raw, server_hostname=host) as tls:
print("peer :", tls.getpeername())
print("version:", tls.version())
print("cipher :", tls.cipher()[0])
print("subject:", tls.getpeercert()["subject"])create_default_context() turns on certificate validation and hostname checking. server_hostname is sent in the ClientHello as SNI (Server Name Indication). It lets a server that hosts many sites on one IP and port 443 choose the right certificate before any HTTP is sent. It is also used for the hostname check.
> Do not disable verification to make an error go away.
Redirects from 80 to 443
Most sites still listen on port 80, but only to send clients to port 443. The server answers with a status such as 301 Moved Permanently or 308 Permanent Redirect and a Location: https://... header. The first request, including its URL and any cookies, still travels unencrypted. HSTS (HTTP Strict Transport Security) reduces this exposure. A server sends the Strict-Transport-Security header over HTTPS, and the browser then upgrades future requests to that host to HTTPS without trying port 80 first.
If you close port 80 entirely, users who type a bare hostname get a connection timeout or refusal instead of a redirect. That is why many deployments keep port 80 open for the redirect.
HTTP/2 and HTTP/3
HTTP/2 normally runs inside TLS on TCP port 443. The client and server agree on it during the handshake using ALPN (Application-Layer Protocol Negotiation), so the port stays the same while the protocol changes. HTTP/3 uses QUIC, which runs over UDP, and it also uses port 443. A server usually advertises HTTP/3 with an Alt-Svc response header, and the browser may switch on later requests.
This has a firewall consequence. Allowing only TCP 443 gives clients HTTP/1.1 and HTTP/2. Clients fall back to those versions when UDP 443 is blocked, so the site keeps working but without HTTP/3.
Alternative ports and their limits
Development servers and application servers often use 8080, 8000, 3000 or 8443. These are in the registered range, so they do not need root privileges on Linux. Ports below 1024 normally do. A common pattern is an application on 8080 behind a reverse proxy that listens on 80 and 443.
A non-default port is part of the origin. https://example.com and https://example.com:8443 are different origins for browser security rules and cookies behave differently across them. Also, a number alone does not prove what is running there. Port 443 can carry something that is not HTTPS, and a web server can listen on any port. Check the actual protocol before drawing conclusions.
Checking from the command line
# Which processes listen on web ports?
ss -tlnp | grep -E ':(80|443|8080)\b'
# Does the port accept connections? (host you own)
nc -vz 192.0.2.10 443
# See the redirect without following it
curl -sI http://example.com/Common errors
| Symptom | Likely cause |
|---|---|
| Connection refused on 80 | Nothing is listening, or the service is bound to 127.0.0.1 only |
| Timeout on 443 | A firewall drops packets, or the security group does not allow 443 |
| `SSL: WRONG_VERSION_NUMBER` | HTTPS client talking to a plain HTTP port such as 8080 |
| Garbled text or `400 Bad Request` for a TLS handshake | Plain HTTP port receiving a TLS ClientHello |
| Certificate name mismatch | Connecting by IP address or a name not in the certificate |
| Works on IPv4 but not IPv6 | The service or firewall rule covers only one address family |
Exercise
A colleague's browser shows "connection timed out" for https://app.example.org, but curl http://app.example.org:8080/ works from the same machine. Name two things you would check next, and say what each result would tell you.
Show answer
First, test the port directly with nc -vz app.example.org 443. A timeout means a firewall or security group is dropping TCP 443, while "connection refused" means the host is reachable but nothing listens on 443. Second, on the server run ss -tlnp | grep 443 to see whether a process is listening and on which address. If the process is bound only to 127.0.0.1 or not running, the fix is in the service configuration, not the firewall. The working 8080 result shows that the host and network path are fine and that the application is only reachable on the alternative port.