HTTP Protocol Fundamentals
Overview
- What you’ll learn: The HTTP protocol architecture, request/response lifecycle, HTTP methods, status codes, headers, and how to inspect HTTP traffic from the command line.
- Prerequisites: Module 2 — TCP/IP & Networking (Lessons 11–20)
- Estimated reading time: 15 minutes
Introduction
The Hypertext Transfer Protocol (HTTP) is the foundation of data communication on the World Wide Web. Every time a browser loads a web page, submits a form, or fetches an API response, HTTP is the protocol that governs how the request is sent and how the server responds. As a Linux system administrator, understanding HTTP at a deep level is essential for configuring web servers, troubleshooting connectivity issues, and securing web applications.
HTTP operates at the application layer (Layer 7) of the OSI model, running on top of TCP. It follows a simple client-server model: a client (typically a browser or command-line tool) sends a request, and a server returns a response. Despite this simplicity, HTTP has evolved significantly — from the early HTTP/0.9 specification to the modern HTTP/2 and HTTP/3 protocols that power today’s high-performance web applications.
In this lesson, you will learn the structure of HTTP requests and responses, the most commonly used methods and status codes, how headers control behavior, and how to inspect HTTP traffic using command-line tools on Ubuntu Server.
The HTTP Request/Response Model
HTTP communication follows a stateless request/response cycle. The client initiates a connection to the server, sends a request message, and the server returns a response message. Each transaction is independent — the server does not inherently remember previous requests (though cookies and sessions layer statefulness on top).
Anatomy of an HTTP Request
An HTTP request consists of three parts: the request line, headers, and an optional body.
GET /index.html HTTP/1.1
Host: www.example.com
User-Agent: curl/7.81.0
Accept: text/html
The request line contains the method (GET), the request URI (/index.html), and the HTTP version (HTTP/1.1). The headers provide additional metadata about the request, and the body (not present in GET requests) carries data for methods like POST and PUT.
Anatomy of an HTTP Response
An HTTP response also has three parts: the status line, headers, and a body.
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Content-Length: 1234
Server: Apache/2.4.52
<!DOCTYPE html>
<html>...</html>
The status line contains the HTTP version, a status code (200), and a reason phrase (OK). The headers describe the response, and the body contains the actual content returned to the client.
HTTP Methods
HTTP defines several request methods, each with a specific semantic purpose. The most commonly used methods are:
- GET: Retrieve a resource. GET requests should be safe and idempotent — they do not modify server state.
- POST: Submit data to the server, typically to create a new resource or trigger a process.
- PUT: Replace an entire resource at the specified URI. Idempotent — sending the same PUT request multiple times produces the same result.
- PATCH: Apply a partial modification to a resource.
- DELETE: Remove the resource at the specified URI.
- HEAD: Identical to GET, but the server returns only headers without a body. Useful for checking resource existence or metadata.
- OPTIONS: Returns the HTTP methods supported by the server for a given URI. Used heavily in CORS preflight requests.
# GET request with curl
$ curl -v http://www.example.com/
# POST request with JSON body
$ curl -X POST -H "Content-Type: application/json"
-d '{"name":"test"}' http://api.example.com/items
# HEAD request — headers only
$ curl -I http://www.example.com/
# OPTIONS request
$ curl -X OPTIONS -v http://api.example.com/
HTTP Status Codes
HTTP status codes are three-digit numbers that indicate the outcome of a request. They are grouped into five classes:
- 1xx Informational: The request was received and processing continues (e.g., 100 Continue, 101 Switching Protocols).
- 2xx Success: The request was successfully received, understood, and accepted (e.g., 200 OK, 201 Created, 204 No Content).
- 3xx Redirection: Further action is needed to complete the request (e.g., 301 Moved Permanently, 302 Found, 304 Not Modified).
- 4xx Client Error: The request contains an error on the client side (e.g., 400 Bad Request, 401 Unauthorized, 403 Forbidden, 404 Not Found).
- 5xx Server Error: The server failed to fulfill a valid request (e.g., 500 Internal Server Error, 502 Bad Gateway, 503 Service Unavailable).
# Check status code only
$ curl -o /dev/null -s -w "%{http_code}n" http://www.example.com/
200
# Follow redirects and show each hop
$ curl -v -L http://example.com/ 2>&1 | grep "< HTTP"
< HTTP/1.1 301 Moved Permanently
< HTTP/1.1 200 OK
HTTP Headers
Headers carry metadata about the request or response. They are key-value pairs separated by a colon. Important headers include:
- Host: Required in HTTP/1.1. Specifies the target host, enabling virtual hosting where a single IP serves multiple domains.
- Content-Type: Indicates the media type of the body (e.g.,
text/html,application/json). - Content-Length: The size of the body in bytes.
- Authorization: Carries authentication credentials (e.g., Bearer tokens, Basic auth).
- Cache-Control: Directives for caching mechanisms (e.g.,
no-cache,max-age=3600). - Connection: Controls whether the network connection stays open after the current transaction (
keep-aliveorclose). - Set-Cookie / Cookie: Used to manage stateful sessions between client and server.
# View response headers
$ curl -I http://www.example.com/
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Content-Length: 1256
Connection: keep-alive
Cache-Control: max-age=604800
# Send custom headers
$ curl -H "Authorization: Bearer mytoken123"
-H "Accept: application/json"
http://api.example.com/data
HTTP Versions
HTTP has evolved through several versions, each improving performance and capabilities:
- HTTP/1.0 (1996): Each request required a separate TCP connection. No persistent connections by default.
- HTTP/1.1 (1997): Introduced persistent connections (keep-alive), chunked transfer encoding, and the Host header for virtual hosting. Still the most widely deployed version.
- HTTP/2 (2015): Binary framing, multiplexing (multiple requests over a single connection), header compression (HPACK), and server push. Significantly improves page load times.
- HTTP/3 (2022): Uses QUIC (UDP-based) instead of TCP, reducing connection latency and improving performance on lossy networks.
# Check HTTP/2 support
$ curl -I --http2 -s https://www.example.com/ | head -1
HTTP/2 200
# Check which HTTP version a server supports
$ curl -sI -o /dev/null -w "%{http_version}n" https://www.example.com/
2
Inspecting HTTP Traffic on Ubuntu
Ubuntu provides several command-line tools for inspecting and debugging HTTP traffic:
# curl — the Swiss Army knife of HTTP
$ curl -v https://www.example.com/ 2>&1 | head -30
# wget — download files via HTTP
$ wget -S http://www.example.com/file.tar.gz
# httpie — a more user-friendly HTTP client (install with apt)
$ sudo apt install httpie
$ http GET https://api.example.com/data
# tcpdump — capture raw HTTP packets on port 80
$ sudo tcpdump -i eth0 -A port 80 -c 20
# ngrep — grep for network traffic
$ sudo apt install ngrep
$ sudo ngrep -q -W byline "GET|POST" tcp port 80
For HTTPS traffic, tcpdump and ngrep will only show encrypted data. Use curl -v or browser developer tools for inspecting TLS-encrypted requests.
Key Takeaways
- HTTP is a stateless, text-based protocol operating at OSI Layer 7 over TCP (or QUIC for HTTP/3).
- Every HTTP transaction consists of a request (method, URI, headers, optional body) and a response (status code, headers, body).
- Common methods include GET, POST, PUT, DELETE, and HEAD — each with specific semantics.
- Status codes are grouped into five classes: 1xx informational, 2xx success, 3xx redirection, 4xx client error, and 5xx server error.
- Headers carry metadata that controls caching, authentication, content negotiation, and connection management.
- HTTP/2 and HTTP/3 improve performance through multiplexing, header compression, and reduced latency.
- Use
curl -v,wget -S, andtcpdumpto inspect HTTP traffic from the Ubuntu command line.
What's Next
In Lesson 22, you will learn how to install, configure, and manage the Apache2 web server on Ubuntu — the world's most widely deployed HTTP server. You will explore virtual hosts, modules, access control, and performance tuning.
繁體中文
概述
- 您將學到: HTTP 協定架構、請求/回應生命週期、HTTP 方法、狀態碼、標頭,以及如何從命令列檢查 HTTP 流量。
- 先決條件: 模組 2 — TCP/IP 與網路(第 11–20 課)
- 預估閱讀時間: 15 分鐘
簡介
超文本傳輸協定(HTTP)是全球資訊網上資料通訊的基礎。每當瀏覽器載入網頁、提交表單或取得 API 回應時,HTTP 就是管理請求傳送方式和伺服器回應方式的協定。作為 Linux 系統管理員,深入理解 HTTP 對於設定網頁伺服器、排除連線問題和保護網頁應用程式至關重要。
HTTP 運作在 OSI 模型的應用層(第 7 層),在 TCP 之上運行。它遵循簡單的客戶端-伺服器模型:客戶端(通常是瀏覽器或命令列工具)發送請求,伺服器返回回應。儘管這種方式很簡單,HTTP 已經有了顯著的發展——從早期的 HTTP/0.9 規範到現代的 HTTP/2 和 HTTP/3 協定。
HTTP 請求/回應模型
HTTP 通訊遵循無狀態的請求/回應循環。客戶端建立與伺服器的連線,發送請求訊息,伺服器返回回應訊息。每個交易都是獨立的——伺服器本身不記住先前的請求(儘管 Cookie 和工作階段可以在此之上增加狀態性)。
HTTP 請求結構
GET /index.html HTTP/1.1
Host: www.example.com
User-Agent: curl/7.81.0
Accept: text/html
HTTP 回應結構
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Content-Length: 1234
Server: Apache/2.4.52
HTTP 方法
- GET: 取得資源。GET 請求應該是安全且冪等的。
- POST: 向伺服器提交資料,通常用於建立新資源。
- PUT: 替換指定 URI 的整個資源。
- PATCH: 對資源進行部分修改。
- DELETE: 刪除指定 URI 的資源。
- HEAD: 與 GET 相同,但伺服器只返回標頭。
- OPTIONS: 返回伺服器對指定 URI 支援的 HTTP 方法。
# 使用 curl 的 GET 請求
$ curl -v http://www.example.com/
# 帶有 JSON 主體的 POST 請求
$ curl -X POST -H "Content-Type: application/json"
-d '{"name":"test"}' http://api.example.com/items
# HEAD 請求 — 僅標頭
$ curl -I http://www.example.com/
HTTP 狀態碼
- 1xx 資訊性: 請求已接收,處理繼續中。
- 2xx 成功: 請求已成功接收、理解和接受(例如:200 OK、201 Created)。
- 3xx 重新導向: 需要進一步操作才能完成請求(例如:301、302)。
- 4xx 客戶端錯誤: 請求包含客戶端錯誤(例如:400、401、403、404)。
- 5xx 伺服器錯誤: 伺服器無法完成有效請求(例如:500、502、503)。
HTTP 標頭
標頭攜帶關於請求或回應的元資料,是以冒號分隔的鍵值對。重要的標頭包括 Host、Content-Type、Content-Length、Authorization、Cache-Control 和 Cookie。
# 查看回應標頭
$ curl -I http://www.example.com/
# 發送自訂標頭
$ curl -H "Authorization: Bearer mytoken123"
-H "Accept: application/json"
http://api.example.com/data
HTTP 版本
- HTTP/1.0(1996): 每個請求需要單獨的 TCP 連線。
- HTTP/1.1(1997): 引入持久連線、分塊傳輸編碼和 Host 標頭。
- HTTP/2(2015): 二進位幀、多路復用、標頭壓縮和伺服器推送。
- HTTP/3(2022): 使用 QUIC(基於 UDP)取代 TCP。
在 Ubuntu 上檢查 HTTP 流量
# curl — HTTP 的瑞士刀
$ curl -v https://www.example.com/
# tcpdump — 擷取 80 埠的原始 HTTP 封包
$ sudo tcpdump -i eth0 -A port 80 -c 20
# ngrep — 網路流量的 grep
$ sudo ngrep -q -W byline "GET|POST" tcp port 80
重點整理
- HTTP 是一個無狀態的、基於文字的協定,運作在 OSI 第 7 層。
- 每個 HTTP 交易由請求和回應組成。
- 常用方法包括 GET、POST、PUT、DELETE 和 HEAD。
- 狀態碼分為五個類別:1xx 資訊性、2xx 成功、3xx 重新導向、4xx 客戶端錯誤和 5xx 伺服器錯誤。
- 使用
curl -v、wget -S和tcpdump從 Ubuntu 命令列檢查 HTTP 流量。
下一步
在第 22 課中,您將學習如何在 Ubuntu 上安裝、設定和管理 Apache2 網頁伺服器——世界上部署最廣泛的 HTTP 伺服器。
日本語
概要
- 学習内容: HTTP プロトコルのアーキテクチャ、リクエスト/レスポンスのライフサイクル、HTTP メソッド、ステータスコード、ヘッダー、およびコマンドラインからの HTTP トラフィック検査方法。
- 前提条件: モジュール 2 — TCP/IP とネットワーキング(レッスン 11–20)
- 推定読了時間: 15 分
はじめに
ハイパーテキスト転送プロトコル(HTTP)は、World Wide Web におけるデータ通信の基盤です。ブラウザがウェブページを読み込んだり、フォームを送信したり、API レスポンスを取得したりするたびに、HTTP がリクエストの送信方法とサーバーの応答方法を管理します。Linux システム管理者として HTTP を深く理解することは、ウェブサーバーの設定、接続問題のトラブルシューティング、ウェブアプリケーションのセキュリティ確保に不可欠です。
HTTP は OSI モデルのアプリケーション層(レイヤー 7)で動作し、TCP 上で実行されます。シンプルなクライアント-サーバーモデルに従います:クライアント(通常はブラウザまたはコマンドラインツール)がリクエストを送信し、サーバーがレスポンスを返します。
HTTP リクエスト/レスポンスモデル
HTTP 通信はステートレスなリクエスト/レスポンスサイクルに従います。各トランザクションは独立しており、サーバーは以前のリクエストを本質的に記憶しません。
HTTP リクエストの構造
GET /index.html HTTP/1.1
Host: www.example.com
User-Agent: curl/7.81.0
Accept: text/html
HTTP レスポンスの構造
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Content-Length: 1234
Server: Apache/2.4.52
HTTP メソッド
- GET: リソースを取得。安全かつ冪等であるべき。
- POST: サーバーにデータを送信(通常はリソースの作成)。
- PUT: 指定された URI のリソース全体を置換。
- PATCH: リソースに部分的な変更を適用。
- DELETE: 指定された URI のリソースを削除。
- HEAD: GET と同じだがボディなしでヘッダーのみ返す。
- OPTIONS: サーバーがサポートする HTTP メソッドを返す。
# curl での GET リクエスト
$ curl -v http://www.example.com/
# JSON ボディ付き POST リクエスト
$ curl -X POST -H "Content-Type: application/json"
-d '{"name":"test"}' http://api.example.com/items
# HEAD リクエスト — ヘッダーのみ
$ curl -I http://www.example.com/
HTTP ステータスコード
- 1xx 情報: リクエスト受信、処理継続中。
- 2xx 成功: リクエストが正常に受信・理解・受け入れられた。
- 3xx リダイレクション: リクエスト完了にはさらなるアクションが必要。
- 4xx クライアントエラー: リクエストにクライアント側のエラーがある。
- 5xx サーバーエラー: サーバーが有効なリクエストの処理に失敗。
HTTP ヘッダー
ヘッダーはリクエストまたはレスポンスに関するメタデータを運びます。重要なヘッダーには Host、Content-Type、Content-Length、Authorization、Cache-Control、Cookie があります。
HTTP バージョン
- HTTP/1.0(1996): 各リクエストに個別の TCP 接続が必要。
- HTTP/1.1(1997): 持続的接続、チャンク転送エンコーディング、Host ヘッダーを導入。
- HTTP/2(2015): バイナリフレーミング、多重化、ヘッダー圧縮、サーバープッシュ。
- HTTP/3(2022): TCP の代わりに QUIC(UDP ベース)を使用。
Ubuntu での HTTP トラフィック検査
# curl — HTTP のスイスアーミーナイフ
$ curl -v https://www.example.com/
# tcpdump — ポート 80 の生パケットをキャプチャ
$ sudo tcpdump -i eth0 -A port 80 -c 20
# ngrep — ネットワークトラフィックの grep
$ sudo ngrep -q -W byline "GET|POST" tcp port 80
重要ポイント
- HTTP は OSI レイヤー 7 で動作するステートレスなテキストベースのプロトコル。
- すべての HTTP トランザクションはリクエストとレスポンスで構成される。
- 一般的なメソッドには GET、POST、PUT、DELETE、HEAD がある。
- ステータスコードは 5 つのクラスに分類される。
curl -v、wget -S、tcpdumpを使用して HTTP トラフィックを検査できる。
次のステップ
レッスン 22 では、Ubuntu に Apache2 ウェブサーバーをインストール、設定、管理する方法を学びます。