アウトライン

nginxのは、サービスを提供するために、複数のワーカー・プロセスを管理するために、マスター・プロセスを使用することです。プロセスの管理を担当マスター労働者、およびワーカープロセスは、実際の顧客サービスを提供し、ワーカー・プロセスの数は、一般的に、プロセス間通信機構の数による労働者の間でCPUサーバ、ロードバランシングおよびその他の機能上のコア数と同じです。次の図に示すことにより、nginxのプロセスとの関係:

nginxのサービスが開始される設定ファイルを読み込みます、その後の行動は、構成ファイルの指示に従って行われます。nginxの設定ファイルはプレーンテキストファイル、デフォルトのインストールnginxので、その設定ファイルは/ usr / local / nginxの/ confに/ディレクトリにあります。これは、nginx.confベースの設定ファイル。その後、#の先頭にラインコンフィギュレーション・ファイルを、またはスペースの数やTABキーの前にあり、そして#と話はコメントとみなされます。ここだけのメイン設定ファイルの構造を理解しています。

nginxの設定ファイルは、「{}」ブロックレベル、全体階層のメインプロファイル、すなわち、最大に分割組成物を意味するブロック(ブロック)各ブロックにブロック名と括弧の対は、編成されレベル、メインレベルのイベント、HTTP、メールや他のレベルで存在することができ、ひいては、HTTPサーバブロックが存在するであろう、サーバーブロックが位置ブロックを含むことができます。すなわち、ブロック間で、外側ブロック継承インナーブロックを入れ子にすることができます。最も基本的な構成項目の構文は、「設定項目名設定項目の設定項目の値が値1つの2 3 ...設定項目値」です。

各レベルは、例えばworker_processesのために、独自のディレクティブ(指令)を持つことができるワーカー・プロセスnginxのサービスの数を指定する命令のメインレベル、です。worker_processesのみ主に存在することができるようにいくつかの命令のみ、階層構造に構成することができ、一部の命令は、複数のレベルで存在していてもよい、この場合、サブブロックは、親のブロック構成を継承する一方、サブブロックなら命令上に配置された親ブロックは、親構成ブロックが上書きされます。フォーマット命令は、「コマンド名パラメータパラメータ1パラメータ2 ... N;」され、パーティションノートパラメータ間の任意の数のスペース、セミコロンを追加する最後を使用することができます。

以下は設定ファイルnginxのの一般的な構造を示しています。

基本的なサービスのnginxの設定項目

nginxのサービスが実行されている、あなたは、いくつかのコアモジュールと基本構成と呼ばれる、サポートされている設定項目を実行するイベント・モジュールをロードする必要があり、基本的な設定項目は、大きく次の4つのカテゴリに分けることができます。

  • デバッグおよび位置決めするための設定項目。
  • 必要な設定項目の通常の動作;
  • CIの最適化パフォーマンス;
  • Eventクラスの設定項目。

各構成要素の特定の実装を次のように

/* Nginx 服务基本配置项 */

/* 用于调试、定位的配置项 */

#以守护进程 Nginx 运行方式
#语法:daemon off | on;
#默认:daemon on;

#master / worker 工作方式
#语法:master_process on | off;
#默认:master_process on;

#error 日志设置
#                   路径        错误级别
#语法:error_log    /path/file  level;
#默认:error_log    logs/error.log  error;
#其中/path/file是一个具体文件;level是日志的输出级别,其取值如下:
#   debug info notice warn error crit alert emerg
#从左至右级别增大;若设定一个级别后,则在输出的日志文件中只输出级别大于或等于已设定的级别;

#处理特殊调试点
#语法:debug_points [stop | abort]
#这个设置是来跟踪调试 Nginx 的;

#仅对指定的客户端输出 debug 级别的日志
#语法:debug_connection [IP | DIR]

#限制 coredump 核心转储文件的大小
#语法:worker_rlimit_core   size;

#指定 coredump 文件的生成目录
#语法:working_directory    path;

/* 正常运行的配置项 */

#定义环境变量
#语法:env  VAR | VAR=VALUE;
#VAR 是变量名,VALUE 是目录;

#嵌入其他配置文件
#语法:include  /path/file;
#include 配置项可以将其他配置文件嵌入到 Nginx 的 nginx.conf 文件中;

#pid 的文件路径
#语法:pid  path/file;
#默认:pid  logs/nginx.pid;
#保存 master 进程 ID 的 pid 文件存放路径;

#Nginx worker 运行的用户及用户组
#语法:user username    [groupname];
#默认:user nobody nobody;

#指定 Nginx worker进程可打开最大句柄个数
#语法:worker_rlimit_nofile limit;

#限制信号队列
#语法:worker_rlimit_sigpending limit;
#设置每个用户发给 Nginx 的信号队列大小,超出则丢弃;

/* 优化性能配置项 */

