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基本
2curl https://example.com
3curl -I https://example.com # ヘッダーのみ
4curl -v https://example.com # 詳細表示
5
6# メソッド指定
7curl -X POST https://api.example.com/users \
8 -H "Content-Type: application/json" \
9 -d '{"name": "John"}'
10
11# 認証
12curl -H "Authorization: Bearer token123" https://api.example.com
13
14# HTTP/2強制
15curl --http2 https://example.com
16
17# レスポンスタイム測定
18curl -w "time_total: %{time_total}s\n" -o /dev/null -s https://example.com
19
20# 証明書情報
21openssl s_client -connect example.com:443 -servername example.com

SRE/インフラ観点

監視項目

  • ステータスコード分布: 4xx/5xxエラー率
  • レイテンシ: P50/P90/P99応答時間
  • スループット: リクエスト/秒
  • エラー率: SLI/SLO指標

パフォーマンス最適化

  • • HTTP/2有効化
  • • 圧縮(gzip, brotli)
  • • CDNでのキャッシュ
  • • Keep-Alive設定