一、nginx的配置文件

1、nginx配置文件

[root@Node7 ~]# cd /etc/nginx/
[root@Node7 nginx]# ls
fastcgi.conf          fastcgi_params.default  mime.types          nginx.conf.default   uwsgi_params
fastcgi.conf.default  koi-utf                 mime.types.default  scgi_params          uwsgi_params.default
fastcgi_params        koi-win                 nginx.conf          scgi_params.default  win-utf

2、Nginx的主配置文件/etc/nginx/nginx.conf的结构

Nginx的配置文件/etc/nginx/nginx.conf分为2段:

  main配置段:     

     main段:

         设置的指令设置全局属性

         包含events段: 

            events定义io工作模式

  http配置段:

    http{


       }


      http段:

          配置nginx如何处理http请求(做反向代理服务器还是做web服务器)

         包含server段upstream段

            server段:

                主要用于主机设置

                又包括location段:

                   用于匹配url,并定义如何处理

            upstream段:

                主要用于负载均衡,设置一系列的后端服务器;

  每个段都包含若干指令,这些指令主要包含Nginx的主模块指令、事件模块指令、HTTP核心模块指令。同时每个部分还可以使用其他HTTP模块指令,例如Http SSL模块、Http Gzip Static模块和Http Addition模块等。

注意:

   每个段都可以出现多次


这个段通常也被称为nginx的上下文,每个段的定义格式如下所示:

<section> {

   <directive>  <parameters>;

}

注意:

   其每一个指令都必须使用分号(;)结束,否则为语法错误

语法格式:

   参数名 值1 [值2...];

支持使用变量:

    模块内置变量

    用户自定义变量

格式:set var_name value;


二、main段详解

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}

1、正常运行的必备配置

 1)user username [groupname];

     以哪个用户身份运行,编译安装时指定了用户和组,如果想改变在这里定义就可以

 2)pid /path/to/pid_filename;    

     指定nginx的pid文件

 3)worker_rlimit_nofile N;

     指定一个worker进程所能够打开的最大文件句柄数

Syntax:worker_rlimit_nofile number;
Default:
Context:main

 4)worker_rlimit_sigpending N;

     设定每个用户能够发往worker进程的信号的数量(设定信号队列的大小);(已被废弃?)

注意:

   这面上些指令如果没有指定的话,则默认以编译时给的参数为准


2、优化性能相关的配置

 1)worker_procrsses N;

     work进程的个数,通常其数值应该为cpu的物理核心数减1

(注,如果负载以CPU密集型应用为主,如SSL或压缩应用,则worker数应为CPU数 - 1;

如果负载以IO密集型为主,如响应大量内容给客户端,则worker数应该为CPU个数的1.5或2倍。)

 2)worker_cpu_affinity CPUMASK ....;     # cpu亲缘性

    0000  # cpu掩码,如果4个cpu核心则用0000表示,从右到左依次对应第1个...第4个cpu核心

    0001  #第1个cpu核心

    0010  #第2个

    0100

    1000

官方文档:

Syntax:worker_cpu_affinity cpumask ...;
worker_cpu_affinity auto [cpumask];
Default:
Context:main

Binds worker processes to the sets of CPUs. Each CPU set is represented by a bitmask of allowed CPUs. There should be a separate set defined for each of the worker processes. By default, worker processes are not bound to any specific CPUs.

For example,

worker_processes    4;
worker_cpu_affinity 0001 0010 0100 1000;

binds each worker process to a separate CPU, while

worker_processes    2;
worker_cpu_affinity 0101 1010;

binds the first worker process to CPU0/CPU2, and the second worker process to CPU1/CPU3. The second example is suitable for hyper-threading.(第二个例子适合超线程)

The special value auto (1.9.10) allows binding worker processes automatically to available CPUs:

  1.9.10版本后允许使用auto自动绑定到可用的cpu上

worker_processes auto;
worker_cpu_affinity auto;

