スキル一覧に戻る

configure-nginx

pjt222
更新日 Yesterday
4 閲覧
17
2
17
GitHubで表示
その他general

について

このスキルは、本番環境向けにNginxをWebサーバーおよびリバースプロキシとして設定します。静的ファイル配信、Let's EncryptによるSSL/TLS終端、ロードバランシング、Node.jsやPythonなどのバックエンドサービスへのプロキシ機能を有効化します。開発者はこれを使用して、アプリケーションにレート制限、セキュリティヘッダー、セキュアなエンドポイントを追加します。

クイックインストール

Claude Code

推奨
メイン
npx skills add pjt222/agent-almanac -a claude-code
プラグインコマンド代替
/plugin add https://github.com/pjt222/agent-almanac
Git クローン代替
git clone https://github.com/pjt222/agent-almanac.git ~/.claude/skills/configure-nginx

このコマンドをClaude Codeにコピー&ペーストしてスキルをインストールします

ドキュメント

配置 Nginx

设置 Nginx 作为具有 SSL 终止和安全加固的 Web 服务器和反向代理。

适用场景

  • 在生产环境中提供静态文件(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;
    }
}

Docker Compose 配合 certbot:

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

预期结果: HTTPS 使用有效的 Let's Encrypt 证书正常工作。

失败处理: 检查 DNS 是否指向服务器。验证端口 80 是否对 ACME 挑战开放。

第 4 步:安全头

server {
    # ... SSL config above ...

    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;

    # Hide Nginx version
    server_tokens off;
}

第 5 步:速率限制

http {
    # Define rate limit zones
    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 步:测试配置

# Test config syntax
docker compose exec nginx nginx -t

# Reload without downtime
docker compose exec nginx nginx -s reload

# Check response headers
curl -I https://example.com

预期结果: nginx -t 报告语法正确。头部包含安全头。

验证清单

  • nginx -t 报告配置有效
  • HTTP 重定向到 HTTPS(如启用 SSL)
  • 后端服务可通过代理访问
  • 响应中存在安全头
  • 速率限制在请求过多时触发
  • SSL Labs 测试获得 A+ 评级(如公开访问)

常见问题

  • 缺少 proxy_set_header Host:后端接收到错误的主机头,导致虚拟主机和重定向失效
  • location 顺序很重要:Nginx 使用最具体的匹配。精确匹配(=)> 前缀匹配(^~)> 正则匹配(~)> 一般前缀
  • SSL 证书续期:设置 cron 或 timer 运行 certbot renew 并重载 Nginx
  • 大请求体:默认 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 — 使用 Nginx 作为 SearXNG 的前端
  • configure-ingress-networking — Kubernetes 入口(NGINX Ingress Controller)

GitHub リポジトリ

pjt222/agent-almanac
パス: i18n/zh-CN/skills/configure-nginx
0
agentsagentskillsai-assisted-developmentclaude-codeskillsteams

関連スキル

llamaguard

その他

LlamaGuardは、暴力やヘイトスピーチなど6つの安全性カテゴリーにおいて、LLMの入力と出力をモデレートするMetaの70-80億パラメータモデルです。94〜95%の精度を提供し、vLLM、Hugging Face、Amazon SageMakerを使用してデプロイ可能です。このスキルを使用して、AIアプリケーションにコンテンツフィルタリングと安全策を簡単に統合できます。

スキルを見る

cost-optimization

その他

このClaudeスキルは、リソースの適正サイジング、タグ付け戦略、支出分析を通じて、開発者がクラウドコストを最適化することを支援します。AWS、Azure、GCPにわたるクラウド支出の削減とコストガバナンスの実施のためのフレームワークを提供します。インフラコストの分析、リソースの適正サイジング、または予算制約への対応が必要な際にご利用ください。

スキルを見る

quantizing-models-bitsandbytes

その他

このスキルは、bitsandbytesを使用してLLMを8ビットまたは4ビット精度に量子化し、精度の低下を最小限に抑えつつ50〜75%のメモリ削減を実現します。限られたGPUメモリでより大規模なモデルを実行したり、推論を高速化するのに理想的で、INT8、NF4、FP4などのフォーマットをサポートしています。HuggingFace Transformersと統合され、QLoRAトレーニングや8ビットオプティマイザーを可能にします。

スキルを見る

dispatching-parallel-agents

その他

このClaudeスキルは、複数のエージェントを配備し、3つ以上の独立した問題を並行して調査・修正します。共有状態や依存関係がなく解決可能な、無関係な障害が発生するシナリオ向けに設計されています。中核となる機能は並列問題解決であり、効率を最大化するために独立した問題領域ごとに1つのエージェントを割り当てます。

スキルを見る