Nginx 默认配置解析


# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
// core 和 events 是核心模块
user nginx; // 模块 core 语法 user user [group]
 // 指定 nginx worker 进程运行用户/组
worker_processes auto; // 模块 core 语法 worker_processes number
 // 启动 worker 进程数量. 1. 利用 SMP 2. 降低worker进程被IO阻塞时的延迟
error_log /var/log/nginx/error.log; // 模块 core 语法 error_log file [ debug | info | notice | warn | error | crit ]
 // 错误日志路径
pid /run/nginx.pid; // 模块 core
 // 进程id存储文件
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf; // 模块 core 语法 include file | *
 // 包含配置文件
// events 块 工作模式与连接数上限
events {
 worker_connections 1024; // 模块 events Syntax: worker_connections number
 // 单个进程最大连接数
 // 支持的最大并发连接 max_clients = worker_connections * worker_processes
 // 作为反向代理时的最大并发 max_clients = worker_processes * worker_connections/4 (浏览器默认打开 2 个连接)
}
// http 块
http {
 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' // 模块 httplog 语法 log_format name format [format ...] 作用域 http server
 '$status $body_bytes_sent "$http_referer" ' // 定义日志格式
 '"$http_user_agent" "$http_x_forwarded_for"';
 access_log /var/log/nginx/access.log main; // 模块 httplog 语法: access_log path [format [buffer=size | off ] 作用域: http, server, location
 // 定义日志路径, 格式
 sendfile on; // 模块 httpcore syntax: sendfile [ on|off ] context: http, server, location
 // 是否调用系统函数 sendfile() 发送文件
 tcp_nopush on; // 模块 httpcore syntax: tcp_nopush [on|off] context: http, server, location
 // 启用/禁用 socket 选项 linux 仅 sendfile on 可用
 tcp_nodelay on; // 启用/禁用 socket 选项
 keepalive_timeout 65; // 模块 httpcore syntax: keepalive_timeout [ time ] context: http, server, location
 // 长连接超时时间,单位是秒
 // 这个值会用于 header Keep-Alive: timeout=time
 // 浏览对于header Keep-Alive: timeout=time 处理
 // MSIE and Opera ignore the "Keep-Alive: timeout=<N>" header.
 // MSIE keeps the connection alive for about 60-65 seconds, then sends a TCP RST.
 // Opera keeps the connection alive for a long time.
 // Mozilla keeps the connection alive for N plus about 1-10 seconds.
 // Konqueror keeps the connection alive for about N seconds.
 types_hash_max_size 2048; // 模块 httpcore Syntax: types_hash_max_size size; Context: http, server, location
 // Sets the maximum size of the types hash tables
 // # types_hash_max_size 影响散列表的冲突率。types_hash_max_size越大,就会消耗更多的内存,但散列key的冲突率会降低,检索速度就更快。types_hash_max_size越小,消耗的内存就越小,但散列key的冲突率可能上升。
 include /etc/nginx/mime.types; // 加载 mime 文件后缀映射文件
 default_type application/octet-stream; // 模块 httpcore context: http, server, location
 // 默认 MIME type
 # Load modular configuration files from the /etc/nginx/conf.d directory.
 # See http://nginx.org/en/docs/ngx_core_module.html#include
 # for more information.
 include /etc/nginx/conf.d/*.conf; // 加载模块配置文件
 // server 块
 server {
 listen 80 default_server; // 模块 httpcore syntax: listen address:port [ default [ backlog=num | rcvbuf=size | sndbuf=size | accept_filter=filter | deferred | bind | ssl ] ] context: server
 // http 服务器监听地址和端口
 listen [::]:80 default_server;
 server_name _; // 模块 httpcore syntax: server_name name [... ] context: server
 // 参数的作用两个:
 // _ 匹配任意域名
 // "" 匹配Host 字段为空的请求
 // 1. 实现虚拟主机的功能: Nginx 将 server_name 和请求中host字段进行匹配, 成功就响应请求。 匹配顺序
 // 1.1 full, static names
 // 1.2 names with a wildcard at the start of the name — *.example.com
 // 1.3 names with a wildcard at the end of the name — www.example.*
 // 1.4 names with regular expressions
 // 1.5 如果以上都不匹配, 匹配 listen 指令, 且 listen 指令使用 default 参数, 或隐含使用default 参数 listen 80;
 // 2. 见 server_name_in_redirect 指令
 root /usr/share/nginx/html; // 模块 httpcore syntax: root path context: http, server, location, if in location
 // 主目录
 # Load configuration files for the default server block.
 include /etc/nginx/default.d/*.conf;
 // location 匹配块
 // ~ #波浪线表示执行一个正则匹配,区分大小写
 // ~* #表示执行一个正则匹配,不区分大小写
 // ^~ #^~表示普通字符匹配,如果该选项匹配,只匹配该选项,不匹配别的选项,一般用来匹配目录
 // = #进行普通字符精确匹配
 // @ #"@" 定义一个命名的 location,使用在内部定向时,例如 error_page, try_files
 // location 生效优先级
 // =前缀的指令严格匹配这个查询。如果找到,停止搜索。
 // 所有剩下的常规字符串,最长的匹配。如果这个匹配使用^〜前缀,搜索停止。
 // 正则表达式,在配置文件中定义的顺序。
 // 如果第3条规则产生匹配的话,结果被使用。否则,如同从第2条规则被使用。
 location / {
 }
 error_page 404 /404.html; // 模块 httpcore syntax: error_page code [ code... ] [ = | =answer-code ] uri | @named_location context: http, server, location, if in location
 // 定义 404 错误的响应页面
 location = /40x.html {
 }
 error_page 500 502 503 504 /50x.html;
 location = /50x.html {
 }
 }
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# // httpssl 模块指令
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}

猜你喜欢

转载自www.cnblogs.com/jpfss/p/10242655.html