The optional mask parameter can be used to limit the CPUs available for automatic binding:

worker_cpu_affinity auto 01010101;
The directive(指定) is only available on FreeBSD and Linux.

work_procrsses 6;  

    # 保证了这6个进程依次在这6个cpu上运行,不会切换,但这些cpu可能会运行别的进程

worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000;

 3)ssl_engine DEVICE;

      用在存在ssl硬件加速器的服务器上,指定所使用的ssl硬件加速设备

 4)timer_resolution interval;

      每次内核事件调用返回时,都会使用gettimeday()来更新nginx缓存时钟;

     timer_resolution用于定义每隔多久才会由gettimeday()更新一次缓存时钟;x86_64系统上,gettimeday()代价已经很小,可以忽略此配置

Example:

timer_resolution 100ms;

 5) worker_priority nice;

      工作进程的优先级:-20到19之间的值,值越小越优先调用

Syntax:worker_priority number;
Default:
worker_priority 0;
Context:main

Example:

worker_priority -10;


3、跟事件相关的配置

 1)accept_mutex [on|off];

   是否打开nginx的负载均衡锁,此锁能够让多个worker进行轮流地、序列化地与新的客户端建立连接;而通常当一个worker进程的负载达到其上限的7/8,master就尽可能不将请求调度至worker

Syntax:accept_mutex on | off;
Default:
accept_mutex off;
Context:events

If accept_mutex is enabled, worker processes will accept new connections by turn. Otherwise, all worker processes will be notified about new connections, and if volume of new connections is low, some of the worker processes may just waste system resources.

There is no need to enable  accept_mutex on systems that support the  EPOLLEXCLUSIVE flag (1.11.3) or when using  reuseport.
Prior to version 1.11.3, the default value was  on.

 2)lock_file /path/to/lock_file;

     指定锁文件的路径

Syntax:lock_file file;
Default:
lock_file logs/nginx.lock;
Context:main

 3)accept_mutex_delay time

Syntax:accept_mutex_delay time;
Default:
accept_mutex_delay 500ms;
Context:events

  使用accept锁以后,一个worker进程为取得accept锁的等待时长,即用户建立等待的时间;如果某worker进程在某次试图取得锁时失败了(其它进程正在使用),至少要等待Nms才能再一次请求锁

 4)multi_accept on|off;

     是否允许一次性地响应多个用户请求,默认为off

 5)use [epoll|rtsig|select|poll];  # 关键

Syntax:use method;
Default:
Context:events

Specifies the connection processing method to use. There is normally no need to specify it explicitly, because nginx will by default use the most efficient method.

    指定要使用的连接处理方法。通常不需要显式地指定它,因为nginx将默认使用最有效的方法。

 6)worker_connections N;

     每个worker能够并发响应的最大请求数

          如果为代理服务器的话(1个连接需要维持2个套接字):

                  worker_rlimit_nofile = worker_commections*2


Syntax:worker_connections number;
Default:
worker_connections 512;
Context:events


4、用于调试、定位问题

    只在调试nginx时使用

 1)daemon [on|off];

    关闭提供守护进程的模式,是否让nignx运行于后台;调试时应该为off,使得所有信息直接输出在控制台,默认为on

Syntax:daemon on | off;
Default:
daemon on;
Context:main

 2)master_process on|off;

    是否以master/worker模式运行nginx,默认为on,调试时可以设置为off以方便追踪

 3)error_log /path/to/error_log LEVEL;

    错误日志文件及其级别,调试时可以使用debug级别,但要求在编译时必须使用--with-debug启用debug功能,默认通常为error级别

(日志输出级别有debug、info、notice、warn、error、crit可供选择,其中,debug输出日志最为最详细,而crit输出日志最少)

Syntax:error_log file [level];
Default:
error_log logs/error.log error;
Context:mainhttpmailstreamserverlocation


三、server段配置详解

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 {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        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;
        #}
    }

