LoginSignup
8
7

More than 5 years have passed since last update.

nginx.confについて

Last updated at Posted at 2018-02-08

nginxの設定ファイルnginx.confのさわりの部分をまとめました。

nginx.confの初期状態

だいたいこんな状態になっているかと思います。

nginx.conf
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


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;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

ディレクティブ

ディレクティブとはuser foo;とかevents{}とかhttp{}などのnginx.confの基本的な構成要素。
;(セミコロン)で終了ものと{}(ブロック)で囲むものがあり、
{}で囲われた箇所をコンテキストと呼び、そのブロック内のみで有効
どのブロックにも囲われていないところはmainコンテキストとして扱われる。

user

nginxを起動すると以下の3つのプロセスが起動します。

  • master process
  • worker process
  • cache manager process

その中でmaster process以外を起動するユーザを設定します。

worker process

Nginxがシングルスレッドで動作するため、コア数に合わせて設定しておく。
autoに指定するとコア数を設定してくれる。

※おまけ

物理CPUの数を確認するコマンド
$ grep physical.id /proc/cpuinfo | sort -u | wc -l

スクリーンショット 2018-02-08 0.14.32.png
このdockerコンテナ(nginx)は物理CPUが2つあるという事です。
仮想マシンの物理CPUが2つとはどういう事なのでしょうか?

CPUごとのコア数を確認するコマンド
$ grep cpu.cores /proc/cpuinfo | sort -u

スクリーンショット 2018-02-08 0.18.11.png

このdockerコンテナのCPUごとのコア数は1ということがわかりました。

論理プロセッサの数
$ grep processor /proc/cpuinfo | wc -l

スクリーンショット 2018-02-08 0.21.58.png

redhatのサイトでは
この3つの数字の中で一番重要で、OSが指定したマイクロ秒で動作できるCPUの数である。
とのこと

events

eventモジュールの定義ができる。
eventモジュールは以下の3つ

worker_connections

nginx.conf
worker_connections 512;

一つのworker processが同時に処理できる接続数です。
※worker process:ユーザのリクエストを処理する。

http

ログの出力先などwebサーバとしてのメイン機能を定義
また、すべての設定をnginx.confに定義するとファイルが膨れ上がってしまうので

nginx.confのhttpディレクティブ抜粋
include /etc/nginx/conf.d/*.conf

と設定ファイルを分けています。

今回の記事で参考にしたのは以下です。ありがとうございます
Nginx設定のまとめ
nginxの設定ファイル nginx.conf の読み方 超入門

また、以下のURLから無料でオライリー社のComplete NGINX Cookbookがダウンロードできます。英語の勉強がてらにいかがでしょうか。
[O'Reilly Ebook] Complete NGINX Cookbook

次回 その7

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