1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

個人的備忘録:Nginxの設定ファイルについて、重要なポイントを自分なりに体系的にまとめてみた

Posted at

はじめに

Nginx は、高性能な Web サーバーおよびリバースプロキシとして広く利用されています。

特に、クラウド環境やコンテナベースのアーキテクチャにおいて、その軽量さと柔軟な設定が注目されています。

しかし、設定ファイルの記述方法が独特であり、適切に設定しないと意図した動作をしないことがあります。

本記事では、Nginx の設定ファイルについて、基本的な構成から具体的な設定例までを詳しく解説します。

記事を書こうと思ったきっかけ

Nginx は Web サーバーやリバースプロキシとして広く使用されていますが、その設定ファイルの構成や記述方法に悩むことが多いです。

特に、ECS などのクラウド環境で利用する際に適切な設定を行うことが重要になります。

本記事では、Nginx の設定ファイルについて、基本的な構成から具体的な設定例までを詳しく解説します。

Nginx の基本構成

Nginx の設定ファイルは /etc/nginx/nginx.conf を基盤とし、その他のディレクトリに追加の設定ファイルを配置できます。

主な設定ファイルの場所

  • /etc/nginx/nginx.conf: Nginx のメイン設定ファイル
  • /etc/nginx/conf.d/*.conf: 仮想ホストや追加の設定ファイル
  • /etc/nginx/sites-available/*.conf: サイトごとの設定(Debian系)
  • /etc/nginx/sites-enabled/*.conf: 有効化されたサイトの設定(Debian系)
  • /etc/nginx/snippets/*.conf: 再利用可能な設定スニペット

nginx.conf の構成

nginx.conf は、基本的に グローバル設定イベント設定HTTP 設定の3つのブロックで構成されます。

(1) グローバル設定

worker_processes auto;  # ワーカープロセス数(auto で自動調整)
pid /run/nginx.pid;  # プロセスIDの保存先

(2) イベント設定

events {
    worker_connections 1024;  # 1ワーカーあたりの接続数
}

(3) HTTP 設定

http {
    include       /etc/nginx/mime.types;  # MIME タイプの設定
    default_type  application/octet-stream;

    sendfile        on;  # sendfile を有効化
    keepalive_timeout 65;  # Keep-Aliveのタイムアウト設定
    server_tokens off;  # Nginx のバージョン情報を非表示にする

    include /etc/nginx/conf.d/*.conf;  # 追加の設定ファイルをロード
}

サーバーブロックの設定

Nginx では、server ブロックを用いて仮想ホストの設定を行います。

server {
    listen 80;  # ポート番号
    server_name example.com;  # ドメイン名

    location / {
        root /var/www/html;  # ドキュメントルート
        index index.html index.htm;
    }

    location /static/ {
        alias /var/www/static/;  # 静的ファイルのパス指定
    }
}

リバースプロキシの設定

Nginx はリバースプロキシとしても利用できます。例えば、バックエンドの Django アプリへプロキシする場合:

server {
    listen 80;
    server_name mysite.com;

    location / {
        proxy_pass http://backend:8000;  # Django コンテナに転送
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

SSL 設定(Let’s Encrypt を使用)

HTTPS を有効にするには、Let's Encrypt などの SSL 証明書を利用できます。

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;

    location / {
        root /var/www/html;
        index index.html;
    }
}

server {
    listen 80;
    server_name example.com;
    return 301 https://$host$request_uri;  # HTTP から HTTPS にリダイレクト
}

設定ファイルのテストと適用

設定を変更した後は、必ず 構文チェック再起動 を行いましょう。

# 設定ファイルの文法チェック
nginx -t

# 設定をリロード(推奨)
systemctl reload nginx

# もし動作が不安定なら完全に再起動
systemctl restart nginx

ログの確認方法

Nginx のエラーログやアクセスログは、トラブルシューティング時に重要です。

# エラーログの確認
cat /var/log/nginx/error.log | tail -n 20

# アクセスログの確認
cat /var/log/nginx/access.log | tail -n 20

まとめ

Nginx の設定ファイルは、用途に応じて柔軟に設定が可能です。

  • 基本設定 (nginx.conf)
  • サーバーブロック (server)
  • リバースプロキシ (proxy_pass)
  • SSL 設定 (listen 443 ssl;)

ECS で運用する場合は、proxy_pass の設定を 環境変数で指定 し、適切な ネットワークモード (awsvpc) を使用 することが重要です。

これらの設定を適切に調整すれば、Nginx をスムーズに運用できます!

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?