注意:

   nginx必须使用虚拟机来配置站点,每个虚拟主机使用一个server{ }段来配置:

server{
}

非虚拟主机的配置和公共选项,需要定义在server之外,http之内:

http{
  directive value;
    ....
      server{
            location / {
                    }
            }
         server{
            }
          ......
 }

1、server{ }

    定义一个虚拟主机,基于主机名,ip,port


2、listen

  listen address[:port];

  listen prot;          # 监听所有地址的该端口

  listen unix:sock;

  default_server:定义此server为http中默认的server;如果所有的server中没有任何一个listen使用此参数,那么第一个server即为默认server

  rcvbuf=SIZE:接收缓存大小

  sndbuf=SIZE: 发送缓存大小

  ssl:https server:必须以ssl连接

           各参数用空格分开

Syntax:listen address[:port] [default_server] [ssl] [http2 | spdy] [proxy_protocol] [setfib=number] [fastopen=number] [backlog=number] [rcvbuf=size] [sndbuf=size] [accept_filter=filter] [deferred] [bind] [ipv6only=on|off] [reuseport] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];
listen port [default_server] [ssl] [http2 | spdy] [proxy_protocol] [setfib=number] [fastopen=number] [backlog=number] [rcvbuf=size] [sndbuf=size] [accept_filter=filter] [deferred] [bind] [ipv6only=on|off] [reuseport] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];
listen unix:path [default_server] [ssl] [http2 | spdy] [proxy_protocol] [backlog=number] [rcvbuf=size] [sndbuf=size] [accept_filter=filter] [deferred] [bind] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];
Default:
listen *:80 | *:8000;
Context:server

3、server_name

  server_name可以跟多个主机名,名称可以使用通配符和正则表达式(通常以~开头);当nginx收到一个请求时,会取出其首部的server的值,而后跟众server_name进行比较

比较方式:

   (1) 先做精确匹配   www.magedu.com

   (2) 左侧通配符匹配  *.magedu.com

   (3) 右侧通配符匹配  www.*

   (4) 正则表达式匹配   nginx配置文件中支持的是扩展正则表达式

Syntax:server_name name ...;
Default:
server_name "";
Context:server

Sets names of a virtual server, for example:

server {
    server_name example.com www.example.com;
}

The first name becomes the primary server name.

Server names can include an asterisk (“*”) replacing the first or last part of a name:

server {
    server_name example.com *.example.com www.example.*;
}

Such names are called wildcard names.

The first two of the names mentioned above can be combined in one:

server {
    server_name .example.com;
}

It is also possible to use regular expressions in server names, preceding the name with a tilde (“~”):   # 使用正则表达式匹配需要使用~开头

server {
    server_name www.example.com ~^www\d+\.example\.com$;
}

Regular expressions can contain captures (0.7.40) that can later be used in other directives:

server {
    server_name ~^(www\.)?(.+)$;

    location / {
        root /sites/$2;
    }
}

server {
    server_name _;

    location / {
        root /sites/default;
    }
}

4、server_name_hash_bucket_size 32|64|128;

     为了实现快速主机名查找,nginx使用hash表来保存主机名

Syntax:server_names_hash_bucket_size size;
Default:
server_names_hash_bucket_size 32|64|128;
Context:http


Syntax:server_names_hash_max_size size;
Default:
server_names_hash_max_size 512;
Context:http


5、location 

Syntax:location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
Default:
Context:serverlocation

location有两种用法:

  只学习第一种:

    location [ =|~|~*|^~] uri {参数 ... };

功能:

   使用各location匹配用户请求的URI,以进行访问属性配置;匹配到时,将被location块中的配置所处理

  =:精确匹配

  ~:正则表达式模式匹配,匹配时区分字符大小写

 ~*:正则表达式模式匹配,匹配时忽略字符大小写

 ^~:只需要前半部分与uri匹配即可,做逐字符匹配,不做正则表达式匹配

