Nginx設定 チートシート
Nginxディレクティブのクイックリファレンス。基本設定、locationブロック、リバースプロキシ、SSL/TLS、キャッシュ、セキュリティ、パフォーマンス、ログを網羅
64 件のコマンド
server { }サーバーブロックを定義
server { listen 80; server_name example.com; }listenリッスンするポートを指定
listen 80;listen (IPv6)IPv6でリッスン
listen [::]:80;server_nameサーバー名(ドメイン)を指定
server_name example.com www.example.com;rootドキュメントルートを指定
root /var/www/html;indexデフォルトのインデックスファイルを指定
index index.html index.htm;worker_processesワーカープロセス数を設定
worker_processes auto;worker_connectionsワーカーあたりの最大接続数
worker_connections 1024;include外部設定ファイルを読み込む
include /etc/nginx/conf.d/*.conf;nginx -t設定ファイルの構文チェック
nginx -tnginx -s reload設定をリロード
nginx -s reloadnginx -s stopNginxを停止
nginx -s stoplocation /プレフィックスマッチのlocation
location / { try_files $uri $uri/ =404; }location = /path完全一致のlocation
location = /favicon.ico { log_not_found off; }location ~ regex正規表現マッチ(大文字小文字区別)
location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; }location ~* regex正規表現マッチ(大文字小文字無視)
location ~* \.(jpg|jpeg|png|gif)$ { expires 30d; }location ^~ /path優先プレフィックスマッチ
location ^~ /images/ { root /data; }try_filesファイルの存在を順に確認
try_files $uri $uri/ /index.html;aliaslocationのパスを別パスに置換
location /static/ { alias /var/www/assets/; }return指定したステータスコードを返す
return 301 https://$host$request_uri;rewriteURLを書き換え
rewrite ^/old/(.*)$ /new/$1 permanent;proxy_passリクエストをバックエンドに転送
proxy_pass http://127.0.0.1:3000;proxy_set_header HostHostヘッダーを設定
proxy_set_header Host $host;proxy_set_header X-Real-IPクライアントIPをバックエンドに転送
proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-ForプロキシチェーンのIPを転送
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto元のプロトコルを転送
proxy_set_header X-Forwarded-Proto $scheme;upstreamアップストリームサーバーグループを定義
upstream backend { server 127.0.0.1:3000; server 127.0.0.1:3001; }proxy_read_timeoutバックエンドの読み取りタイムアウト
proxy_read_timeout 90s;proxy_connect_timeoutバックエンドへの接続タイムアウト
proxy_connect_timeout 30s;proxy_bufferingプロキシバッファリングの有効/無効
proxy_buffering off;listen 443 sslSSL付きで443ポートをリッスン
listen 443 ssl;ssl_certificateSSL証明書ファイルのパス
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;ssl_certificate_keySSL秘密鍵ファイルのパス
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;ssl_protocols許可するSSL/TLSプロトコル
ssl_protocols TLSv1.2 TLSv1.3;ssl_ciphers使用する暗号スイートを指定
ssl_ciphers HIGH:!aNULL:!MD5;ssl_prefer_server_ciphersサーバーの暗号スイートを優先
ssl_prefer_server_ciphers on;ssl_session_cacheSSLセッションキャッシュを設定
ssl_session_cache shared:SSL:10m;ssl_session_timeoutSSLセッションのタイムアウト
ssl_session_timeout 1d;expiresレスポンスの有効期限を設定
expires 30d;add_header Cache-ControlCache-Controlヘッダーを追加
add_header Cache-Control 'public, max-age=31536000';proxy_cache_pathプロキシキャッシュのパスを設定
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m;proxy_cacheプロキシキャッシュゾーンを指定
proxy_cache my_cache;proxy_cache_validキャッシュの有効期間を設定
proxy_cache_valid 200 60m;proxy_cache_bypassキャッシュをバイパスする条件
proxy_cache_bypass $http_pragma;deny指定IPからのアクセスを拒否
deny 192.168.1.100;allow指定IPからのアクセスを許可
allow 10.0.0.0/8;auth_basicBasic認証を有効化
auth_basic 'Restricted Area';auth_basic_user_fileBasic認証のユーザーファイルを指定
auth_basic_user_file /etc/nginx/.htpasswd;add_header X-Frame-Optionsクリックジャッキング対策ヘッダー
add_header X-Frame-Options SAMEORIGIN;add_header X-Content-Type-OptionsMIMEスニッフィング防止ヘッダー
add_header X-Content-Type-Options nosniff;limit_req_zoneレートリミットゾーンを定義
limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;limit_reqレートリミットを適用
limit_req zone=one burst=20 nodelay;gzipgzip圧縮を有効化
gzip on;gzip_typesgzip圧縮するMIMEタイプを指定
gzip_types text/plain text/css application/json application/javascript;gzip_min_lengthgzip圧縮する最小サイズ
gzip_min_length 256;sendfilesendfileを有効化(高速ファイル転送)
sendfile on;tcp_nopushTCP_NOPUSHオプションを有効化
tcp_nopush on;tcp_nodelayTCP_NODELAYオプションを有効化
tcp_nodelay on;keepalive_timeoutKeep-Aliveタイムアウトを設定
keepalive_timeout 65;access_logアクセスログのパスを設定
access_log /var/log/nginx/access.log;error_logエラーログのパスとレベルを設定
error_log /var/log/nginx/error.log warn;log_formatカスタムログフォーマットを定義
log_format main '$remote_addr - $remote_user [$time_local] "$request" $status';access_log offアクセスログを無効化
access_log off;open_log_file_cacheログファイルのキャッシュを設定
open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;