HTTP/HTTPS
Webの基盤プロトコル
HTTPとは
HTTP(HyperText Transfer Protocol)は、WebブラウザとWebサーバー間でハイパーテキストを転送するプロトコルです。 リクエスト/レスポンスモデルで動作し、ステートレスな設計です。 HTTPSはTLS/SSLで暗号化されたセキュアなバージョンです。
HTTPリクエスト/レスポンス
リクエスト
GET /api/users HTTP/1.1 Host: example.com User-Agent: Mozilla/5.0 Accept: application/json Authorization: Bearer token123
レスポンス
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 42
Cache-Control: max-age=3600
{"id": 1, "name": "User"}HTTPメソッド
| メソッド | 用途 | 特性 |
|---|---|---|
| GET | リソース取得 | 安全、冪等、キャッシュ可 |
| POST | リソース作成、データ送信 | 非安全、非冪等 |
| PUT | リソース置換 | 非安全、冪等 |
| PATCH | リソース部分更新 | 非安全、非冪等 |
| DELETE | リソース削除 | 非安全、冪等 |
| HEAD | ヘッダのみ取得 | 安全、冪等 |
| OPTIONS | 対応メソッド確認 | 安全、冪等(CORS preflight) |
HTTPステータスコード
1xx 情報100 Continue, 101 Switching Protocols
2xx 成功200 OK, 201 Created, 204 No Content
3xx リダイレクト301 Moved Permanently, 302 Found, 304 Not Modified
4xx クライアントエラー400 Bad Request, 401 Unauthorized, 403 Forbidden, 404 Not Found, 429 Too Many Requests
5xx サーバーエラー500 Internal Server Error, 502 Bad Gateway, 503 Service Unavailable, 504 Gateway Timeout
HTTPバージョンの進化
HTTP/1.1
- • Keep-Alive(コネクション再利用)
- • パイプライン(実用上は問題あり)
- • Head-of-Line Blocking問題
HTTP/2
- • バイナリプロトコル(効率的)
- • 多重化(1接続で複数リクエスト並列)
- • ヘッダー圧縮(HPACK)
- • サーバープッシュ
HTTP/3
- • QUIC(UDP上)で動作
- • 0-RTT接続(再接続高速化)
- • 改善されたHoL Blocking耐性
- • コネクションマイグレーション
重要なHTTPヘッダー
キャッシュ制御
- •
Cache-Control: max-age, no-cache, no-store - •
ETag: リソースのバージョン - •
Last-Modified: 最終更新日時
セキュリティ
- •
Strict-Transport-Security: HSTS - •
Content-Security-Policy: CSP - •
X-Content-Type-Options: nosniff
CORS
- •
Access-Control-Allow-Origin - •
Access-Control-Allow-Methods - •
Access-Control-Allow-Headers
その他
- •
Content-Type: メディアタイプ - •
Authorization: 認証情報 - •
X-Forwarded-For: 元クライアントIP
HTTPデバッグ
1 # curl基本 2 curl https://example.com 3 curl -I https://example.com # ヘッダーのみ 4 curl -v https://example.com # 詳細表示 5 6 # メソッド指定 7 curl -X POST https://api.example.com/users \ 8 -H "Content-Type: application/json" \ 9 -d '{"name": "John"}' 10 11 # 認証 12 curl -H "Authorization: Bearer token123" https://api.example.com 13 14 # HTTP/2強制 15 curl --http2 https://example.com 16 17 # レスポンスタイム測定 18 curl -w "time_total: %{time_total}s\n" -o /dev/null -s https://example.com 19 20 # 証明書情報 21 openssl s_client -connect example.com:443 -servername example.com
SRE/インフラ観点
監視項目
- • ステータスコード分布: 4xx/5xxエラー率
- • レイテンシ: P50/P90/P99応答時間
- • スループット: リクエスト/秒
- • エラー率: SLI/SLO指標
パフォーマンス最適化
- • HTTP/2有効化
- • 圧縮(gzip, brotli)
- • CDNでのキャッシュ
- • Keep-Alive設定