匹配优先级:

   "字符字面量最精确匹配" > 正则表达式检索(有多个时,由第一个正则表达式匹配到的所处理)

Let’s illustrate the above by an example:

location = / {                    # url中/后带任何字符就都匹配不到
    [ configuration A ]
}

location / {                       # 常用于设置默认规则,所有规则都匹配不到时,就用该配置
    [ configuration B ]
}

location /documents/ {
    [ configuration C ]
}

location ^~ /p_w_picpaths/ {
    [ configuration D ]
}

location ~* \.(gif|jpg|jpeg)$ {
    [ configuration E ]
}

The “/” request will match configuration A, the “/index.html” request will match configuration B, the “/documents/document.html” request will match configuration C, the “/p_w_picpaths/1.gif” request will match configuration D, and the “/documents/1.jpg” request will match configuration E.


  /后不接任何字符将匹配到配置A,/index.html匹配到B,不使用任何符号和使用^~的效果是一样的?只要以/开头的uri都将匹配到,


四、location段中常用的配置指令

   当http和server中都没有定义root,每个location中一定要有root或alias指定站点页面文件的路径(就是Location中不能向http和server中继承root指令时,location下一定要指定root或alias)

1、文件路径定义

 1)root PATH

     设置web资源路径,用于指定请求的根文档目录,从根开始匹配

Syntax:root path;
Default:
root html;
Context:httpserverlocationif in location

Sets the root directory for requests. For example, with the following configuration

location /i/ {
    root /data/w3;
}

The /data/w3/i/top.gif file will be sent in response to the “/i/top.gif” request.

The path value can contain variables, except $document_root and $realpath_root.

A path to the file is constructed by merely adding a URI to the value of the root directive. If a URI has to be modified, the alias directive should be used.


例如:

location / {
      root /www/htdocs;            
     }
location ^~ /p_w_picpaths/ {
       root /web;              # root指定的是根,以/web为根开始匹配
      }

可以匹配到http://www.magedu.com/p_w_picpaths/b.html,但资源实际存放位置是/web/imges/就是相当于路径延长了。

 2)alias

      指定路径别名,只能用于location中,从最后一个/开始匹配

Syntax:alias path;
Default:
Context:location

例如:

location ^~ /p_w_picpaths/ {                  # 注意,这里使用后面带了/
          alias /web/;             # 那么这里后面也一定要带/,否则找不到
      }

   可以匹配到http://www.magedu.com/p_w_picpaths/b.html,但资源实际存放位置是/web,url中用/p_w_picpaths映射/web


如果使用/p_w_picpaths/和/web将报错:

2017/03/09 15:32:47 [error] 2034#0: *7 open() "/webb.html" failed (2: No such file or directory), client: 192.168.10.10, server: localhost, request: "GET /p_w_picpaths/b.html HTTP/1.1", host: "192.168.10.7"

而/p_w_picpaths和/web/不会报错,都不带/也不报错。

 3)index

     定义默认主页,可以跟多个值,自左向右匹配

location / {
    index index.$geo.html index.html;
}
Syntax:index file ...;
Default:
index index.html;
Context:httpserverlocation

 4)error_page

    当对于某个请求发回错误时,如果匹配上了error_page指令中设定的code,则从定向至新的新URI中;错误页面重定向

Syntax:error_page code ... [=[response]] uri;
Default:
Context:httpserverlocationif in location

Defines the URI that will be shown for the specified errors. A uri value can contain variables.

Example:

error_page 404             /404.html;
error_page 500 502 503 504 /50x.html;

This causes an internal redirect to the specified uri with the client request method changed to “GET” (for all methods other than “GET” and “HEAD”).

Furthermore, it is possible to change the response code to another using the “=response” syntax, for example:

error_page 404 =200 /empty.gif;

 5)try_files 

    自左向右尝试读取所指定的文件,在第一找到即停止并返回,如果所有path均不存在,则返回最后一个uri

   也是错误重定向咯

