> 文章列表 > Nginx.conf 配置详解

Nginx.conf 配置详解

Nginx.conf 配置详解

#安全问题,建议用nobody,不要用root.
#user  nobody;
 
#worker数和服务器的cpu数相等是最为适宜
worker_processes  2;
 
#work绑定cpu(4 work绑定4cpu)
worker_cpu_affinity 0001 0010 0100 1000
  
#error_log path(存放路径) level(日志等级) path表示日志路径,level表示日志等级,
#具体如下:[ debug | info | notice | warn | error | crit ]
#从左至右,日志详细程度逐级递减,即debug最详细,crit最少,默认为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;  
 
    #这个值是表示nginx要支持哪种多路io复用。
    #一般的Linux选择epoll, 如果是(*BSD)系列的Linux使用kquene。
    #windows版本的nginx不支持多路IO复用,这个值不用配。
    use epoll;
 
    # 当一个worker抢占到一个链接时,是否尽可能的让其获得更多的连接,默认是off 。
    multi_accept on; //并发量大时缓解客户端等待时间。
    # 默认是on ,开启nginx的抢占锁机制。
    accept_mutex  on; //master指派worker抢占锁
}
http {
    #当web服务器收到静态的资源文件请求时,依据请求文件的后缀名在服务器的MIME配置文件中找到对应的MIME Type,再根据MIME Type设置HTTP Response的Content-Type,然后浏览器根据Content-Type的值处理文件。
    include       mime.types;  #/usr/local/nginx/conf/mime.types
 
    #如果 不能从mime.types找到映射的话,用以下作为默认值-二进制
    default_type  application/octet-stream;
 
     #日志位置
     access_log  logs/host.access.log  main;
 
     #一条典型的accesslog:
     #101.226.166.254 - - [21/Oct/2013:20:34:28 +0800] "GET /movie_cat.php?year=2013 HTTP/1.1" 200 5209 "http://www.baidu.com" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; MDDR; .NET4.0C; .NET4.0E; .NET CLR 1.1.4322; Tablet PC 2.0); 360Spider"
     #1)101.226.166.254:(用户IP)
     #2)[21/Oct/2013:20:34:28 +0800]:(访问时间) 
     #3)GET:http请求方式,有GET和POST两种
     #4)/movie_cat.php?year=2013:当前访问的网页是动态网页,movie_cat.php即请求的后台接口,year=2013为具体接口的参数
     #5)200:服务状态,200表示正常,常见的还有,301永久重定向、4XX表示请求出错、5XX服务器内部错误
     #6)5209:传送字节数为5209,单位为byte
     #7)"http://www.baidu.com":refer:即当前页面的上一个网页
     #8)"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; #.NET CLR 3.0.30729; Media Center PC 6.0; MDDR; .NET4.0C; .NET4.0E; .NET CLR 1.1.4322; Tablet PC 2.0); 360Spider": agent字段:通常用来记录操作系统、浏览器版本、浏览器内核等信息
 
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                       '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
 
    #开启从磁盘直接到网络的文件传输,适用于有大文件上传下载的情况,提高IO效率。
    sendfile        on; //大文件传递优化,提高效率
   
    #一个请求完成之后还要保持连接多久,0表示完成请求后直接关闭连接。默认:75s
    keepalive_timeout  60s; 
 
    #开启或者关闭gzip模块, 文件压缩,再传输,提高效率
    #gzip  on ; 
 
    #设置允许压缩的页面最小字节数,页面字节数从header头中的Content-Length中进行获取。
    #gzip_min_lenth 1k;
 
    # gzip压缩比,1 压缩比最小处理速度最快,9 压缩比最大但处理最慢(传输快但比较消耗cpu)
    #gzip_comp_level 4;
 
    #匹配MIME类型进行压缩,(无论是否指定)"text/html"类型总是会被压缩的。
    #gzip_types types text/plain text/css application/json  application/x-javascript text/xml   
 
    #动静分离
    #服务器端静态资源缓存,最大缓存到内存中的文件,不活跃期限
    open_file_cache max=655350 inactive=20s;   
   
    #活跃期限内最少使用的次数,否则视为不活跃。
    open_file_cache_min_uses 2;
 
    #验证缓存是否活跃的时间间隔 
    open_file_cache_valid 30s;
    
    upstream  myserver{
        # ip_hash;
        server 192.168.161.132:8080 weight=1;
        server 192.168.161.132:8081 weight=1 backup;
        #hash $request_uri
        #hash_method crc32
    }
 
    server {
        #监听端口号
        listen       80;
        #服务名
        server_name  192.168.137.129;
        #字符集
        #charset utf-8;
        location / {   #匹配任何查询。
            root   html;#资源查找位置。         /user/local/nginx/html
            #默认访问首页索引文件的名称
            index  index.html index.htm;
            #反向代理路径
            proxy_pass http://myserver;
            #反向代理的超时时间
            proxy_connect_timeout 10;
            proxy_redirect default;
         }
          #普通匹配
        location  /images/ {    
                    root images ;
        }
         #反正则匹配
         location ^~ /images/jpg/ {  # 匹配任何以 /images/jpg/ 开头的任何查询并且停止搜索。任何正则表达式将不会被测试。 
                      root images/jpg/ ;
         }
         #正则匹配
         location ~*.(gif|jpg|jpeg)$ {       
                      #所有静态文件直接读取硬盘
                      root pic ;
                      #expires定义用户浏览器缓存的时间为3天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力
                      expires 3d; 
         }
        #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;
        } 
    }
}