nginx(三、配置文件概要解释)

上一篇的安装和配置静态资源服务,看了官方文档后很快搞出来了,但真正要做到灵活应用,必须得对配置文件有个整体的了解,所以这里不能再继续按照单一功能模块来进行了,插播一篇整体的描述介绍。

/etc/nginx/nginx.conf


user  nginx;
worker_processes  1;

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

这个是主要配置文件,里面包含了conf.d下的子配置文件,个人认为设计者的主要目的是,conf.d文件夹下主要来配置server模块,而其他杂项则在nginx.conf里配置。

关键配置解释

user nginx;
这是让nginx以什么用户来用运行,一般情况下是nginx,但如果你的访问目录需要root权限,最好改为user root;

worker_processes 1;
默认是1,如果服务器单运行nginx的话,设置成cpu核数

error_log /var/log/nginx/error.log warn;
记录错误日志,详情参考官方文档,只挑选几个以后可能用到的记录一下:
1、日志级别从小到大排列依次是debug, info, notice, warn, error, crit, alert, emerg,默认是error,如果是crit,则记录的日志包括crit, alert, emerg
2、可以仅为选定的客户端地址启用调试日志

 error_log /path/to/log;

 events {
     debug_connection 192.168.1.1;
     debug_connection 192.168.10.0/24;
 }

3、调试日志循环写入内存缓冲区的大小error_log memory:32m debug;

pid /var/run/nginx.pid;
进程id,这个没什么好深挖的

events{}配置块
这个配置块里一般配置每个worker(也就是每核CPU)可以打开的最大并发连接数worker_connections 1024,原文档里有这么一段描述:

It should be kept in mind that this number includes all connections (e.g. connections with proxied servers, among others), not only connections with clients. Another consideration is that the actual number of simultaneous connections cannot exceed the current limit on the maximum number of open files, which can be changed by worker_rlimit_nofile.

翻译一下大概就是,这个默认的1024连接数,不仅仅是与客户端的连接,而是所有连接,还包括代理服务器的连接等,并且这个值还受worker_rlimit_nofile配置项的限制,这个配置项的作用域不在events模块,而在main模块,所以要放在外面。这个值一般填写linux的最大文件打开数即ulimit -HSn指令显示的数量,一般情况下用压测工具来测试一下服务器的连接并发极限,极限值的7折一般就是实际最大承压数,用实际承压数/CPU核数就是单个worker的最大连接数,比如实际承压数是1048576,16核CPU,那么worker_connections的值就是1048576/16=65536,配置如下:

worker_rlimit_nofile 1048576;

events {
    worker_connections  65536;
    multi_accept on;
    use epoll;
}

http {}配置块
这个模块可配置的内容较少,直接把注释写在配置块里了

http {
    #设定mime类型
    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,调用 sendfile 函数0拷贝输出文件
    #如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.
    sendfile        on;
    #tcp_nopush     on;

    #连接超时时间
    keepalive_timeout  65;

    #gzip  on;
    
    #包含其他配置文件目录
    include /etc/nginx/conf.d/*.conf;
}

以上就是默认文档的初步解释

你可能感兴趣的:(nginx(三、配置文件概要解释))