Back to Skills

configure-nginx

pjt222
Updated 2 days ago
5 views
17
2
17
View on GitHub
Othergeneral

About

This Claude Skill configures Nginx as a web server and reverse proxy for production use. It handles static file serving, reverse proxying to backend services, SSL/TLS termination with Let's Encrypt, and load balancing. It also adds security enhancements like rate limiting and security headers.

Quick Install

Claude Code

Recommended
Primary
npx skills add pjt222/agent-almanac -a claude-code
Plugin CommandAlternative
/plugin add https://github.com/pjt222/agent-almanac
Git CloneAlternative
git clone https://github.com/pjt222/agent-almanac.git ~/.claude/skills/configure-nginx

Copy and paste this command in Claude Code to install this skill

Documentation


name: configure-nginx description: > NginxをWebサーバーおよびリバースプロキシとして設定する。静的ファイル配信、アップストリームサービスへの リバースプロキシ、Let's EncryptによるSSL/TLS終端、locationブロック、ロードバランシング、 レート制限、およびセキュリティヘッダーをカバーする。本番環境での静的ファイル配信、バックエンド サービス(Node.js、Python、R/Shiny)へのリバースプロキシ、SSL/TLSの終端、インスタンス間の ロードバランシング、またはエンドポイント強化のためのレート制限とセキュリティヘッダーの追加に使用する。 license: MIT allowed-tools: Read Write Edit Bash Grep Glob metadata: author: Philipp Thoss version: "1.0" domain: containerization complexity: intermediate language: multi tags: nginx, reverse-proxy, ssl, tls, lets-encrypt, web-server, security-headers locale: ja source_locale: en source_commit: 6f65f316 translator: claude-sonnet-4-6 translation_date: 2026-03-16

Nginxの設定

SSL終端とセキュリティ強化を備えたWebサーバーおよびリバースプロキシとしてNginxをセットアップする。

使用タイミング

  • 本番環境での静的ファイル(HTML、CSS、JS)の配信
  • バックエンドサービス(Node.js、Python、Go、R/Shiny)へのリバースプロキシ
  • Let's Encrypt証明書によるSSL/TLSの終端
  • 複数のバックエンドインスタンスへのロードバランシング
  • レート制限とセキュリティヘッダーの追加

入力

  • 必須: デプロイ先(Dockerコンテナまたはベアメタル)
  • 必須: プロキシ先のバックエンドサービス(host:port)
  • 任意: SSL用ドメイン名
  • 任意: 静的ファイルディレクトリ

手順

ステップ1: 基本リバースプロキシ

nginx.conf:

events {
    worker_connections 1024;
}

