Nginx技术研究系列6-配置详解

前两篇文章介绍了Nginx反向代理和动态路由:

Ngnix技术研究系列1-通过应用场景看Nginx的反向代理

Ngnix技术研究系列2-基于Redis实现动态路由

随着研究的深入,很重要的一点就是了解Nginx各个配置和作用。整理一下分享给大家。

一、先说Nginx配置文件

Nginx的配置文件是一个普通的纯文本文件,使用了Nginx自定义的一套配置语法,更接近于脚本语言,混合了Shell、Perl和C的部分特性:

  • 使用#开始一个注释行
  • 配置指令以分号结束,可以接受多个参数,用空白字符分隔
  • 可以使用单引号或者双引号来定义字符串,允许用“”转义字符
  • 配置指令和参数也可以使用引号来指定,特别是当它含有空格的时候
  • 配置块(block)的特殊的配置指令,它有一个{…}参数且无须分号结束,{…}里面可以包含多个配置指令,相当于C语言里的复合语句
  • 有的配置指令只能出现在指定的配置块中(即语境Context)
  • 配置块里可以再包含配置块,嵌套层次没有限制,但需符合配置块的语义
  • 可以使用include指令包含其他配置文件,支持“*”通配符,类似C语言
  • 使用$var可以引用预定义的一些变量,增加配置的灵活性
  • 不能识别或错误的配置指令会导致Nginx解析失败,无法启动

Ngnix配置文件的默认位置在:

/usr/local/openresty/nginx/conf/nginx.conf

可以复制新建一个配置文件。

 二、进程配置

进程配置指令不属于任何配置块,只能在全局域(main)配置
worker_processes number | auto;
设置Nginx能够启动的worker进程的数量,它直接影响Nginx的性能。通常当worker的数量与服务器的CPU核心数相等时,可以获取最佳的性能,这时每一个worker都会工作在一个独立的CPU核心上,完全消除CPU调度的成本。(需配合worker_cpu_affinity指令)
Worker_processes的默认值是1. 如果不清楚服务器CPU核心数量,那么可以设置为auto参数,Nginx会尝试探测数量并设置。cat /proc/cpuinfo | grep processor
master_process on | off;
决定是否启用Nginx的进程池机制,默认值是on,如果设置为off,那么Nginx不会建立master进程,只会用一个worker进程处理请求,worker_processes指令也会失效,并发处理能力大大下降。
worker_cpu_affinity auto [cpumask];
指定worker进程运行在某个CPU核心上,即CPU绑定,对于多核心的CPU来说可以减少CPU切换,提高Cache命中率,让Nginx更充分地利用CPU资源
Worker_processes 4;
Worker_cpu_affinity 0001 0010 0100 1000;
1.9.10之前,只能使用掩码的方式手工绑定,现在则可以用auto参数让Nginx自动绑定CPU。

worker_directory path;
配置Nginx的工作目录,实际上仅用来存放coredump文件,在Nginx发生意外崩溃时可以用gdb调试查找原因。

三、运行日志配置

在Nginx中运行日志分为两种,记录TCP/HTTP访问请求的access_log和记录服务器错误信息的error_log
error_log file|stderr level ;
指定Nginx的运行错误日志,默认是安装目录下的logs/error.log 支持设置其他路径,或者使用标准错误输出stderr。第二个参数level是日志允许输出级别,取值是debug|info|notice|warn|error|crit|alert|emerg, 只有高于这个级别的日志才会记录下来,默认值是error

四、Events配置

Nginx采用事件驱动,利用操作系统内核提供的epoll、kqueue等系统调用来高效地处理网络连接,events配置块就是用来配置Nginx的事件机制。Events配置指令不多,默认配置就饿可以工作的很好
worker_connections number;
设置每个worker进程可以处理的最大连接数量,它决定了Nginx的并发能力。这个指令决定了单个进程的处理能力。Nginx的整体最大可处理的连接数再乘上worker_processes的数量。
worker_connections的默认值是1024,可根据实际情况适当增大。

五、Http配置

Nginx使用http块配置HTTP相关的所有功能,包括cache、fastcgi、gzip、server、location、proxy、upsteam等。

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

resolver address … [valid=time] [ipv6=on|off]

配置域名解析服务器,否则Nginx将无法正确解析域名的地址,无法访问后端的Web服务

keepalive_timeout timeout;

设置keepalive的超时时间,默认75s,主要用于客户端复用Http长连接,提高服务器的性能,如果希望服务器发送数据后能主动断开连接,可以设置为0

access_log path[format [buffer=size][flush=time][if=condition]];

access_log指令用于配置http的访问日志,日志的格式由log_format决定,为了优化磁盘读写,可以设置buffer和flush选项,指定写磁盘的缓冲区大小和刷新时间。 access_log /var/logs/nginx/access.log buffer=8k flush=1s;

 六、Proxy配置

proxy_connect_timeout time;
与Nginx服务器建立连接的超时时间,一般不超过75s,示例:proxy_connect_timeout 60s;

proxy_cookie_domain off;
proxy_cookie_domain domain replacement;
HttpHeader中,重写Set-Cookie中domain的配置,例如: proxy_cookie_domain localhost example.org;

proxy_read_timeout time;
该指令设置与代理服务器的读超时时间。它决定了nginx会等待多长时间来获得请求的响应。这个时间不是获得整个response的时间,而是两次reading操作的时间。
这个超时时间很重要,取决与调用方的超时配置,Nginx要小于等于调用方的超时配置
有个有价值的参考连接:http://yunjianfei.iteye.com/blog/2265918
当Nginx遇到报表查询、导出功能?
http://blog.chinaunix.net/uid-182114-id-4700107.html

proxy_send_timeout time
该指令设置了发送请求给upstream服务器的超时时间。超时设置不是为了整个发送期间,而是在两次write操作期间。如果超时后,upstream没有收到新的数据,nginx会关闭连接。默认60s。

周国庆

2017/10/25

原文地址:https://www.cnblogs.com/tianqing/p/7732640.html