#Nginx worker 进程的个数
#语法:worker_process   number;
#默认:worker_process   1;

#绑定 Nginx worker 进程到指定的 CPU 内核
#语法:worker_cpu_affinity  cpumask [cpumask...]

#SSL 硬件加速
#语法:ssl_engine   device;

#系统调用 gettimeofday 的执行频率
#语法:timer_resolution t;

#Nginx worker 进程优先级设置
#语法:worker_priority  nice;
#默认:worker_priority  0;

/* 事件类配置项  */
#一般有以下几种配置:
#1、是否打开accept锁
#   语法格式:accept_mutex [on | off];

#2、lock文件的路径
#   语法格式:lock_file  path/file;

#3、使用accept锁后到真正建立连接之间的延迟时间
#   语法格式:accept_mutex_delay Nms;

#4、批量建立新连接
#   语法格式:multi_accept [on | off];
#
#5、选择事件模型
#   语法格式:use [kqueue | rtisg | epoll | /dev/poll | select | poll | eventport];

#6、每个worker进行的最大连接数
#   语法格式:worker_connections number;

HTTPのコアモジュールの設定

「特異的に参照するとnginxのコアモジュールは、HTTPに配置されました

/* HTTP 核心模块配置的功能 */

/* 虚拟主机与请求分发 */

#监听端口
#语法:listen   address:port[default | default_server | [backlong=num | rcvbuf=size | sndbuf=size | 
# accept_filter | deferred | bind | ipv6only=[on | off] | ssl]];
# 默认:listen:80;
# 说明:
#   default或default_server:将所在的server块作为web服务的默认server块;当请求无法匹配配置文件中的所有主机名时,就会选择默认的虚拟主机;
#   backlog=num:表示 TCP 中backlog队列存放TCP新连接请求的大小,默认是-1,表示不予设置;
#   rcvbuf=size:设置监听句柄SO_RCVBUF的参数;
#   sndbuf=size:设置监听句柄SO_SNDBUF的参数;
#   accept_filter:设置accept过滤器,只对FreeBSD操作系统有用;
#   deferred:设置该参数后,若用户发起TCP连接请求,并且完成TCP三次握手,但是若用户没有发送数据,则不会唤醒worker进程,直到发送数据;
#   bind:绑定当前端口 / 地址对,只有同时对一个端口监听多个地址时才会生效;
#   ssl:在当前端口建立的连接必须基于ssl协议;
#配置块范围:server

#主机名称
#语法:server_name  name[...];
#默认:server_name  "";
#配置块范围:server

#server name 是使用散列表存储的
#每个散列桶占用内存大小
#语法:server_names_hash_bucket_size    size;
#默认:server_names_hash_bucker_size    32|64|128;
#
#散列表最大bucket数量
#语法:server_names_hash_max_size   size;
#默认:server_names_hash_max_size   512;
#默认:server_name_in_redirect  on;
#配置块范围:server、http、location

#处理重定向主机名
#语法:server_name_in_redirect  on | off;
#默认:server_name_in_redirect  on;
#配置块范围:server、http、location

#location语法:location[= | ~ | ~* | ^~ | @] /uri/ {}  
#配置块范围:server
        #location尝试根据用户请求中的URI来匹配 /uri表达式,若匹配成功,则执行{}里面的配置来处理用户请求  
#以下是location的一般配置项  
#1、以root方式设置资源路径  
#   语法格式:root path;  
#2、以alias方式设置资源路径  
#   语法格式:alias path;  
#3、访问首页  
#   语法格式:index file...;  
#4、根据HTTP返回码重定向页面  
#   语法格式:error_page code [code...] [= | =answer-code] uri | @named_location;  
#5、是否允许递归使用error_page  
#   语法格式:recursive_error_pages [on | off];  
#6、try_files  
#   语法格式:try_files path1 [path2] uri;  

/* 文件路径的定义 */

#root方式设置资源路径
#语法:root path;
#默认:root html;
#配置块范围:server、http、location、if

#以alias方式设置资源路径
#语法:alias path;
#配置块范围:location

#访问主页
#语法:index    file...;
#默认:index    index.html;
#配置块范围:http、server、location

#根据HTTP返回码重定向页面  
#   语法:error_page code [code...] [= | =answer-code] uri | @named_location;  
#配置块范围:server、http、location、if

#是否允许递归使用error_page  
#   语法:recursive_error_pages [on | off];  
#配置块范围:http、server、location

#try_files  
#   语法:try_files path1 [path2] uri;  
#配置块范围:server、location

/* 内存及磁盘资源分配 */

# HTTP 包体只存储在磁盘文件中
# 语法:client_body_in_file_only    on | clean | off;
# 默认:client_body_in_file_only  off;
# 配置块范围:http、server、location

