首页 > 代码库 > nginx(2)配置

nginx(2)配置

配置:

       配置的组成部分:

              主配置文件:nginx.conf

                     include conf.d/*.conf

              fastcgi、uwsgi、SCGI等相关的配置文件

              mime.types:支持的mime类型

                           

       主配置文件的配置指令:

              directive value1 [value2 ...];

                           

              注意:

                     (1) 指令必须以分号结尾;

                     (2) 支持使用配置变量 :

                            由模块引入:内建变量;

                            由用户引用:自定义变量;

                                   set variable_name value;

                                   引用变量:$variable_name

                                                

       主配置文件结构:

              main block:主配置段;全局配置段;

              event {

                     ...

              } 事件驱动相关的配置;

              http {

                     ...

              } http/https等 相关的配置段;

              mail {

                     ...

              }

             

              http配置的结构:

                     http {

                            ...

                            ...

                            server {

                                   ...

                                   server_name

                                   root

                                   alias

                                   location {

                                          ...

                                   }

                                   ...

                            }

                            server {

                                   ...

                            }

                     }

                                  

*****************************************************

       main block常见的配置指令:

              分类:

                     正常运行必备的配置;

                     优化性能相关的配置;

                     用于调试及定位问题相关的配置;

                                  

              正常运行必备的配置:

                     1、user USERNAME [GROUPNAME];

                            指定用于运行worker进程的用户和组;不指定则使用nobody

                                         

                     2、pid  /PATH/TO/PID_FILE;

                            指定nginx进程的pid文件路径;

                                         

                     3、worker_rlimit_nofile_number #;

                            单个worker进程所能够打开 的最大文件数量;

                                         

              性能优化相关的配置:

                     1、worker_processes number|auto;

                            worker进程的数量;通常应该为cpu核心数量;

                                         

                     2、worker_cpu_affinity cpumask ...;

                            worker_cpu_affinity auto [cpumask];  cpu亲和力

                           

                            CPUMASK:

                                   0000 0001

                                   0000 0010

                    

                     3、worker_priority nice;

                            [-20,19]

                                         

              调试、定位问题:

                     1、daemon on|off;

                            是否以守护进程方式运行nginx进程;

                     2、mastet_process on|off;

                            是否以master/worker模型启动nignx进程;                                         

                     3、error_log file [level];

                            错误日志文件位置及其级别;

                     4、thread_pool name threads=number [max_queue=number];   默认只有32

                            线程池中的线程数量,及后援队列的长度;

*********************************************************

Events配置段:

Events {

1、worker_connections number;

                            每个worker进程所能够并发打开的最大连接数;      

                            worker_processes * worker_connections      

                     2、use method;

                            指明并发连接请求处理时使用的方法;

                           

                            use  epoll;

                           

                     3、accept_mutex on | off;

                            启用时,表示用于让多个worker轮流地、序列化地响应新请求;

4、keepalive_timeout 60

#keepalive超时时间。 这里指的是http层面的keep-alive 并非tcp的keepalive  如果想了解详情 请戳这里 http://www.bubuko.com/infodetail-260176.html

5、open_file_cache max=2000 inactive=60s;

为打开文件指定缓存,默认是没有启用的,max指定缓存最大数量,建议和打开文件数一致,inactive是指经过多长时间文件没被请求后删除缓存 打开文件最大数量为我们再main配置的worker_rlimit_nofile参数

 

}

******************************************

http配置段

       http {

                     ...

              }             

              定义套接字相关功能             

                     1、server { ... }

                            配置一个虚拟主机;                           

                                   server {

                                          listen PORT;

                                          server_name  HOSTNAME;

                                          root /PATH/TO/DOCUMENTROOT;

                                          ...

                                   }

                           

                            注意:

                                   (1) 基于port的虚拟主机:

                                          listen指令要使用不同的端口;

                                   (2) 基于Hostname的虚拟主机;

                                          server_name指令指向不同的主机名;

                                   (3) 基于ip的虚拟主机:

                                          listen IP:PORT;

                           

                     2、listen address[:port] [default_server] [ssl] [backlog=number] [rcvbuf=size] [sndbuf=size];

                           listen port [default_server] [ssl];

                           listen unix:path [default_server] [ssl] ;            

                            default_server:默认虚拟主机;

                            ssl:限制只能通过ssl连接提供服务;

                            backlog:后缓队列的长度;

                            rcvbuf:接收缓冲大小;

                            sndbuf:发送缓冲区大小;      

                    

                     3、server_name name ...;

                            指明当前server的主机名;后可跟一个或空白字符分隔的多个主机;

                            支持使用*任意长度的任意字符;

                            支持~起始的正则表达式模式字符串;

                           

                            应用策略:

                                   (1) 首先精确匹配;

                                   (2) 左则*通配符匹配;

                                   (3) 右侧*通配符匹配;

                                   (4) 正则表达式模式匹配;

                                                                                                               

                                   server_name  www.magedu.com;                                  

                                   server_name *.magedu.com;                                  

                                   server_name  www.magedu.*;                                  

                                   server_name ~^.*\.magedu\..*$;                                  

                                   mail.magedu.com, www.magedu.com                                  

                     4、tcp_nodelay  on|off;

                            对keepalived模式下的连接是否启用TCP_NODELAY选项;                           

                     5、sendfile on | off;

                            是否启用sendfile功能;                           

             

              定义路径相关配置

                     6、root path;

                            设置web资源路径映射;用于指明用户请求的url所对应的本地文件系统上的文档所在目录路径;

                            可用上下文:http, server, location, if                           

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

                           location @name { ... }

                          

                           根据用户请求的URI来匹配定义的location,匹配到时,此请求将被相应的location块中的指令所处理;

                          

                            server {

                                   ...

                                   location {

                                         

                                   }

                                   location {

                                          ...

                                   }

                            }

                           

                            =:URI精确匹配;

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

                            ~*:做正则表达式模式匹配,不区分字符大小写;

                            ^~:对URI的左半部分做匹配检查,不区分字符大小写;

                           

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

                     8、alias path;

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

                            alias  /bbs/  /web/forum/

                            http://www.magedu.com/bbs/a.jpg                                  

                                   location  /bbs/  {

                                          alias  /web/forum/;

                                   }                                  

                                   /web/forum/a.jpg                           

                                   location  /bbs/  {

                                          root  /web/forum/;

                                   }                                  

                                   /web/forum/bbs/a.jpg                           

                           

                            注意:

                                   root指令:给定的路径对应于location中的/uri/左侧的/;

                                   alias指令:给定的路径对应于location中的/uri/右侧的/;                           

                     9、index file ...;

                            可用位置:http, server, location                           

                            默认主面;                           

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

                            根据用户请求的资源的http响应的状态码实现错误页重定向;                           

                            http://www.magedu.com/hello.html --> 因为资源不存在而被改为对

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


nginx(2)配置