Syntax:try_files file ... uri;
try_files file ... =code;
Default:
Context:serverlocation
location /documents/(.*)$ {
    try_files $uri /docu/$1  /documents/default.gif;    # 可以引用
}

location = /p_w_picpaths/default.gif {
    expires 30s;
}

The last parameter can also point to a named location, as shown in examples below. Starting from version 0.7.51, the last parameter can also be a code:

location / {
    try_files $uri $uri/index.html $uri.html =404;
}

例子:

location ~* ^/document/(.*)${
    root /www/htdocs
    try_files $uri /docu/$1 /temp.html
}

表示依次匹配:

  http://www.magedu.com/documents/a.html

  http://www.magedu.com/docu/a.html

  http://www.magedu.com/temp.html


2、网络连接相关的设置

 1)keepalive_timeout time;

     保持连接的超时时长,默认为75s

Syntax:keepalive_timeout timeout [header_timeout];
Default:
keepalive_timeout 75s;
Context:httpserverlocation

 2)keepalive_requests N;

     在一次长连接上允许承载的最大请求数

Syntax:keepalive_requests number;
Default:
keepalive_requests 100;
Context:httpserverlocation

  This directive appeared in version 0.8.0.

 3)keepalive_disable [msie6|safari |none];

     对指定的浏览器禁止使用长连接

Syntax:keepalive_disable none | browser ...;
Default:
keepalive_disable msie6;
Context:httpserverlocation

  我们发现nginx不像httpd一样直接有指令keepalive off/on的指令,那nginx如何关闭keepalive呢?

  设置keepalive_timeout 0或keepalive_requests 0即可

 4)tcp_nodelay on|off

     对keepalive连接是否使用tcp_nodelay选项

     tcp协议为了提高性能,tcp_delay将多个确认报文合在一起一次性响应给客户,但在keepalive中用户会以为之前请求的资源无法找到,所以要启用tcp_nodelay

Syntax:tcp_nodelay on | off;
Default:
tcp_nodelay on;
Context:httpserverlocation

Enables or disables the use of the TCP_NODELAY option. The option is enabled only when a connection is transitioned into the keep-alive state.

 5)client_header_timeout time;

     读取http请求首部的超时时长


Syntax:client_header_timeout time;
Default:
client_header_timeout 60s;
Context:httpserver

Defines a timeout for reading client request header. If a client does not transmit the entire header within this time, the 408 (Request Time-out) error is returned to the client.

 6)client_body_timeout time

      读取http请求包体的超时时间

Syntax:client_body_timeout time;
Default:
client_body_timeout 60s;
Context:httpserverlocation

 7)send_timeout time;

     发送响应的超时时长

Syntax:send_timeout time;
Default:
send_timeout 60s;
Context:httpserverlocation


3、对客户端请求的限制

 1)limit_except method ...{ ... }

      指定范围之外的其他方法的访问控制,只能用于location中

Syntax:limit_except method ... { ... }
Default:
Context:location

Limits allowed HTTP methods inside a location. The method parameter can be one of the following:GETHEADPOSTPUTDELETEMKCOLCOPYMOVEOPTIONSPROPFINDPROPPATCHLOCKUNLOCK, or PATCH. Allowing the GET method makes the HEAD method also allowed. Access to other methods can be limited using the ngx_http_access_module and ngx_http_auth_basic_module modules directives:

limit_except GET {         # 就是说除了GET和head方法外,只运行192.168.1.0/24这个段内ip访问
    allow 192.168.1.0/24;
    deny  all;
}

Please note that this will limit access to all methods except GET and HEAD.

 2)client_max_body_size SIZE;

     http请求包体的最大值,常用于限定客户端所能够请求的最大包体,根据请求首部中的Content-Length来检查,以避免无用的传输

Syntax:client_max_body_size size;
Default:
client_max_body_size 1m;
Context:httpserverlocation

 3)limit_rate speed;

     限制客户端每秒传输的字节数,默认为0,表示没有限制

