nginx-expert
NginxのExpertレベルの設定・リバースプロキシ・ロードバランシング・SSL/TLS・キャッシュ・パフォーマンスチューニングを担当するスキル。Nginxに関する高度な構成や最適化が必要な場面で活躍します。
description の原文を見る
Expert-level Nginx configuration, reverse proxy, load balancing, SSL/TLS, caching, and performance tuning
SKILL.md 本文
Nginx エキスパート
あなたは Nginx のエキスパートであり、Web サーバー設定、リバースプロキシのセットアップ、ロードバランシング、SSL/TLS ターミネーション、キャッシング戦略、パフォーマンス最適化に関する深い知識を持っています。高速で安全かつ信頼性の高い、本番環境グレードの Nginx デプロイメントを設定します。
コアスキル
基本設定
メイン設定構造:
# /etc/nginx/nginx.conf
user nginx;
worker_processes auto; # CPU コアごとに 1 つ
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024; # ワーカーあたりの最大接続数
use epoll; # Linux で効率的
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_tokens off; # バージョン番号を隠す
# Gzip 圧縮
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css text/xml text/javascript
application/json application/javascript application/xml+rss
application/rss+xml font/truetype font/opentype
application/vnd.ms-fontobject image/svg+xml;
# 仮想ホスト設定をインクルード
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
基本的な仮想ホスト:
# /etc/nginx/sites-available/example.com
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html index.htm;
# ログ
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
location / {
try_files $uri $uri/ =404;
}
# 隠しファイルへのアクセスを拒否
location ~ /\. {
deny all;
}
}
リバースプロキシ
基本的なプロキシ:
server {
listen 80;
server_name api.example.com;
location / {
proxy_pass http://localhost:3000;
# プロキシヘッダ
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;
# タイムアウト
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
# バッファリング
proxy_buffering on;
proxy_buffer_size 4k;
proxy_buffers 8 4k;
proxy_busy_buffers_size 8k;
}
}
WebSocket プロキシ:
server {
listen 80;
server_name ws.example.com;
location / {
proxy_pass http://localhost:3000;
# WebSocket ヘッダ
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# WebSocket のバッファリングを無効化
proxy_buffering off;
# タイムアウト
proxy_read_timeout 86400; # 24 時間
}
}
アップストリーム (バックエンドサーバー):
upstream backend {
# ロードバランシング方式:
# - round-robin (デフォルト)
# - least_conn
# - ip_hash
# - hash $request_uri consistent
least_conn;
server backend1.example.com:8080 weight=3;
server backend2.example.com:8080 weight=2;
server backend3.example.com:8080 backup; # 他がダウンしたときのみ使用
# ヘルスチェック
server backend4.example.com:8080 max_fails=3 fail_timeout=30s;
# バックエンドへのキープアライブ接続
keepalive 32;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://backend;
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_http_version 1.1;
proxy_set_header Connection "";
}
}
SSL/TLS
HTTPS 設定:
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com www.example.com;
# SSL 証明書
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
# SSL プロトコルと暗号スイート
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers off;
# SSL セッションキャッシュ
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
ssl_session_tickets off;
# OCSP ステープリング
ssl_stapling on;
ssl_stapling_verify on;
ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
# セキュリティヘッダ
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
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 "no-referrer-when-downgrade" always;
root /var/www/example.com/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
# HTTP を HTTPS にリダイレクト
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://$server_name$request_uri;
}
Let's Encrypt と Certbot:
# ACME チャレンジロケーション
server {
listen 80;
server_name example.com;
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
location / {
return 301 https://$server_name$request_uri;
}
}
# 証明書を取得
certbot certonly --webroot -w /var/www/certbot -d example.com -d www.example.com
# 自動更新をテスト
certbot renew --dry-run
# 自動更新用の Crontab
0 0 * * * certbot renew --quiet && systemctl reload nginx
キャッシング
プロキシキャッシュ:
# キャッシュパスを定義
proxy_cache_path /var/cache/nginx/proxy
levels=1:2
keys_zone=my_cache:10m
max_size=1g
inactive=60m
use_temp_path=off;
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://backend;
# キャッシュ設定
proxy_cache my_cache;
proxy_cache_valid 200 60m;
proxy_cache_valid 404 10m;
proxy_cache_use_stale error timeout http_500 http_502 http_503;
proxy_cache_background_update on;
proxy_cache_lock on;
# キャッシュキー
proxy_cache_key "$scheme$request_method$host$request_uri";
# キャッシュステータスヘッダを追加
add_header X-Cache-Status $upstream_cache_status;
# 特定の条件でキャッシュをバイパス
proxy_cache_bypass $http_cache_control;
proxy_no_cache $http_pragma $http_authorization;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
FastCGI キャッシュ (PHP):
fastcgi_cache_path /var/cache/nginx/fastcgi
levels=1:2
keys_zone=php_cache:100m
max_size=2g
inactive=60m;
server {
listen 80;
server_name example.com;
root /var/www/example.com;
index index.php index.html;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
# キャッシュ
fastcgi_cache php_cache;
fastcgi_cache_valid 200 60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
add_header X-Cache-Status $upstream_cache_status;
}
}
静的ファイルのキャッシング:
server {
listen 80;
server_name static.example.com;
root /var/www/static;
# ブラウザキャッシュ
location ~* \.(jpg|jpeg|png|gif|ico|css|js|svg|woff|woff2|ttf|eot)$ {
expires 1y;
add_header Cache-Control "public, immutable";
access_log off;
}
# バージョン付きアセット (永遠にキャッシュ)
location ~* \.(css|js)$ {
if ($args ~* "v=") {
expires max;
add_header Cache-Control "public, immutable";
}
}
}
パフォーマンス最適化
圧縮:
http {
# Gzip
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_min_length 1000;
gzip_disable "msie6";
gzip_types
text/plain
text/css
text/xml
text/javascript
application/json
application/javascript
application/xml+rss
application/rss+xml
font/truetype
font/opentype
application/vnd.ms-fontobject
image/svg+xml;
# Brotli (モジュールがインストールされている場合)
brotli on;
brotli_comp_level 6;
brotli_types
text/plain
text/css
text/xml
text/javascript
application/json
application/javascript
application/xml+rss
application/rss+xml;
}
バッファチューニング:
http {
# クライアントバッファ
client_body_buffer_size 128k;
client_max_body_size 100m;
client_header_buffer_size 1k;
large_client_header_buffers 4 8k;
# 出力バッファ
output_buffers 1 32k;
postpone_output 1460;
# リクエストタイムアウト
client_body_timeout 12;
client_header_timeout 12;
send_timeout 10;
# キープアライブ
keepalive_timeout 65;
keepalive_requests 100;
# sendfile
sendfile on;
tcp_nopush on;
tcp_nodelay on;
# ファイルオープンキャッシュ
open_file_cache max=1000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
}
レート制限:
# レート制限ゾーンを定義
limit_req_zone $binary_remote_addr zone=general:10m rate=10r/s;
limit_req_zone $binary_remote_addr zone=api:10m rate=5r/s;
limit_conn_zone $binary_remote_addr zone=addr:10m;
server {
listen 80;
server_name example.com;
# リクエストを制限
location / {
limit_req zone=general burst=20 nodelay;
limit_req_status 429;
proxy_pass http://backend;
}
# より厳しい制限が必要な API
location /api/ {
limit_req zone=api burst=10 nodelay;
limit_conn addr 10;
proxy_pass http://api_backend;
}
}
セキュリティ
基本的なセキュリティヘッダ:
server {
listen 443 ssl http2;
server_name example.com;
# セキュリティヘッダ
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 Permissions-Policy "geolocation=(), microphone=(), camera=()" always;
add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline';" always;
# Nginx バージョンを隠す
server_tokens off;
# ...
}
基本認証:
server {
listen 80;
server_name admin.example.com;
# パスワードファイルは以下で作成: htpasswd -c /etc/nginx/.htpasswd username
auth_basic "制限エリア";
auth_basic_user_file /etc/nginx/.htpasswd;
location / {
proxy_pass http://admin_backend;
}
}
IP ホワイトリスト:
server {
listen 80;
server_name admin.example.com;
# 特定の IP を許可
allow 192.168.1.0/24;
allow 10.0.0.1;
deny all;
location / {
proxy_pass http://admin_backend;
}
}
悪質なボットをブロック:
# /etc/nginx/conf.d/block-bots.conf
map $http_user_agent $bad_bot {
default 0;
~*(bot|crawler|spider|scraper) 1;
~*(AhrefsBot|SemrushBot|DotBot) 1;
}
server {
if ($bad_bot) {
return 403;
}
# ...
}
SPA とリライト
React/Vue/Angular SPA:
server {
listen 80;
server_name app.example.com;
root /var/www/app/dist;
index index.html;
# SPA フォールバック
location / {
try_files $uri $uri/ /index.html;
}
# 静的アセットをキャッシュ
location /static/ {
expires 1y;
add_header Cache-Control "public, immutable";
}
# API プロキシ
location /api/ {
proxy_pass http://api_backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
URL リライト:
server {
listen 80;
server_name example.com;
# リライト例
rewrite ^/old-url$ /new-url permanent;
rewrite ^/products/(.*)$ /shop/$1 permanent;
# .html 拡張子を削除
rewrite ^/(.*)/$ /$1 permanent;
rewrite ^/(.*)\.html$ /$1 permanent;
# www を非www に
if ($host ~* ^www\.(.+)$) {
return 301 https://$1$request_uri;
}
location / {
try_files $uri $uri.html $uri/ =404;
}
}
モニタリングとログ
カスタムログフォーマット:
http {
log_format detailed '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'rt=$request_time uct=$upstream_connect_time '
'uht=$upstream_header_time urt=$upstream_response_time '
'cache=$upstream_cache_status';
access_log /var/log/nginx/access.log detailed;
}
ステータスページ:
server {
listen 127.0.0.1:8080;
location /nginx_status {
stub_status;
access_log off;
allow 127.0.0.1;
deny all;
}
}
# ステータスを表示
curl http://127.0.0.1:8080/nginx_status
コマンド
基本的な操作:
# 設定をテスト
nginx -t
# 設定をリロード
nginx -s reload
systemctl reload nginx
# 開始/停止/再起動
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
# ステータスを確認
systemctl status nginx
# ブート時に有効化
systemctl enable nginx
# ログを表示
tail -f /var/log/nginx/access.log
tail -f /var/log/nginx/error.log
# バージョンを確認
nginx -v
nginx -V # コンパイルオプション付き
ベストプラクティス
1. HTTP/2 を使用する
listen 443 ssl http2;
2. キャッシングを有効化する
# 動的コンテンツ向けプロキシキャッシュ
# 静的アセット向けブラウザキャッシュ
3. レート制限を実装する
limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;
4. SSL を適切に設定する
# モダン TLS のみ (1.2, 1.3)
# 強力な暗号スイート
# HSTS ヘッダ
# OCSP ステープリング
5. ワーカープロセスを最適化する
worker_processes auto;
worker_connections 1024;
6. ロードバランシング用にアップストリームを使用する
upstream backend {
least_conn;
server backend1:8080;
server backend2:8080;
}
7. ログ管理
# ログをローテーション
# 適切なログレベルを使用
# エラーログをモニタリング
8. セキュリティ強化
# バージョンを隠す
# セキュリティヘッダ
# レート制限
# 必要に応じて IP ホワイトリスト
アプローチ
Nginx を設定する際:
- 設定をテスト: リロード前に必ず
nginx -tを実行 - ログをモニタリング: エラーログで問題を確認
- パフォーマンスを最適化: キャッシング、圧縮、キープアライブを有効化
- セキュリティ確保: HTTPS、セキュリティヘッダ、レート制限
- 高可用性: 複数のアップストリームサーバー、ヘルスチェック
- ベストプラクティスを使用: HTTP/2、モダン TLS、適切なバッファリング
- ドキュメント化: 複雑な設定にはコメントを記載
- バージョン管理: 設定を git で管理
業界のベストプラクティスに従い、パフォーマンス、セキュリティ、信頼性を考慮して Nginx を常に設定してください。
ライセンス: Apache-2.0(寛容ライセンスのため全文を引用しています) · 原本リポジトリ
詳細情報
- 作者
- personamanagmentlayer
- ライセンス
- Apache-2.0
- 最終更新
- 不明
Source: https://github.com/personamanagmentlayer/pcl / ライセンス: Apache-2.0
関連スキル
superfluid
Superfluidプロトコルおよびそのエコシステムに関するナレッジベースです。Superfluidについて情報を検索する際は、ウェブ検索の前にこちらを参照してください。対応キーワード:Superfluid、CFA、GDA、Super App、Super Token、stream、flow rate、real-time balance、pool(member/distributor)、IDA、sentinels、liquidation、TOGA、@sfpro/sdk、semantic money、yellowpaper、whitepaper
civ-finish-quotes
実質的なタスクが真に完了した際に、文明風の儀式的な引用句を追加します。ユーザーやエージェントが機能追加、リファクタリング、分析、設計ドキュメント、プロセス改善、レポート、執筆タスクといった実際の成果物を完成させるときに、明示的な依頼がなくても使用します。短い返信や小さな修正、未完成の作業には適用しません。
nookplot
Base(Ethereum L2)上のAIエージェント向け分散型調整ネットワークです。エージェントがオンチェーンアイデンティティを登録する、コンテンツを公開する、他のエージェントにメッセージを送る、マーケットプレイスで専門家を雇う、バウンティを投稿・請求する、レピュテーションを構築する、共有プロジェクトで協業する、リサーチチャレンジを解くことでNOOKをマイニングする、キュレーションされたナレッジを備えたスタンドアロンオンチェーンエージェントをデプロイする、またはアグリーメントとリワードで収益を得る場合に利用できます。エージェントネットワーク、エージェント調整、分散型エージェント、NOOKトークン、マイニングチャレンジ、ナレッジバンドル、エージェントレピュテーション、エージェントマーケットプレイス、ERC-2771メタトランザクション、Prepare-Sign-Relay、AgentFactory、またはNookplotが言及された場合にトリガーされます。
web3-polymarket
Polygon上でのPolymarket予測市場取引統合です。認証機能(L1 EIP-712、L2 HMAC-SHA256、ビルダーヘッダー)、注文発注(GTC/GTD/FOK/FAK、バッチ、ポストオンリー、ハートビート)、市場データ(Gamma API、Data API、オーダーブック、サブグラフ)、WebSocketストリーミング(市場・ユーザー・スポーツチャネル)、CTF操作(分割、統合、償却、ネガティブリスク)、ブリッジ機能(入金、出金、マルチチェーン)、およびガスレスリレイトランザクションに対応しています。AIエージェント、自動マーケットメーカー、予測市場UI、またはPolygraph上のPolymarketと統合するアプリケーション構築時に活用できます。
ethskills
Ethereum、EVM、またはブロックチェーン関連のリクエストに対応します。スマートコントラクト、dApps、ウォレット、DeFiプロトコルの構築、監査、デプロイ、インタラクションに適用されます。Solidityの開発、コントラクトアドレス、トークン規格(ERC-20、ERC-721、ERC-4626など)、Layer 2ネットワーク(Base、Arbitrum、Optimism、zkSync、Polygon)、Uniswap、Aave、Curveなどのプロトコルとの統合をカバーします。ガスコスト、コントラクトのデシマル設定、オラクルセキュリティ、リエントランシー、MEV、ブリッジング、ウォレット管理、オンチェーンデータの取得、本番環境へのデプロイ、プロトコル進化(EIPライフサイクル、フォーク追跡、今後の変更予定)といったトピックを含みます。
xxyy-trade
このスキルは、ユーザーが「トークン購入」「トークン売却」「トークンスワップ」「暗号資産取引」「取引ステータス確認」「トランザクション照会」「トークンスキャン」「フィード」「チェーン監視」「トークン照会」「トークン詳細」「トークン安全性確認」「ウォレット一覧表示」「マイウォレット」「AIスキャン」「自動スキャン」「ツイートスキャン」「オンボーディング」「IP確認」「IPホワイトリスト」「トークン発行」「自動売却」「損切り」「利益確定」「トレーリングストップ」「保有者」「トップホルダー」「KOLホルダー」などをリクエストした場合、またはSolana/ETH/BSC/BaseチェーンでXXYYを経由した取引について言及した場合に使用します。XXYY Open APIを通じてオンチェーン取引とデータ照会を実現します。