• 解读nginx配置


    nginx配置指令

      main配置段常见的配置指令
      分类:
        正常运行必备的配置
        优化性能相关的配置
        用于调试及定位问题相关的配置
        事件驱动相关的配置

      正常运行必备的配置
       1、user
        Syntax:user user [group]:运行nginx所使用的用户
        Default:user nobody nobody
        Context:main

       2、pid /PATH/TO/PID_FILE
        指定存储nginx主进程进程号码的文件路径

       3、include file | mask;
        指明包含进来的其他配置文件片段;

       4、load_module file;
        指明要装载的动态模块

      性能优化相关的配置:
       1、worker_processes number | auto;
        worker进程的数量,通常应该等于小于当前主机的cpu的物理核心数
        auto:当前主机物理CPU核心数;

       2、work_cpu_affinity cpumask...;
        work_cpu_affinity auto [cpumask];

    nginx进程的cpu亲缘性;
      CPU MASK:
      00000000;
      0000 0001:0号CPU
      0000 0010:1号CPU
      0000 0100:2号CPU
      0000 1000:3号CPU
      0001 0000:4号CPU
      ...
      0000 0011:0、1号CPU

        3、work_priority number;
          指定worker进程的nice值,设定worker进程优先级[-20,20],一般为负值,提示优先级

        4、worker_rlimit_nofile number;
          worker进程所能够打开的文件数量上限;(大多数设置为65535),提醒|对于高并发的服务器配置 至关重要

      调试、定位问题:
        1、daemon on|off;
          是否以守护进程方式运行nginx;

        2、master_process on|off;
          是否已master/worker模型运行nginx;默认为on;

        3、error_log file [level];

      事件驱动相关的配置
        events {
          ...
        }

        1、worker_connections number;
          每个worker进程所能够打开的最大并发连接数数量;

          worker_processes * worker_connections

        2、use method
          指明并发连接请求的处理方法;

          usr epoll;

        3、accept_mutex on | off;(互斥锁)
          处理新的连接请求的方法;on意味着由各worker轮流处理新请求,off意味着每个新请求的到达都会通知所有的worker进程;

      http协议的相关配置:
        http {
          ...
          server {
            ...
            server_name
            root
            location [operator] /url {
            ...
            }
          }
          server {
            ...
          }
        }

    <Directoty "">
    可定义访问权限
    <Directoty>

    <Location "">
    <Location>

    与套接字相关的配置;

      1、server {...}
        配置以各虚拟主机;

    server {
      listen PORT;
      server_name localhost;
      root /PATH/TO/DOCUMENT_ROOT;
      (如果是作为代理服务器,那么需要将root选项改为proxy )
    }

        2、listen PORT|address[port]|unix:/PATH/TO/SCCKET_FILE
          listen address[:port] [default_server] [ssl] [http2 | spdy] [backlog=number] [rcvbuf=size][sndbuf=size]
          degault_server:设定为默认虚拟主机;
          ssl:限制仅能够通过ssl连接提供服务;
          backlog=number:后援队列长度;
          rcvbuf=size:接受缓冲区大小;
          sndbuf=size:发送缓冲区大小;

    3、server_name name...;
      指明虚拟主机的主机名称;后可跟多个由空白字符分隔的字符串;
      支持*通配任意长度的任意字符;server_name *.fqszywz.top www.fqszywz.*
      支持~起始的字符做正则表达式模式匹配;server_name ~,^wwwd+.fqszywz.com$

      匹配机制:
      (1)首先是字符串精确匹配;
      (2)左侧*通配符
      (3)右侧*通配符
      (4)正则表达式

    4、tcp_nodelay on | off;
      在keepalived模式下的连接是否启用TCP_NODELAY选项;

      tcp_nopush on | off;
      在sendfile模式下,是否启用TCP_CORK选项;

    5、sendfie on | off;
      是否启用sendfile功能;

      定义路径相关的配置
    6、root_path;
      设置web资源路径映射;用于指明用户请求的url所对应的本地文件系统上的文档所在目录路径;可用的位置有http、server、location,if in location;

    7、location [= | ~ | ~* | ^~ ] uri {...}

      在一个server中location配置段可存在多个,用于实现从uri到文件系统的路径映射;nginx会根据用户请求的URI来检查定义的所有location,并找出一个最佳匹配,而后应用其配置;

      #:对URI做精确匹配;例如:http://www/fqszywz.com/,http://www.fqszywz.com/index.html
      location = /{
      ...
      }
      ~:对URI做正则表达式模式匹配,区分字符大小写;
      ~*:对URI做正则表达式模式匹配,不区分字符大小写;
      ^~:对URI的左边部分做匹配检查,不区分字符大小写;
      不带符号:以URI为前缀的所有uri;

      匹配优先级:=,^~,~/~*,不带符号;

      root /vhosts/www/htdocs/
      http://www.fqszywz.com/index.html --> /vhosts/www/htdocs/index.html

    8、alias path;
      定义路径别名,文档映射的另一种机制;仅能用于location上下文

      注意:location中使用root指定和alias指令的意义不同;
      local /images/ {
      root /nginx/html/;
      alias /nginx/html/
      }
      (a)root,给定的路径对于location中的/uri/左侧的/;
        即 访问的路径为/nginx/html/images/index.html
      (b)alias,给定的路径对应于location中的/uri/右侧的/;
        即 访问的路径为/nginx/html/index.html

    9、index file ...;
      默认资源:http,server,location;

    10、error_page code ...[=[response]] uri;

      error_page 404 /404.html;
      location = /404.html {
      root /www/error_pages;
      }

    11、client_body_temp_path path[level1 [level2 [level3]]];
      设定用于村粗客户端请求报文的body部分的临时存储路径及子目录结构和数量;

      16进制的数字
      client_body_temp_path /var/tmp/client_body 1 2 2
      1:表示用一位16进制数字表示一级子目录:0-f
      2:表示用2位16进制数字表示二级子目录:00-ff
      3:表示用2位16进制数字表示三级子目录:00-ff

      对客户端进行限制的相关配置
    12、limit_rate rate;
      限制响应给客户端的传输速率,单位是bytes/second,0表示无限制;

    13、limit_except method ...{...}
      限制对指定的请求方法之外的其他方法的使用客户端

      limit_except GET{
      allow 192.168.1.0/24;
      deny all;
      }

      文件操作优化的配置
    14、aio on | off |threads=[pool]
    是否启用aio功能

    15、directio size |off;
      在Linux主机启动O_DIRECT标记,此处意味文件大于等于给定的大小时使用,例如directio 4m;

    16、open_file_cache off;
      open_file_cache max=N [inactive=time];

      nginx可以缓存一下三种信息
      (1)文件的描述符、文件大小和最近一次的修改时间
      (2)打开的目录结构;
      (3)没有找到的或者没有权限访问的文件的相关信息;

      max=N:可混村的缓存项上限;达到上限后会使用LRU算法实现缓存管理;

      inactive=time:缓存想的非活动时长,在此处指定的时长内未被命中的或命中的次数少于open_file_cache_min_uses指令所指定的次数的缓存项即为非活动项;

    17、open_file_cache_valid time;
      缓存项有效性的检查频率;默认为60s;

    18、open_file_cache_min_uses number;
      在open_file_cache指令的inactive参数指定的时长内,至少应该被命中多少次方可被认为是活动项

    19、open_file_cace_error on | off
      是否缓存查找时发生错误的文件一类的信息;

      ngx_http_access_module模块
      实现基于IP的访问控制功能

    20、allow address | CIDR |unix:|all;
    21、deny address | CIDR |unix: |all;

      http,server,location,limit_except

      ngx_http_auth_basic_module模块
      实现基于用户的访问控制,使用basic机制进行用户认证;

    22、auth_basic string | off;
    23、auth_basic_user_file file;

    location /admin/ {
    alias /webapps/app1/data/;
    auth_basic "Admin Area"
    auth_basic_user_file /etc/nginx/.ngxpasswd;
    }
    注意:htpasswd命令由httpd-tools所提供;

    ngx_http_stub_status_module模块
    用于输出nginx的基本状态信息;

    Active connectopns:291
    server accepts handled requests
    16630948 16630948 31070465
    Reading:6 Writing:179 Waiting:106

    Active connections:活动状态的连接数
    accepts:已经接受的客户端请求的总数;
    handled:已经处理完成的客户端请求的总数;
    requests:客户段发来的总的请求数;
    Reading:处于读取客户端请求报文首部的连接的连接数;
    Writing:处于想客户端发送响应报文过程中的连接数;
    Waiting:处于等待客户端发出请求的空闲连接数;

    24、stub_status;

    配置实例:
    location /basic_status{
    stub_status;
    }

    ngx_http_log_module模块
    he ngx_http_log_module module writes request log in the specified formad

    25、log_format name string ...;
      string可以使用nginx核心模块及其他模块内嵌的变量;

    26、access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
      access_log off;

    访问日志文件路径,格式及相关的缓冲的配置;
    buffer=size
    flush=time

    $remote_addr 客户端地址 219.227.111.255
    $remote_user 客户端用户名称 —
    $time_local 访问时间和时区 18/Jul/2014:17:00:01 +0800
    $request 请求的URI和HTTP协议 “GET /article-10000.html HTTP/1.1”
    $http_host 请求地址,即浏览器中你输入的地址(IP或域名) www.ha97.com 198.98.120.87
    $status HTTP请求状态 200
    $upstream_status upstream状态 200
    $body_bytes_sent 发送给客户端文件内容大小 1547
    $http_referer url跳转来源 https://www.google.com/
    $http_user_agent 用户终端浏览器等信息 “Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; SV1; GTB7.0; .NET4.0C;
    $ssl_protocol SSL协议版本 TLSv1
    $ssl_cipher 交换数据中的算法 RC4-SHA
    $upstream_addr 后台upstream的地址,即真正提供服务的主机地址 10.36.10.80:80
    $request_time 整个请求的总时间 0.165
    $upstream_response_time 请求过程中,upstream响应时间

    27、open_log_file_cache max=N [inactive=time] [min_uses=N] [Valid=time]


    open_log_file_cache off;

    缓存各日志文件相关的元数据信息;

    max:缓存的最大文件描述符数量;
    min_uses:在inactive指定的时长内访问大于等于此值方可被当作活动项;
    inactive:非活动时长
    valid:验证缓存中个缓存项是否为活动项的时间间隔;

    nginx(3)
    ngx_http_referer_module模块(防盗链)

    1、valid_referers none | blocker |server_names | string...;
    定义referer首部的合法可用值

    none:请求报文首部没有referer首部;
    blocked:请求报文的referer首部没有值;
    server_name:参数,其可以有值作为主机名或主机名模式;
    arbitrary_string:直接字符串,但可使用*作通配符;
    regular expression:被指定的正则表达式模式匹配到的字符串;要使用~打头,例如:~.*.fqszywz.com;
    配置实例:
    valid_referers none block server_names *.fqszywz.com *.ssz.com fqszywz.* ssz.* ~.fqszywz.;
    if ($invalid_referer) {
    return http://www.fqszywz.top
    }


    ngx_http_proxy_module模块

        The ngx_http_proxy_module module allows passing requests to another server.

    1、proxy_pass URL;


    Context:location,if in location,limit_except

    注意:proxy_pass后面的路径不带uri时,其会将location的uri传递给后端主机
    server {
    ...
    server_name HOSTNAME;
    location ~|~* /uri/{
    proxy http://host;
    }
    ...
    }

    http://HOSTNAME/uri/-->http://host/uri/;

    2、proxy_set_header field value;
      设定发往后端主机的请求报文的请求首部的值;Context:http,server,locationl

      proxy_set_header X-Real-IP $rempte_addr;
      proxy_set_header X-Forwarded-For $proxy-add_x_forwarded_for;

    3、proxy_cache_path
      定义可用于proxy功能的缓存;Context:http

    proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name;size [inactive=time] [max_size=size]
    [manager_files-number][manager_sleep=time][manager_threshold=time][loader_file=number][loader_sleep=time]
    [lader_threshold=time][purger=on|off][purger_file=number][purger_sleep=time][purger_thresgold=time];

    4、proxy_cache zone |off;
      指明要调用的缓存,或关闭缓存机制;Context:http,server,location

    5、proxy_cache_key string;
      缓存中用于"键"的内容;

      默认值:proxy_cache_key $scheme$proxy_host$request_uri;

    6、proxy_cache_purge string;
      清理缓存

      ngx_http_headers_module模块

      向由代理服务器响应给客户端的响应白问添加自定义首部,或修改指定首部的值;

    1、add_header name value [always];
      添加自定义首部;

      add_header X-Via $server-addr;
      add_header X_Accel $server_name;

    2、expires [modified] time;
      expores epoch | max | off;

    用于定义Expire或Cache-Control首部的值;

    ngx_http_upstream_module模块

    1、upstream name {...}
    定义后端服务器组;引入一个新的上下文;只能用于http{}上下文中;

    默认的调度方法是wrr;

    2、server address [permeters];


    定义服务器地址和相关的参数;
    地址格式:
    IP[:PORT]
    HOSTNAME[:PORT]
    unix:/PATH/TO/SOME_SOCK_FOLE

    参数
    weighe=number
    权重,默认1
    max_fails=number
    失败尝试的最大次数
    fail_timeout=time
    设置服务器为不可用状态的超时时长
    backup
    把服务器标记为"备用"状态;
    down
    手动标记其为不可用;


    3、least_conn;
      最少连接调度算法;当server拥有不同的权重时为wlc;当所有后端主机的连接数相同时,则使用wrr进行调度;

    4、ip_hash;
      源地址hash算法;能够将来自同一个源IP地址的请求始终发往同一个upstream server;

    5、hash key[consistent];
      基于指定的key的hash表实现请求调度,此处的key可以文本、变量或二者的组合

    consistent:参数,指定使用一致性hash算法;

    示例:
    hash $request_uri consistent
    hash $remote_addr
    hash $cookie_name

    6、keepalive connections;
      可使用长连接的连接数量:每worker与后端服务保持的最大空闲长连接数量;

  • 相关阅读:
    Failed to load resource: net::ERR_FILE_NOT_FOUND
    gulp安装详解
    npm install gulp-cli -g时npm ERR! code ECONNREFUSED
    webpack4.43
    修改cmd默认路径
    delphi设置鼠标图形
    Linux常用命令学习
    IO模型介绍 以及同步异步阻塞非阻塞的区别
    TCP的三次握手与四次挥手过程,各个状态名称与含义
    常见的设计模式详解:单例模式、工厂模式、观察者模式
  • 原文地址:https://www.cnblogs.com/fqszywz/p/12686380.html
Copyright © 2020-2023  润新知