http {
    upstream app {
        server app:3000;
    }

    server {
        listen 80;
        server_name example.com;

        location / {
            proxy_pass http://app;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }
}

Docker Composeサービス:

services:
  nginx:
    image: nginx:1.27-alpine
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
    depends_on:
      - app

期待結果: ポート80へのリクエストがappサービスに転送される。

ステップ2: 静的ファイル配信

server {
    listen 80;
    root /usr/share/nginx/html;
    index index.html;

    location / {
        try_files $uri $uri/ /index.html;
    }

    location /assets/ {
        expires 1y;
        add_header Cache-Control "public, immutable";
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff2?)$ {
        expires 6M;
        add_header Cache-Control "public";
    }
}

ステップ3: Let's EncryptによるSSL/TLS

certbotのwebrootメソッドを使用する:

server {
    listen 80;
    server_name example.com;

    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }

    location / {
        return 301 https://$host$request_uri;
    }
}

server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;

    location / {
        proxy_pass http://app;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

certbot付きDocker Compose:

services:
  nginx:
    image: nginx:1.27-alpine
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
      - certbot-webroot:/var/www/certbot:ro
      - certbot-certs:/etc/letsencrypt:ro

  certbot:
    image: certbot/certbot
    volumes:
      - certbot-webroot:/var/www/certbot
      - certbot-certs:/etc/letsencrypt

volumes:
  certbot-webroot:
  certbot-certs:

初回証明書の取得:

docker compose run --rm certbot certonly \
  --webroot -w /var/www/certbot \
  -d example.com --email [email protected] --agree-tos

期待結果: 有効なLet's Encrypt証明書でHTTPSが動作する。

失敗時: DNSがサーバーを指していることを確認する。ACMEチャレンジのためにポート80が開いていることを確認する。

ステップ4: セキュリティヘッダー

server {
    # ... 上記のSSL設定 ...

    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;
    add_header Strict-Transport-Security "max-age=63072000; includeSubDomains" always;
    add_header Content-Security-Policy "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline';" always;

    # Nginxバージョンを隠す
    server_tokens off;
}

ステップ5: レート制限

http {
    # レート制限ゾーンの定義
    limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s;
    limit_req_zone $binary_remote_addr zone=login:10m rate=1r/s;

    server {
        location /api/ {
            limit_req zone=api burst=20 nodelay;
            proxy_pass http://app;
        }

        location /login {
            limit_req zone=login burst=5;
            proxy_pass http://app;
        }
    }
}

ステップ6: ロードバランシング

upstream app {
    least_conn;
    server app1:3000;
    server app2:3000;
    server app3:3000 backup;
}
メソッドディレクティブ動作
ラウンドロビン(デフォルト)均等分配
最少接続least_conn最も負荷の少ないサーバーにルーティング
IPハッシュip_hashスティッキーセッション
重み付きserver app:3000 weight=3比例配分

ステップ7: 設定のテスト

# 設定構文のテスト
docker compose exec nginx nginx -t

# ダウンタイムなしでリロード
docker compose exec nginx nginx -s reload

# レスポンスヘッダーの確認
curl -I https://example.com

期待結果: nginx -tが構文OKを報告する。ヘッダーにセキュリティヘッダーが含まれる。

バリデーション

  • nginx -tが設定が有効であると報告する
  • HTTPがHTTPSにリダイレクトされる(SSL有効時)
  • バックエンドサービスがプロキシ経由で到達可能
  • レスポンスにセキュリティヘッダーが存在する
  • 過剰なリクエストでレート制限が発動する
  • SSL Labsテストでa+評価を取得する(公開の場合)

よくある落とし穴

  • proxy_set_header Hostの不足: バックエンドが誤ったHostヘッダーを受信し、バーチャルホストとリダイレクトが壊れる
  • locationの順序が重要: Nginxは最も具体的なマッチを使用する。完全一致(=)> プレフィックス(^~)> 正規表現(~)> 一般プレフィックス
  • SSL証明書の更新: certbot renewを実行してNginxをリロードするcronまたはタイマーをセットアップする
  • 大きなリクエストボディ: デフォルトのclient_max_body_sizeは1MB。ファイルアップロード用に増やす: client_max_body_size 50m;
  • WebSocketプロキシ: 追加ヘッダーが必要。パターンはconfigure-reverse-proxyを参照

関連スキル

  • configure-reverse-proxy - WebSocketやTraefikを含むマルチツールプロキシパターン
  • setup-compose-stack - Nginxを含むcomposeスタック
  • deploy-searxng - SearXNGのフロントエンドとしてNginxを使用
  • configure-ingress-networking - Kubernetes Ingress(NGINX Ingress Controller)

GitHub Repository

pjt222/agent-almanac
Path: i18n/ja/skills/configure-nginx
0
agentsagentskillsai-assisted-developmentclaude-codeskillsteams

Related Skills

llamaguard

Other

LlamaGuard is Meta's 7-8B parameter model for moderating LLM inputs and outputs across six safety categories like violence and hate speech. It offers 94-95% accuracy and can be deployed using vLLM, Hugging Face, or Amazon SageMaker. Use this skill to easily integrate content filtering and safety guardrails into your AI applications.

View skill

cost-optimization

Other

This Claude Skill helps developers optimize cloud costs through resource rightsizing, tagging strategies, and spending analysis. It provides a framework for reducing cloud expenses and implementing cost governance across AWS, Azure, and GCP. Use it when you need to analyze infrastructure costs, right-size resources, or meet budget constraints.

View skill

quantizing-models-bitsandbytes

Other

This skill quantizes LLMs to 8-bit or 4-bit precision using bitsandbytes, achieving 50-75% memory reduction with minimal accuracy loss. It's ideal for running larger models on limited GPU memory or accelerating inference, supporting formats like INT8, NF4, and FP4. The skill integrates with HuggingFace Transformers and enables QLoRA training and 8-bit optimizers.

View skill

dispatching-parallel-agents

Other

This Claude Skill dispatches multiple agents to investigate and fix 3+ independent problems concurrently. It is designed for scenarios involving unrelated failures that can be resolved without shared state or dependencies. The core capability is parallel problem-solving, assigning one agent per independent problem domain to maximize efficiency.

View skill