nginx常用配置详解(1)

通过解析大神配置的nginx.conf,在实践中学习nginx配置。

1、worker_process/worker_connections
user   tengine;
worker_processes  1;
events {
    worker_connections  1500;
}

worker_processes:worker进程的数量; 如果没有特别大的性能消耗,通常设为1就足够了。
worker_connections:每个worker进程的连接数。

通过worker_processesworker_connnections可以计算出nginx的max_clients
默认情况下,max_clients=worker_processes * worker_connection。
作反向代理时,max_clients=worker_processes * worker_connections/4。

2、动态加载模块dso

这个模块主要是用来运行时动态加载模块,而不用每次都要重新编译Tengine。

  1. 动态加载模块的个数限制为128个
  2. 只有http模块支持动态加载模块。
dso {
 #   load ngx_http_rewrite_module.so;
    load ngx_http_reqstat_module.so;
    load ngx_http_access_module.so;
    load ngx_http_concat_module.so;
    load ngx_http_limit_conn_module.so;
    load ngx_http_limit_req_module.so;
    load ngx_http_sysguard_module.so;
    load ngx_http_upstream_session_sticky_module.so;
    load ngx_http_trim_filter_module.so;
}

动态加载的模块解析:

  1. ngx_http_rewrite_module 通过正则表达式重定向。
  2. ngx_http_reqstat_module 通过定义的变量来统计nginx的运行情况。
  3. ngx_http_access_module 通过deny和allow来限制客户端IP ,检查规则按照第一次匹配的IP。
  4. ngx_http_concat_module 合并多个文件在一个响应报文中。
  5. ngx_http_limit_conn_module 可以按照定义的键限定每个键值的连接数,也可以设定单一 IP 来源的连接数。只有那些正在被处理的请求所在的连接才会被计数。
  6. ngx_http_limit_req_module 可以按照定义的键限定每个键值的请求频率,也可以设定单一IP来源的请求频率。
  7. ngx_http_sysguard_module 该模块监控内存(含swap分区)、CPU 和请求的响应时间,当某些监控指标达到设定的阈值时,跳转的到指定的url。目前该模块仅对系统支持sysinfo函数时,才支持基于load与内存信息的保护,以及系统支持laodavg函数时支持基于load进行保护。模块需要从/proc文件系统中读取内存信息。
  8. ngx_http_upstream_session_sticky_module 该模块是一个负载均衡模块,通过cookie实现客户端与后端服务器的会话保持, 在一定条件下可以保证同一个客户端访问的都是同一个后端服务器。
  9. ngx_http_trim_filter_module 该模块用于删除 html , 内嵌 javascript 和 css 中的注释以及重复的空白符。
3、http模块

