nginx / Web Service

15.3 nginx主配置文件(ngx_http_core_module:main配置段)

 

nginx主配置文件默认配置;

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
#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;
}


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


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

主配置文件的配置指令格式:directive value [value2 …];

1>指令必须以分号结尾;
2>支持使用配置变量;

内建变量:由nginx模块引入,可直接使用;
自定义变量:由用户使用’set’命令定义,格式为”set variable_name value”,需引用变量时,使用”$variable_name”;

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

nginx主配置文件配置指令;

配置内容分类:
正常运行必备的配置;
优化性能相关的配置;
用于调试及定位问题相关的配置;
事件驱动相关的配置;

一、main配置段常用配置指令及参数说明:
====================================

user:<正常运行必备的配置1>

  Syntax:	user user [group];
  Default: 	user nobody nobody;
  Context: 	main
指明运行nginx子进程的用户及组,组名省略则以用户所在的基本组为基准;

worker_processes:<性能优化1>

worker_processes:<性能优化1>
  Syntax:	worker_processes number | auto;
  Default: 	worker_processes 1;	
  Context: 	main
  
定义worker进程的数量;
最优值取决于许多因素,包括(但不限于)CPU核心数量、存储数据的硬盘驱动器数量和负载模式;
可以设置其值为CPU的核心数量,也可以设置为auto,auto参数是从nginx的1.2.5版本开始支持;

worker_cpu_affinity:<性能优化2>

  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;
将每个worker进程绑定到一个单独的CPU核心上;

worker_processes  2;
worker_cpu_affinity  0101  1010;
将第一个worker进程绑定到第0、2号CPU核心上,将第二个worker进程绑定到第1、3号CPU核心上;

worker_priority:<性能优化3>

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

定义worker进程的调度优先级(nice值),取值范围”-20到19″,数值越小,优先级越高;

worker_rlimit_nofile:<性能优化4>

  Syntax:	worker_rlimit_nofile number;
  Default:	 —
  Context: 	main

worker进程所能打开的文件数量上限;

pid:<正常运行必备的配置2>

  Syntax:	pid file;
  Default: 	pid logs/nginx.pid;
  Context: 	main
指定PID文件路径;

error_log:<用于调试及定位问题相关的配置1>

Syntax:	 	error_log file [level];
Default: 	error_log logs/error.log error;
Context: 	main, http, mail, stream, server, location

load_module:<正常运行必备的配置3>

  Syntax:	load_module file;
  Default: 	—
  Context: 	main

This directive appeared in version 1.9.11.
Loads a dynamic module.

Example:
load_module modules/ngx_mail_module.so;

include:<正常运行必备的配置4>

  Syntax:	include file | mask;
  Default: 	—
  Context: 	any

Usage example:
include mime.types;
include vhosts/*.conf;

daemon:<用于调试及定位问题相关的配置2>

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

保持默认开启;

master_process:<用于调试及定位问题相关的配置3>

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

保持默认开启;

events:<事件驱动相关的配置1>

  Syntax:	events { ... }
  Default: 	—
  Context: 	main

events常用配置指令及参数:worker_connections、use、accept_mutex;

worker_connections:<事件驱动相关的配置2>

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

每个worker进程所能打开的最大并发连接数数量;
这个数字包括所有连接(例如与代理服务器的连接等),而不仅仅是与客户端的连接;
实际同时连接的数量不能超过打开文件的最大数量的当前限制,即不能超过由"worker_rlimit_nofile"设定的数值;

use  method:<事件驱动相关的配置3>

  Syntax:	use method;
  Default:	—
  Context:	events

指明并发连接请求的处理方法;一般不需要特别指定,nginx会以最高效的方法处理并发连接请求;

accept_mutex:<事件驱动相关的配置4>

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

处理新的连接请求的方法,'On'表示由各个worker进程轮流处理新请求;
'Off'表示各个新请求的到达都会通知所有的worker进程,如果新连接的容量很低,那么一些worker进程可能只会浪费系统资源;

 

Leave a Reply

Your email address will not be published. Required fields are marked *