nginx 配置及设置介绍

本文参考了相关作者的内容,参考内容见文中:文章来源链接。

1、Nginx默认配置

以下为Nginx默认配置信息,配置所在路径为: /opt/app/nginx/conf/nginx.conf

#执行Nginx worker进程的用户
user  nobody;

#启动进程,通常设置成和cpu的数量相等
worker_processes  1;

#全局错误日志及pid文件
#nginx的error_log类型如下(从左到右:debug最详细 crit最少): 
#[ debug | info | notice | warn | error | crit ] 
#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保存路径
    #access_log  logs/access.log  main;
    
    #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,
    #对于普通应用,必须设为 on,
    #如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,
    #以平衡磁盘与网络I/O处理速度,降低系统的uptime.
    sendfile        on;
    #tcp_nopush     on;

    #连接超时时间
    #keepalive_timeout  0;
    keepalive_timeout  65;
    
    #是否开启gzip压缩
    #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;

        # 将服务器错误页面跳转到静态页面
        #
        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;
    #    }
    #}

}

2、部分配置设置介绍

2.1 worker_processes设置

文章来源
搜索到原作者的话:

As a general rule you need the only worker with large number ofworker_connections, say 10,000 or 20,000.However, if nginx does CPU-intensive work as SSL or gzipping andyou have 2 or more CPU, then you may set worker_processes to be equalto CPU number.Besides, if you serve many static files and the total size of the filesis bigger than memory, then you may increase worker_processes toutilize a full disk bandwidth.

[Igor Sysoev]

一般一个进程足够了,你可以把连接数设得很大。如果有SSL、gzip这些比较消耗CPU的工作,而且是多核CPU的话,可以设为和CPU的数量一样。或 者要处理很多很多的小文件,而且文件总大小比内存大很多的时候,也可以把进程数增加,以充分利用IO带宽(主要似乎是IO操作有block)。
根据我配置实践,服务器是“多个CPU+gzip+网站总文件大小大于内存”的环境,worker_processes设置为CPU个数的两倍比较好。

2.2 events 设置

文章来源

events模块中包含nginx中所有处理连接的设置.
常用配置项如下

events{  
    use epoll;  
    worker_connections 20000;  
    client_header_buffer_size 4k;  
    open_file_cache max=2000 inactive=60s;  
    open_file_cache_valid 60s;  
    open_file_cache_min_uses 1;  
}  

下面为详细说明
use epoll;
使用epoll的I/O 模型(值得注意的是如果你不知道Nginx该使用哪种轮询方法的话,它会选择一个最适合你操作系统的)
补充说明:
与apache相类,nginx针对不同的操作系统,有不同的事件模型
A)标准事件模型
Select、poll属于标准事件模型,如果当前系统不存在更有效的方法,nginx会选择select或poll
B)高效事件模型
Kqueue:使用于FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 和 MacOS X.使用双处理器的MacOS X系统使用kqueue可能会造成内核崩溃。
Epoll:使用于Linux内核2.6版本及以后的系统。 /dev/poll:使用于Solaris 7 11/99+, HP/UX 11.22+ (eventport), IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+。
Eventport:使用于Solaris 10. 为了防止出现内核崩溃的问题, 有必要安装安全补丁
查看linux版本号可以使用 cat /proc/version命令

cat /proc/version  

输出如下

Linux version 2.6.32-504.23.4.el6.x86_64 ([email protected])   
(gcc version 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC) )   
#1 SMP Tue Jun 9 20:57:37 UTC 2015  

worker_connections 2000;
工作进程的最大连接数量 理论上每台nginx服务器的最大连接数为worker_processes*worker_connections
worker_processes为我们再main中开启的进程数

keepalive_timeout 60;
keepalive超时时间。 这里指的是http层面的keep-alive 并非tcp的keepalive 如果想了解详情 请戳这里里面写的很详细 有兴趣的可以去看一下

client_header_buffer_size 4k;
客户端请求头部的缓冲区大小,这个可以根据你的系统分页大小来设置,一般一个请求头的大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为系统分页大小。查看系统分页可以使用 getconf PAGESIZE命令

getconf PAGESIZE  

输入如下:

[xxxx nginx]# getconf PAGESIZE  
4096  

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

ulimit -a  #open files 为最大文件打开数

open_file_cache_valid 60s;
这个是指多长时间检查一次缓存的有效信息。如果有一个文件在inactive时间内一次没被使用,它将被移除

open_file_cache_min_uses 1;
open_file_cache指令中的inactive参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如果有一个文件在inactive时间内一次没被使用,它将被移除。

你可能感兴趣的:(nginx 配置及设置介绍)