由于http模块内容较多,将会分块解析,第三点内容内的信息均属于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" "$proxy_host:$proxy_port" "$upstream_addr"';
    access_log  logs/access.log.$year$month$day  main;
    sendfile        on;
    keepalive_timeout  65;
    client_max_body_size 50m;
    include /app/tengine/conf.d/*.conf;
    server_tag off;
    
    #gzip压缩功能设置
    gzip on;
    gzip_min_length 500;
    gzip_buffers 32 64k;
    #gzip_http_version 1.0;
    gzip_comp_level 6;
    gzip_types text/plain application/x-javascript application/javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
    gzip_vary off;
    gzip_disable "MSIE [1-6]\.";
    req_status_zone server "$host,$server_addr:$server_port" 10M;
  1. include mime.types; #MIME是网络资源的媒体类型,使用include指令导入mime.types文件
  2. default_type application/octet-stream; 指定默认MIME类型为application/octet-stream,文件扩展名有bin、class、dms、exe、lha、lzh.
  3. log_format main '$remote_addr - r e m o t e u s e r [ remote_user [ time_local] “KaTeX parse error: Double superscript at position 30: … '̲status b o d y b y t e s s e n t " body_bytes_sent " http_referer” ’
    ’ “ h t t p u s e r a g e n t " " http_user_agent" " http_x_forwarded_for” “ p r o x y h o s t : proxy_host: proxy_port” "KaTeX parse error: Expected 'EOF', got '#' at position 37: … #̲ngx_http_log_mo…body_bytes_sent : 发送给客户端的字节数,不包括响应头的大小; 该变量与Apache模块mod_log_config里的“%B”参数兼容。
    $bytes_sent:发送给客户端的总字节数。
    $connection:连接的序列号。
    $connection_requests:当前通过一个连接获得的请求数量。
    $msec:日志写入时间。单位为秒,精度是毫秒。
    $pipe:如果请求是通过HTTP流水线(pipelined)发送,pipe值为“p”,否则为“.”。
    $request_length:请求的长度(包括请求行,请求头和请求正文)。
    $request_time:请求处理时间,单位为秒,精度毫秒; 从读入客户端的第一个字节开始,直到把最后一个字符发送给客户端后进行日志写入为止。
    $status:响应状态。
    $time_iso8601:ISO8601标准格式下的本地时间。
    $time_local:通用日志格式下的本地时间。*
    ===============================================================
    所以,配置文件中的配置含义如下:
用到的参数 含义
$remote_addr 客户端IP
$remote_user 客户端用户名称
$time_local 访问时间,格式如下13/Apr/2019:21:22:44 +0800
$request 请求的方式、url和http协议
$status 请求的状态码
$body_bytes_sent 发送给客户端文件主体的大小
$http_referer 记录从哪个页面链接访问过来的
$http_x_forwarded_for 客户端的IP地址
$proxy_host 打印请求的upstream的名称
$proxy_port 打印请求的upstream的端口
$upstream_addr 转发后的具体地址
  1. access_log logs/access.log. y e a r year month$day main; #使用名称为main的日志格式打印日志,并设置了日志名称。
  2. sendfile on; #允许sendfile方式来传输文件,默认为off,可以在http块,server块,location块
  3. keepalive_timeout 65; #连接超时时间,默认75s,可以在http,server,location块
  4. client_max_body_size 50m;#文件大小设置,默认为1m。
  5. include /app/tengine/conf.d/*.conf;#包含的子配置文件。
  6. server_tag off;#隐藏nginx的版本号。
  7. gizp on;#表示开启gzip ngx_http_gzip_module 模块是指使用gzip方法压缩响应的过滤器。通常能将传输数据的大小减小一半或者更多。
  8. gzip_min_length 500; #设置被压缩的响应的最小长度,这个长度是在响应头中的Content-Length字段获取的。
  9. gzip_buffers 32 64k; #设置用于压缩响应的缓冲区的数量和大小。此配置为32个缓冲区,大小为64k。
  10. gzip_http_version 1.0; #设置压缩响应所需的请求的最低HTTP版本。默认为1.1。
  11. gzip_comp_level 6; #设置响应的gzip压缩级别,范围为1-9。
  12. gzip_types text/plain application/x-javascript application/javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; #指定被压缩的MIME类型。
  13. gzip_vary off; #禁止在gzip模式下的响应头字段 “Vary: Accept-Encoding” 。
  14. gzip_disable “MSIE [1-6].”;#禁用请求头具有符合正则表达式“MSIE [1-6].”的gizp响应。
  15. req_status_zone server “ h o s t , host, server_addr:$server_port” 10M;#ngx_req_status用来展示nginx请求状态信息,req_status_zone定义请求状态ZONE,请求按照string分组来排列req_status_zone name string size。添加了心跳检测的应用,在路径后加上/req-status 可以看到统计页面,如下图:
    在这里插入图片描述这篇笔记只记录了nginx的主模块、events模块、http模块。
    upstream模块和server模块还需要继续学习吖。
发布了48 篇原创文章 · 获赞 31 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/weixin_44723434/article/details/90182072