Nginx ユーザーマニュアル
基本操作
Nginx の開始
# Nginx を開始
nginx
# systemctl を使用(systemd の場合)
systemctl start nginx
停止
# 高速停止(即座に終了)
nginx -s stop
# 優雅停止(リクエストの処理が完了するのを待つ)
nginx -s quit
設定再読み込み
# 設定ファイルを再読み込み
nginx -s reload
ログの再オープン
# ログファイルを再オープン
nginx -s reopen
設定チェック
# 設定ファイルの構文をテスト
nginx -t
# 完全な設定を表示
nginx -T
設定ファイル構造
メイン設定ファイル構造
nginx.conf
├── main (グローバルブロック)
│ ├── user
│ ├── worker_processes
│ ├── error_log
│ └── pid
│
├── events (events ブロック)
│ └── worker_connections
│
└── http (http ブロック)
├── include
├── default_type
├── log_format
├── access_log
├── sendfile
├── keepalive_timeout
├── gzip
│
└── server (server ブロック)
├── listen
├── server_name
├── location
│ ├── root
│ ├── index
│ └── proxy_pass
│
└── error_page
一般的な指令
main ブロック
# Nginx を実行するユーザー
user nginx;
# ワーカープロセス数(auto は CPU コア数を自動検出)
worker_processes auto;
# エラーログパス
error_log /var/log/nginx/error.log warn;
# PID ファイルパス
pid /var/run/nginx.pid;
events ブロック
events {
# ワーカーごとの最大接続数
worker_connections 1024;
# epoll モデルを使用(Linux)
use epoll;
# 複数の接続を同時に受け入れる
multi_accept on;
}
http ブロック
http {
# MIME タイプを含める
include /etc/nginx/mime.types;
# デフォルトのファイルタイプ
default_type application/octet-stream;
# アクセスログ
access_log /var/log/nginx/access.log;
# ファイル送信
sendfile on;
# TCP_NOPUSH と TCP_NODELAY
tcp_nopush on;
tcp_nodelay on;
# Keep-alive タイムアウト
keepalive_timeout 65;
# gzip 圧縮を有効化
gzip on;
}
静的ファイルサービス
基本設定
server {
listen 80;
server_name example.com www.example.com;
# ルートディレクトリ
root /var/www/html;
# デフォルトインデックスファイル
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
# アクセスログ
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
}
ディレクトリリスティング
location / {
# ディレクトリリスティングを有効化
autoindex on;
# ファイルサイズを表示
autoindex_exact_size on;
# ローカル時間を表示
autoindex_localtime on;
}
静的リソースキャッシュ
location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff|woff2|ttf|svg)$ {
expires 30d;
add_header Cache-Control "public, no-transform";
}
リ버스プロキシ
基本リ버스プロキシ
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://backend_server;
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_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=100m inactive=60m use_temp_path=off;
server {
listen 80;
server_name example.com;
location / {
proxy_cache my_cache;
proxy_cache_valid 200 60m;
proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
proxy_cache_lock on;
proxy_pass http://backend_server;
proxy_set_header Host $host;
}
}
WebSocket プロキシ
location /ws/ {
proxy_pass http://backend_server;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_read_timeout 86400;
}
ロードバランシング
ラウンドロビン
upstream backend {
server backend1.example.com;
server backend2.example.com;
server backend3.example.com;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://backend;
}
}
重み付けロードバランシング
upstream backend {
server backend1.example.com weight=5;
server backend2.example.com weight=2;
server backend3.example.com;
}
IP ハッシュ
upstream backend {
ip_hash;
server backend1.example.com;
server backend2.example.com;
server backend3.example.com down;
}
最小接続
upstream backend {
least_conn;
server backend1.example.com;
server backend2.example.com;
}
SSL/TLS 設定
基本 HTTPS 設定
server {
listen 443 ssl http2;
server_name example.com;
ssl_certificate /etc/nginx/ssl/example.com.crt;
ssl_certificate_key /etc/nginx/ssl/example.com.key;
# SSL プロコルバージョン
ssl_protocols TLSv1.2 TLSv1.3;
# SSL 暗号スイート
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
ssl_prefer_server_ciphers off;
# HSTS
add_header Strict-Transport-Security "max-age=63072000" always;
location / {
root /var/www/html;
index index.html;
}
}
HTTP から HTTPS へのリダイレクト
server {
listen 80;
server_name example.com;
return 301 https://$server_name$request_uri;
}
一般的な設定例
PHP-FPM 設定
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.php index.html;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
Node.js プロキシ
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
レート制限設定
# 接続ベースのレート制限
limit_conn_zone $binary_remote_addr zone=addr:10m;
server {
location / {
limit_conn addr 10;
}
}
# リクエストレートの制限
limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;
server {
location / {
limit_req zone=one burst=20 nodelay;
}
}
直リンク防止設定
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
valid_referers none blocked example.com *.example.com;
if ($invalid_referer) {
return 403;
}
}
ログ管理
カスタムログフォーマット
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;
条件付きログ
map $request_uri $loggable {
~^/health-check 0;
default 1;
}
access_log /var/log/nginx/access.log combined if=$loggable;
ログローテーション
logrotate を使用:
# /etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
create 0640 www-data adm
sharedscripts
postrotate
[ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
endscript
}