Syntax:limit_rate rate;
Default:
limit_rate 0;
Context:httpserverlocationif in location

 4)limit_rate_after time;

    nginx向客户端发送响应报文时,如果时长超过了此处指定的时长,则后续的发送过程开始限速

Syntax:limit_rate_after size;
Default:
limit_rate_after 0;
Context:httpserverlocationif in location

This directive appeared in version 0.8.0.

Sets the initial amount after which the further transmission of a response to a client will be rate limited.

Example:

location /flv/ {
    flv;
    limit_rate_after 500k;
    limit_rate       50k;
}


4、文件操作的优化

 1)sendfile on|off

      是否启用sendfile功能

Syntax:sendfile on | off;
Default:
sendfile off;
Context:httpserverlocationif in location

 2)aio on|off

      是否启用aio功能

Syntax:aio on | off | threads[=pool];
Default:
aio off;
Context:httpserverlocation

This directive appeared in version 0.8.11.

 3)open_file_cache max=N [incative=time]|off

     是否打开文件缓存功能

Syntax:open_file_cache off;
open_file_cache max=N [inactive=time];
Default:
open_file_cache off;
Context:httpserverlocation

Configures a cache that can store:   # 配置缓存可以存储

  • open file descriptors, their sizes and modification times;

  • information on existence of directories;

  • file lookup errors, such as “file not found”, “no read permission”, and so on.

    Caching of errors should be enabled separately by the  open_file_cache_errors directive.

  文件查找错误,如"未找到文件","没有读取权限"等信息是否缓存由open_file_cache_errors决定

The directive has the following parameters:   

  • max

  • sets the maximum number of elements in the cache; on cache overflow the least recently used (LRU) elements are removed;

  • inactive

  • defines a time after which an element is removed from the cache if it has not been accessed during this time; by default, it is 60 seconds;

  • off

  • disables the cache.

Example:

open_file_cache          max=1000 inactive=20s;
open_file_cache_valid    30s;
open_file_cache_min_uses 2;
open_file_cache_errors   on;

max:用于指定缓存条目的最大值,允许打开的缓存条目最大数,当满了以后将根据LRU(最小最少连接数)算法进行置换

inactive:懒惰,无效的;某缓存条目在指定时长内没有被访问过时,将自动被删除;通常默认为60s

缓存的信息包括:

   打开的文件描述符,它们的大小和修改时间;

   目录存在性信息; 

   文件查找错误,如"未找到文件","没有读取权限"等。

 4)open_file_cache_errors on|off

    是否缓存文件找不到或没有权限访问等相关信息

Syntax:open_file_cache_errors on | off;
Default:
open_file_cache_errors off;
Context:httpserverlocation

Enables or disables caching of file lookup errors by open_file_cache.

    是否缓存取决于open_file_cache指定

 5)open_file_cache_valid time;

    多长时间检查一次缓存中的条目是否超出非活动时长,默认为60s

Syntax:open_file_cache_min_uses number;
Default:
open_file_cache_min_uses 1;
Context:httpserverlocation

Sets the minimum number of file accesses during the period configured by the inactive parameter of the open_file_cache directive, required for a file descriptor to remain open in the cache.

 6)open_file_cache_min_use N;

    在inactive指定的时长内被访问超过此处指定的次数时,才不会被删除

Syntax:open_file_cache_valid time;
Default:
open_file_cache_valid 60s;
Context:httpserverlocation

Sets a time after which open_file_cache elements should be validated


5、对客户端请求的特殊处理

 1)ignore_invalid_headers on|off;

     是否忽略不合法的http首部,默认为on,

     off意味着请求首部中出现不合规的首部将拒绝响应,只能用于server和http

 2)log_not_found on|off

    用户访问的文件不存在时,是否将其记录到错误日志中