# HTTP 包体尽量写入到一个内存buffer中
# 语法:client_body_single_buffer   on | off;
# 默认:client_body_single_buffer   off;
# 配置块范围:http、server、location

# 存储 HTTP 头部的内存buffer大小
# 语法:client_header_buffer_size   size;
# 默认:client_header_buffer_size   1k;
# 配置块范围:http、server

# 存储超大 HTTP 头部的内存buffer大小
# 语法:large_client_header_buffer_size   number    size;
# 默认:large_client_header_buffer_size   4   8k;
# 配置块范围:http、server

# 存储 HTTP 包体的内存buffer大小
# 语法:client_body_buffer_size   size;
# 默认:client_body_buffer_size   8k/16k;
# 配置块范围:http、server、location

# HTTP 包体的临时存放目录
# 语法:client_body_temp_path   dir-path    [level1 [level2 [level3]]];
# 默认:client_body_temp_path   client_body_temp;
# 配置块范围:http、server、location

# 存储 TCP 成功建立连接的内存池大小
# 语法:connection_pool_size    size;
# 默认:connection_pool_size    256;
# 配置块范围:http、server

# 存储 TCP 请求连接的内存池大小
# 语法:request_pool_size    size;
# 默认:request_pool_size    4k;
# 配置块范围:http、server

/* 网络连接设置 */

# 读取 HTTP 头部的超时时间
# 语法:client_header_timeout   time;
# 默认:client_header_timeout   60;
# 配置块范围:http、server、location

# 读取 HTTP 包体的超时时间
# 语法:client_body_timeout   time;
# 默认:client_body_timeout   60;
# 配置块范围:http、server、location

# 发送响应的超时时间
# 语法:send_timeout   time;
# 默认:send_timeout   60;
# 配置块范围:http、server、location

# TCP 连接的超时重置
# 语法:reset_timeout_connection   on | off;
# 默认:reset_timeout_connection   off;
# 配置块范围:http、server、location

# 控制关闭 TCP 连接的方式
# 语法:lingering_close off | on | always;
# 默认:lingering_close on;
# 配置块范围:http、server、location
# always 表示关闭连接之前无条件处理连接上所有用户数据;
# off 表示不处理;on 一般会处理;

# lingering_time
# 语法:lingering_time   time;
# 默认:lingering_time   30s;
# 配置块范围:http、server、location

# lingering_timeout
# 语法:lingering_timeout   time;
# 默认:lingering_time   5s;
# 配置块范围:http、server、location

# 对某些浏览器禁止keepalive功能
# 语法:keepalive_disable   [mise6 | safari | none]...
# 默认:keepalive_disable   mise6  safari;
# 配置块范围:http、server、location

# keepalive超时时间
# 语法:keepalive_timeout   time;
# 默认:keepalive_timeout   75;
# 配置块范围:http、server、location

# keepalive长连接上允许最大请求数
# 语法:keepalive_requests  n;
# 默认:keepalive_requests  100;
# 配置块范围:http、server、location

# tcp_nodelay
# 语法:tcp_nodelay on | off;
# 默认:tcp_nodelay on;
# 配置块范围:http、server、location

# tcp_nopush
# 语法:tcp_nopush on | off;
# 默认:tcp_nopush off;
# 配置块范围:http、server、location

/* MIME 类型设置 */

# MIME type 与文件扩展的映射
# 语法:type{...}
# 配置块范围:http、server、location
# 多个扩展名可映射到同一个 MIME type

# 默认 MIME type
# 语法:default_type    MIME-type;
# 默认:default_type    text/plain;
# 配置块范围:http、server、location

# type_hash_bucket_size
# 语法:type_hash_bucket_size   size;
# 默认:type_hash_bucket_size   32 | 64 | 128;
# 配置块范围:http、server、location

# type_hash_max_size
# 语法:type_hash_max_size   size;
# 默认:type_hash_max_size   1024;
# 配置块范围:http、server、location

/* 限制客户端请求 */

# 按 HTTP 方法名限制用户请求
# 语法:limit_except    method...{...}
# 配置块:location
# method 的取值如下:
# GET、HEAD、POST、PUT、DELETE、MKCOL、COPY、MOVE、OPTIONS、
# PROPFIND、PROPPATCH、LOCK、UNLOCK、PATCH

# HTTP 请求包体的最大值
# 语法:client_max_body_size    size;
# 默认:client_max_body_size    1m;
# 配置块范围:http、server、location

# 对请求限制速度
# 语法:limit_rate  speed;
# 默认:limit_rate  0;
# 配置块范围:http、server、location、if
# 0 表示不限速

# limit_rate_after规定时间后限速
# 语法:limit_rate_after  time;
# 默认:limit_rate_after    1m;
# 配置块范围:http、server、location、if

/* 文件操作的优化 */

# sendfile系统调用
# 语法:sendfile    on | off;
# 默认:sendfile    off;
# 配置块:http、server、location

