2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

nginx 1.16 設定解剖

Last updated at Posted at 2020-07-12

nginx 1.16.1のデフォルト設定についての解説記事. CentOS 7にRPMインストールしたnginx 1.16.1の/etc/nginx/nginx.confのデフォルト設定を詳しく見ていきます.

  • 環境情報

CentOS Linux release 7.6.1810 (Core)
nginx version: nginx/1.16.1

nginx.conf全体

/etc/nginx/nginx.confの全体設定は次のような感じです. 以下、詳しく見ていく.

/etc/nginx/nginx.conf
1 # For more information on configuration, see:
2 #   * Official English Documentation: http://nginx.org/en/docs/
3 #   * Official Russian Documentation: http://nginx.org/ru/docs/
4
5 user nginx;
6 worker_processes auto;
7 error_log /var/log/nginx/error.log;
8 pid /run/nginx.pid;
9
10 # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
11 include /usr/share/nginx/modules/*.conf;
12
13 events {
14     worker_connections 1024;
15 }
16
17 http {
18     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
19                       '$status $body_bytes_sent "$http_referer" '
20                       '"$http_user_agent" "$http_x_forwarded_for"';
21
22     access_log  /var/log/nginx/access.log  main;
23
24     sendfile            on;
25     tcp_nopush          on;
26     tcp_nodelay         on;
27     keepalive_timeout   65;
28     types_hash_max_size 2048;
29
30     include             /etc/nginx/mime.types;
31     default_type        application/octet-stream;
32
33     # Load modular configuration files from the /etc/nginx/conf.d directory.
34     # See http://nginx.org/en/docs/ngx_core_module.html#include
35     # for more information.
36     include /etc/nginx/conf.d/*.conf;
37
38     server {
39         listen       80 default_server;
40         listen       [::]:80 default_server;
41         server_name  _;
42         root         /usr/share/nginx/html;
43
44         # Load configuration files for the default server block.
45         include /etc/nginx/default.d/*.conf;
46
47         location / {
48         }
49
50         error_page 404 /404.html;
51             location = /40x.html {
52         }
53
54         error_page 500 502 503 504 /50x.html;
55             location = /50x.html {
56         }
57     }
58
59 # Settings for a TLS enabled server.
60 #
61 #    server {
62 #        listen       443 ssl http2 default_server;
63 #        listen       [::]:443 ssl http2 default_server;
64 #        server_name  _;
65 #        root         /usr/share/nginx/html;
66 #
67 #        ssl_certificate "/etc/pki/nginx/server.crt";
68 #        ssl_certificate_key "/etc/pki/nginx/private/server.key";
69 #        ssl_session_cache shared:SSL:1m;
70 #        ssl_session_timeout  10m;
71 #        ssl_ciphers HIGH:!aNULL:!MD5;
72 #        ssl_prefer_server_ciphers on;
73 #
74 #        # Load configuration files for the default server block.
75 #        include /etc/nginx/default.d/*.conf;
76 #
77 #        location / {
78 #        }
79 #
80 #        error_page 404 /404.html;
81 #            location = /40x.html {
82 #        }
83 #
84 #        error_page 500 502 503 504 /50x.html;
85 #            location = /50x.html {
86 #        }
87 #    }
88
89 }
90

Nginx本体の設定

/etc/nginx/nginx.conf
1 # For more information on configuration, see:
2 #   * Official English Documentation: http://nginx.org/en/docs/
3 #   * Official Russian Documentation: http://nginx.org/ru/docs/
4
# + Nginx本体の設定
#  ワーカプロセスの実行ユーザを指定
5 user nginx;
# ワーカのプロセス数
# 「auto」にするとCPUコア数を自動検出してコア数と同じ数のワーカプロセスを起動する
6 worker_processes auto;
# エラーログの出力設定
7 error_log /var/log/nginx/error.log;
# PIDファイルの出力先
8 pid /run/nginx.pid;
9
10 # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
11 include /usr/share/nginx/modules/*.conf;
12
# ワーカプロセスのイベント駆動についての設定を記述する
# eventsディレクディブは必須設定のため、コンテキストの内容が空でも記述する
13 events {
       # ワーカプロセスが処理するコネクション数
       # コネクション数はワーカ毎の最大数
14     worker_connections 1024;
15 }
16

HTTPサーバの設定

/etc/nginx/nginx.conf
# + HTTPサーバの設定
# HTTPサーバについての設定を記述する
17 http {
       # ログフォーマット定義
18     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
19                       '$status $body_bytes_sent "$http_referer" '
20                       '"$http_user_agent" "$http_x_forwarded_for"';
       # ログファイルの出力先
22     access_log  /var/log/nginx/access.log  main;

       # + パフォーマンスチューニング
       # ファイル読み込みとレスポンス送信にsenfile()システムコールを有効
       # sendfile()を有効にすると、ファイルディスクリプタから直接クライアントに送信するため、
         ファイル送信の性能が向上する
24     sendfile            on;
       # sendfileディレクティブが有効な場合、TCP_CORKオプションが使用される
       # 最大パケットサイズでレスポンスヘッダとファイルを送信できるため、送信パケット数を最小化できる
       # 基本的には有効にしておく
25     tcp_nopush          on;
26     tcp_nodelay         on;
       # nginxに常時接続しているクライアントのタイムアウト時間
27     keepalive_timeout   65;
28     types_hash_max_size 2048;

       # MIMEタイプの指定
       # HTTPではContent-Typeヘッダフィールドの値にファイルの種類を指定する必要がある
30     include             /etc/nginx/mime.types;
       # デフォルトのMIMEタイプ
31     default_type        application/octet-stream;
32
33     # Load modular configuration files from the /etc/nginx/conf.d directory.
34     # See http://nginx.org/en/docs/ngx_core_module.html#include
35     # for more information.
36     include /etc/nginx/conf.d/*.conf;

# + バーチャルサーバの定義
# バーチャルサーバはserverディレクティブに記述する
38     server {
           # LISTENポート番号
           # 「default_server」でデフォルトサーバとして指定
           # 「default_server」が明示されていない場合は、一番上に記述したserverがデフォルトサーバ
39         listen       80 default_server;
40         listen       [::]:80 default_server;
           # ホスト名
           # ワイルドカードも使用できる
41         server_name  _;
           # 公開ディレクトリ
42         root         /usr/share/nginx/html;
43
44         # Load configuration files for the default server block.
45         include /etc/nginx/default.d/*.conf;
46
           # マッチするURL毎にどのファイルを配信するのか
47         location / {
48         }
49
50         error_page 404 /404.html;
51             location = /40x.html {
52         }
53
54         error_page 500 502 503 504 /50x.html;
55             location = /50x.html {
56         }
57     }

TLS/SSLの設定

デフォルトでは全てコメントアウトされている.

58
59 # Settings for a TLS enabled server.
60 #
61 #    server {
62 #        listen       443 ssl http2 default_server;
63 #        listen       [::]:443 ssl http2 default_server;
64 #        server_name  _;
65 #        root         /usr/share/nginx/html;
66 #
67 #        ssl_certificate "/etc/pki/nginx/server.crt";
68 #        ssl_certificate_key "/etc/pki/nginx/private/server.key";
69 #        ssl_session_cache shared:SSL:1m;
70 #        ssl_session_timeout  10m;
71 #        ssl_ciphers HIGH:!aNULL:!MD5;
72 #        ssl_prefer_server_ciphers on;
73 #
74 #        # Load configuration files for the default server block.
75 #        include /etc/nginx/default.d/*.conf;
76 #
77 #        location / {
78 #        }
79 #
80 #        error_page 404 /404.html;
81 #            location = /40x.html {
82 #        }
83 #
84 #        error_page 500 502 503 504 /50x.html;
85 #            location = /50x.html {
86 #        }
87 #    }
88
89 }
90
2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?