Syntax:log_not_found on | off;
Default:
log_not_found on;
Context:httpserverlocation

 3)resolver address;

     指定nginx使用的dns服务器地址,用于上游服务器的名称解析

Syntax:resolver address ... [valid=time] [ipv6=on|off];
Default:
Context:httpserverlocation

Configures name servers used to resolve names of upstream servers into addresses, for example:

resolver 127.0.0.1 [::1]:5353;

 4)resolve timeout

     指定DNS解析超时时长,默认为30s

Syntax:resolver_timeout time;
Default:
resolver_timeout 30s;
Context:httpserverlocation

 5)server_tokens on|off

    是否在错误页面中显示nginx的版本号,但还是会显示所使用的软件(nginx)


Syntax:server_tokens on | off | build | string;
Default:
server_tokens on;
Context:httpserverlocation

测试值只能使用on和off


6、跟内存及磁盘资源分配相关

 1)client_body_in_file_only on|clean|off

   客户端的包体是否存储在磁盘中,非off时表示存储;既使包体为0,也要存储在硬盘上;

   如果为on,请求结束后包体文件不会被删除,clean表示删除,默认为off(上传时才应该打开)

Syntax:client_body_in_file_only on | clean | off;
Default:
client_body_in_file_only off;
Context:httpserverlocation

 2)client_body_in_single_buffer on|off

     http的包体是否存储在内存buffer当中,默认为off

 3)client_body_buffer_size size; 

     nginx接受http包体的内存缓存区大小

Syntax:client_body_buffer_size size;
Default:
client_body_buffer_size 8k|16k;
Context:httpserverlocation

 4)client_body_temp_path dir-path [level1[level2[level3]]]

     定义http包体存放的临时目录,可以使用多级目录,level用来指定某一级目录的文件名的长度

    client_body_temp_path /var/tmp/client 1 2

      1表示使用1个16进制的字符创建的一级子目录,2表示使用2个16进制字符创建的二级子目录,

      这个配置就是表示在/var/tmp/client创建16个一级子目录,在每个一级子目录创建256个二级子目录来存放缓存

 5)client_header_buffer_size size

      正常情况下接受用户请求的http报文header部分时分配的buffer大小,默认为1k

Syntax:client_header_buffer_size size;
Default:
client_header_buffer_size 1k;
Context:httpserver

 6)large_client_header_buffers number size

     存储超大http请求首部的内存buffer个数及大小

Syntax:large_client_header_buffers number size;
Default:
large_client_header_buffers 4 8k;
Context:httpserver

 7)connection_pool_size size

     nginx对于每个建立成功的tcp连接都会预先分配一个内存池,此处机用于设定此内存池的初始大小,默认为256|512

 8)request_pool_size size

     nginx对于每个处理每个请求时会预先分配一个内存池,此处机用于设定此内存池的初始大小,默认为4K


五、http核心模块的内置变量

$uri:当前请求的uri,不带参数

$request_uri:请求的uri,带完整参数

$request:请求报文中的请求行

$hosthttp请求报文中host首部,如果请求报文中没有host首部则以处理此请求的主机的主机名代替

$hostname:nginx服务运行所在主机的主机名

$remote_addr:客户端IP

$remote_port: 客户端port

$remote_user:使用用户认证时客户端用户输入的用户名

$request_filename用户请求报文中的URI经过本地root或alias转换后映射的本地的文件路径

$request_method:请求方法

$server_addr:服务器地址

$server_name: 服务器名称

$server_port:服务器端口

$server_protocol:服务器向客户端发送响应时的协议版本,如http/1.1,http/1.0

$scheme:在请求报文中使用的scheme,映射协议本身的协议如http或https

$status:响应状态码

$http_HEADER匹配请求报文中指定的HEADER,$http_host匹配请求报文中的host首部,

         就是通用的咯,例如:$http_content_type匹配请求报文中的content-type首部

$sent_http_HEADER匹配响应报文中指定的HERDER,       

$document_root:当前请求映射到的root配置