# AIO 系统调用
# 语法:aio on | off;
# 默认:aio off;
# 配置块:http、server、location

# directio
# 语法:directio    size | off;
# 默认:directio    off;
# 配置块:http、server、location

# directio_alignment
# 语法:directio_alignment    size;
# 默认:directio_alignment    512;
# 配置块:http、server、location

# 打开文件缓存
# 语法:open_file_cache max=N [inactive=time] | off;
# 默认:open_file_cache off;
# 配置块:http、server、location

# 是否缓存打开文件的错误信息
# 语法:open_file_cache_errors  on | off;
# 默认:open_file_cache_errors  off;
# 配置块:http、server、location

# 不被淘汰的最小访问次数
# 语法:open_file_cache_min_user  number;
# 默认:open_file_cache_min_user  1;
# 配置块:http、server、location

# 检验缓存中元素有效性的频率
# 语法:open_file_cache_valid  time;
# 默认:open_file_cache_valid  60s;
# 配置块:http、server、location

/* 客户请求的特殊处理 */

# 忽略不合法的 HTTP 头部
# 语法:ignore_invalid_headers  on | off;
# 默认:ignore_invalid_headers  on;
# 配置块:http、server

# HTTP 头部是否允许下划线
# 语法:underscores_in_headers  on | off;
# 默认:underscores_in_headers  off;
# 配置块:http、server

# If_Modified_Since 头部的处理策略
# 语法:if_modified_since   [off | exact | before]
# 默认:if_modified_since   exact;
# 配置块:http、server、location

# 文件未找到时是否记录到error日志
# 语法:log_not_found   on | off;
# 默认:log_not_found   on;
# 配置块:http、server、location

# 是否合并相邻的“/
# 语法:merge_slashes   on | off;
# 默认:merge_slashes   on;
# 配置块:http、server、location

# DNS解析地址
# 语法:resolver    address...;
# 配置块:http、server、location

# DNS解析的超时时间
# 语法:resolver_timeout    time;
# 默认:resolver_timeout    30s;
# 配置块:http、server、location

# 返回错误页面是否在server中注明Nginx版本
# 语法:server_tokens   on | off;
# 默认:server_tokens   on;
# 配置块:http、server、location

以下は、成功したのUbuntu 12.04 nginxのにインストールされたシステムの後にメインの設定ファイルです:

#Nginx服务器正常启动时会读取该配置文件,以下的值都是默认的,若需要可自行修改;
#以下是配置选项

#Nginx worker进程运行的用户以及用户组
#语法格式:user  username[groupname]
#user  nobody;

#Nginx worker 进程个数
worker_processes  1;

#error 日志设置
#语法格式:error /path/file level
#其中/path/file是一个具体文件;level是日志的输出级别,其取值如下:
#debug info notice warn error crit alert emerg,从左至右级别增大;
#若设定一个级别后,则在输出的日志文件中只输出级别大于或等于已设定的级别;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#保存master进程ID的pid文件存放路径
#语法格式:pid path/file
#pid        logs/nginx.pid;

#事件类配置项
#一般有以下几种配置:
#1、是否打开accept锁
#   语法格式:accept_mutex [on | off];
#2、lock文件的路径
#   语法格式:lock_file  path/file;
#3、使用accept锁后到真正建立连接之间的延迟时间
#   语法格式:accept_mutex_delay Nms;
#4、批量建立新连接
#   语法格式:multi_accept [on | off];
#5、选择事件模型
#   语法格式:use [kqueue | rtisg | epoll | /dev/poll | select | poll | eventport];
#6、每个worker进行的最大连接数
#   语法格式:worker_connections number;
events {
    worker_connections  1024;
}

#以下是http模块
http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

#server块
#   每个server块就是一个虚拟主机,按照server_name来区分
    server {
#监听端口
        listen       80;
#主机名称
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
#location语法:location[= | ~ | ~* | ^~ | @] /uri/ {}
        #location尝试根据用户请求中的URI来匹配 /uri表达式,若匹配成功,则执行{}里面的配置来处理用户请求
#以下是location的一般配置项
#1、以root方式设置资源路径
#   语法格式:root path;
#2、以alias方式设置资源路径
#   语法格式:alias path;
#3、访问首页
#   语法格式:index file...;
#4、根据HTTP返回码重定向页面
#   语法格式:error_page code [code...] [= | =answer-code] uri | @named_location;
#5、是否允许递归使用error_page
#   语法格式:recursive_error_pages [on | off];
#6、try_files
#   语法格式:try_files path1 [path2] uri;
        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

参考文献:

「nginxのの深い理解」

nginxのモジュール開発入門します

マスターへのエントリからnginxの開発。」

https://www.kancloud.cn/digest/understandingnginx/202587

贈り物を取得するにはクリックしてください