0
0

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] なんの変哲もないnginx.confの設定確認

Posted at

目的

  • nginx.confの概要を理解する
  • それぞれの記述意図を理解する

前提

  • APIの前に配置するNginxのファイルです
    • APIがDjango REST Frameworkで構築されているのでGunicornを利用しています

Nginxの設定ファイル

nginx.conf
upstream gunicorn {
    server unix:///sotaheavymetal21_back/tmp/gunicorn_socket;
}

server {
    listen 80;
    server_name api.sotaheavymetal21.company.co.jp internal.sotaheavymetal21.company.co.jp;
    server_tokens off;

    # ファイルサイズの変更、デフォルト値は1M
    client_max_body_size 5M;

    # HTTP レスポンスヘッダの Content_Type に付与する文字コード
    charset utf-8;

    # ログ設定
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    # API 通信
    location /api {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_read_timeout 3600;
        proxy_pass http://gunicorn;
    }

    # ヘルスチェック
    location /api/health {
        empty_gif;
        access_log off;
        break;
    }

    # HTTP 通信をタイムアウトせずに待つ秒数
    keepalive_timeout 60;
}


api.sotaheavymetal21.company.co.jpおよびinternal.sotaheavymetal21.company.co.jpという2つのサブドメインに対して設定しています
主にAPI通信とヘルスチェックの処理を行うための設定が含まれています

上流サーバーの設定

nginx.conf
upstream gunicorn {
    server unix:///sotaheavymetal21_back/tmp/gunicorn_socket;
}

目的: NginxからGunicorn(アプリケーションサーバー)へのリクエストをプロキシするため

upstreamブロックは、gunicornという名前の上流サーバーグループを定義しています
この上流サーバーはUNIXドメインソケットを使用して、/sotaheavymetal21_back/tmp/gunicorn_socketに接続されます
通常、Gunicornのようなアプリケーションサーバーにリクエストを転送するために使用されます

UNIXドメインソケットは、TCPソケットに比べて通信オーバーヘッドが低いため、同一ホスト内での通信においてパフォーマンスが向上します

nginx.conf
server {
    listen 80;
    server_name api.sotaheavymetal21.company.co.jp internal.sotaheavymetal21.company.co.jp;
    server_tokens off;

    client_max_body_size 5M;
    charset utf-8;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    location /api {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_read_timeout 3600;
        proxy_pass http://gunicorn;
    }

    location /api/health {
        empty_gif;
        access_log off;
        break;
    }

    keepalive_timeout 60;
}
listen 80;

特定のサブドメインに対するリクエスト処理の詳細を定義し、適切なロギング、セキュリティ、およびパフォーマンス設定を適用します

nginx.conf
server_name api.sotaheavymetal21.company.co.jp internal.sotaheavymetal21.company.co.jp;

このサーバーブロックは、api.sotaheavymetal21.company.co.jpおよびinternal.sotaheavymetal21.company.co.jpという2つのサブドメインに対して適用されます

nginx.conf
server_tokens off;

目的:潜在的な攻撃者にサーバーの詳細情報を提供しないようにするため

Nginxが送信するエラーページやレスポンスヘッダからNginxのバージョン情報を削除します
サーバートークンをオフにすることで、エラーページやレスポンスヘッダにNginxのバージョン情報が含まれないようにします

nginx.conf
client_max_body_size 5M;

目的:過度に大きなリクエストがサーバーに負荷をかけることを防ぐため

クライアントから送信されるリクエストボディの最大サイズを5MBに設定します(デフォルトは1MB)

nginx.conf
charset utf-8;

目的:クライアントが適切に文字エンコーディングを解釈できるようにするため

HTTPレスポンスヘッダのContent-Typeに付与する文字コードをUTF-8に設定します

nginx.conf
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

リクエストの詳細やエラーの診断が容易になるため

アクセスログとエラーログの出力先を指定します

APIリクエストのプロキシ設定

nginx.conf
location /api {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_read_timeout 3600;
    proxy_pass http://gunicorn;
}
location /api;

/apiで始まるリクエストに対する設定です

nginx.conf
proxy_set_header X-Real-IP $remote_addr;

クライアントのIPアドレスを上流サーバー(Gunicorn)に渡します

nginx.conf
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

クライアントのIPアドレスを含むX-Forwarded-Forヘッダを設定します
プロキシを経由するリクエストの元のIPアドレスを保持します

nginx.conf
proxy_set_header Host $http_host;

リクエストのホストヘッダを上流サーバーに渡します
オリジナルのホストヘッダを保持します

nginx.conf
proxy_read_timeout 3600;

長時間の処理が必要なリクエストに対応するため

プロキシからのレスポンスを待つ最大時間を3600秒(1時間)に設定します

nginx.conf
proxy_pass http://gunicorn;

リクエストを上流サーバー(Gunicorn)に転送します

ヘルスチェックの設定

nginx.conf
location /api/health {
    empty_gif;
    access_log off;
    break;
}
location /api/health;

/api/healthへのリクエストに対する設定です

nginx.conf
empty_gif;

空のGIF画像を返します
通常、ヘルスチェックのエンドポイントで軽量なレスポンスを返すために使用されます

nginx.conf
access_log off;

このロケーションに対するアクセスログの記録を無効にします

nginx.conf
break;

他の処理を行わずにこの設定でリクエストの処理を終了します

接続のキープアライブ設定

nginx.conf
keepalive_timeout 60;

HTTP接続をタイムアウトせずに待つ秒数を60秒に設定します
クライアントとサーバー間の持続的な接続を維持する時間です

まとめ

この設定ファイルは、api.sotaheavymetal21.company.co.jpおよびinternal.sotaheavymetal21.company.co.jpのサブドメインに対して、APIリクエストをGunicornサーバーにプロキシし、ヘルスチェックリクエストには軽量なレスポンスを返すように構成されています
さらに、セキュリティやパフォーマンス、ログ設定も含まれており、全体的な運用に必要な基本的な設定が網羅されています

おわりに

お疲れ様でした!!
なんの変哲もないですが、この機会に概要だけ学んでおきましょう
少しでも皆さんのお力添えになればと思っています🐰

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?