Nginx详解-服务器集群

<div id="article_details" class="details">
    <div class="article_title">   
         <span class="ico ico_type_Original"></span>


    <h1>
        <span class="link_title"><a href="/zouyujie1127/article/details/41605063">
        Nginx详解-服务器集群            
        </a></span>
    </h1>
</div>

   

        <div class="article_manage clearfix">
        <div class="article_r">
            <span class="link_postdate">2014-11-29 23:09</span>
            <span class="link_view" title="阅读次数">17975人阅读</span>
            <span class="link_comments" title="评论次数"> <a href="#comments" οnclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_pinglun'])">评论</a>(0)</span>
            <span class="link_collect tracking-ad" data-mod="popu_171"> <a href="javascript:void(0);" οnclick="javascript:collectArticle('Nginx详解-服务器集群','41605063');return false;" title="收藏" target="_blank">收藏</a></span>
             <span class="link_report"> <a href="#report" οnclick="javascript:report(41605063,2);return false;" title="举报">举报</a></span>

        </div>
    </div>
    <div class="embody" style="display:none" id="embody">
        <span class="embody_t">本文章已收录于:</span>
        <div class="embody_c" id="lib" value="{"err":0,"msg":"ok","data":[]}"></div>
    </div>
    <style type="text/css">        
            .embody{
                padding:10px 10px 10px;
                margin:0 -20px;
                border-bottom:solid 1px #ededed;                
            }
            .embody_b{
                margin:0 ;
                padding:10px 0;
            }
            .embody .embody_t,.embody .embody_c{
                display: inline-block;
                margin-right:10px;
            }
            .embody_t{
                font-size: 12px;
                color:#999;
            }
            .embody_c{
                font-size: 12px;
            }
            .embody_c img,.embody_c em{
                display: inline-block;
                vertical-align: middle;               
            }
             .embody_c img{               
                width:30px;
                height:30px;
            }
            .embody_c em{
                margin: 0 20px 0 10px;
                color:#333;
                font-style: normal;
            }
    </style>
    <script type="text/javascript">
        $(function () {
            try
            {
                var lib = eval("("+$("#lib").attr("value")+")");
                var html = "";
                if (lib.err == 0) {
                    $.each(lib.data, function (i) {
                        var obj = lib.data[i];
                        //html += '<img src="' + obj.logo + '"/>' + obj.name + "  ";
                        html += ' <a href="' + obj.url + '" target="_blank">';
                        html += ' <img src="' + obj.logo + '">';
                        html += ' <em><b>' + obj.name + '</b></em>';
                        html += ' </a>';
                    });
                    if (html != "") {
                        setTimeout(function () {
                            $("#lib").html(html);                      
                            $("#embody").show();
                        }, 100);
                    }
                }      
            } catch (err)
            { }
            
        });
    </script>
      <div class="category clearfix">
        <div class="category_l">
           <img src="http://static.blog.csdn.net/images/category_icon.jpg">
            <span>分类:</span>
        </div>
        <div class="category_r">
                    <label οnclick="GetCategoryArticles('702315','zouyujie1127','top','41605063');">
                        <span οnclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_fenlei']);">ASP.NET WebForm开发<em>(77)</em></span>
                      <img class="arrow-down" src="http://static.blog.csdn.net/images/arrow_triangle _down.jpg" style="display:inline;">
                      <img class="arrow-up" src="http://static.blog.csdn.net/images/arrow_triangle_up.jpg" style="display:none;">
                        <div class="subItem">
                            <div class="subItem_t"><a href="http://blog.csdn.net/zouyujie1127/article/category/702315" target="_blank">作者同类文章</a><i class="J_close">X</i></div>
                            <ul class="subItem_l" id="top_702315">                            
                            </ul>
                        </div>
                    </label>                    
                    <label οnclick="GetCategoryArticles('2731949','zouyujie1127','top','41605063');">
                        <span οnclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_fenlei']);">ASP.NET 分布式开发<em>(5)</em></span>
                      <img class="arrow-down" src="http://static.blog.csdn.net/images/arrow_triangle _down.jpg" style="display: inline;">
                      <img class="arrow-up" src="http://static.blog.csdn.net/images/arrow_triangle_up.jpg" style="display: none;">
                        <div class="subItem" style="display: none;">
                            <div class="subItem_t"><a href="http://blog.csdn.net/zouyujie1127/article/category/2731949" target="_blank">作者同类文章</a><i class="J_close">X</i></div>
                            <ul class="subItem_l" id="top_2731949"><li class="tracking-ad" data-mod="popu_140"><em>•</em><a href="http://blog.csdn.net/zouyujie1127/article/details/43243925" id="top_aritcle_43243925undefined1265746948774904" target="_blank" title="WCF入门教程2——创建第一个WCF程序">WCF入门教程2——创建第一个WCF程序</a></li> <li class="tracking-ad" data-mod="popu_140"><em>•</em><a href="http://blog.csdn.net/zouyujie1127/article/details/43166815" id="top_aritcle_43166815undefined27141943434253335" target="_blank" title="WCF入门教程1——WCF简要介绍">WCF入门教程1——WCF简要介绍</a></li> <li class="tracking-ad" data-mod="popu_140"><em>•</em><a href="http://blog.csdn.net/zouyujie1127/article/details/41412009" id="top_aritcle_41412009undefined5225351336412132" target="_blank" title="文件并发(日志处理)--队列--Redis+Log4Net">文件并发(日志处理)--队列--Redis+Log4Net</a></li> <li class="tracking-ad" data-mod="popu_140"><em>•</em><a href="http://blog.csdn.net/zouyujie1127/article/details/41411195" id="top_aritcle_41411195undefined1458413563668728" target="_blank" title="Redis主从复制">Redis主从复制</a></li> </ul>
                        </div>
                    </label>                    
        </div>
    </div>
    <script type="text/javascript" src="http://static.blog.csdn.net/scripts/category.js"></script>  
        <div class="bog_copyright">         
            <p class="copyright_p">版权声明:本文为博主原创文章,未经博主允许不得转载。博客新家:http://www.cnblogs.com/jiekzou/</p>
        </div>

  

  
  
     

<div style="clear:both"></div><div style="border:solid 1px #ccc; background:#eee; float:left; min-width:200px;padding:4px 10px;"><p style="text-align:right;margin:0;"><span style="float:left;">目录<a href="#" title="系统根据文章中H1到H6标签自动生成文章目录">(?)</a></span><a href="#" οnclick="javascript:return openct(this);" title="展开">[+]</a></p><ol style="display:none;margin-left:14px;padding-left:14px;line-height:160%;"><li><a href="#t0">Nginx是什么
</a></li><li><a href="#t1">Nginx的应用现状</a></li><li><a href="#t2">Nginx的特点</a></li><li><a href="#t3">Nginx的事件处理机制</a></li><li><a href="#t4">Nginx的不为人知的特点</a></li><li><a href="#t5">Nginx的内部进程模型

</a></li><li><a href="#t6">Nginx是如何处理一个请求</a></li><li><a href="#t7">Nginx典型的应用场景

</a></li><li><a href="#t8">Nginx的应用</a></li><ol><li><a href="#t9">Nginx常见配置说明</a></li><li><a href="#t10">模块参数</a></li></ol><li><a href="#t11">案例</a></li><ol><li><a href="#t12">配置静态资源</a></li><li><a href="#t13">扩展</a></li></ol></ol></div><div style="clear:both"></div><div id="article_content" class="article_content">

<h2><a name="t0"></a>Nginx<span style="font-family:黑体">是什么</span><br>
</h2>
<p>代理服务器<span style="font-family:Times New Roman">:</span><span style="font-family:宋体">一般是指局域网内部的机器通过代理服务器发送请求到互联网上的服务器</span><span style="font-family:Times New Roman">,</span><span style="font-family:宋体">代理服务器一般作用在客户端。应用比如:</span><span style="font-family:Times New Roman">GoAgent</span><span style="font-family:宋体">,翻墙神器</span><span style="font-family:Times New Roman">.</span></p>
<p> <img src="https://img-blog.csdn.net/20141129230257018?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvem91eXVqaWUxMTI3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt=""></p>
<p>一个完整的代理请求过程为:客户端首先与代理服务器创建连接,接着根据代理服务器所使用的代理协议,请求对目标服务器创建连接、或者获得目标服务器的指定资源。 <span style="font-family:Times New Roman">Web</span><span style="font-family:宋体">代理(</span><span style="font-family:Times New Roman">proxy</span><span style="font-family:宋体">)服务器是网络的中间实体。 代理位于</span><span style="font-family:Times New Roman">Web</span><span style="font-family:宋体">客户端和</span><span style="font-family:Times New Roman">Web</span><span style="font-family:宋体">服务器之间,扮演“中间人”的角色。</span><span style="font-family:Times New Roman">HTTP</span><span style="font-family:宋体">的代理服务器即是</span><span style="font-family:Times New Roman">Web</span><span style="font-family:宋体">服务器又是</span><span style="font-family:Times New Roman">Web</span><span style="font-family:宋体">客户端。</span></p>
<p>代理服务器是介于客户端和<span style="font-family:Times New Roman">Web</span><span style="font-family:宋体">服务器之间的另一台服务器,有了它之后,浏览器不是直接到</span><span style="font-family:Times New Roman">Web</span><span style="font-family:宋体">服务器去取回网页而是向代理服务器发出请求,信号会先送到代理服务器,由代理服务器来取回浏览器所需要的信息并传送给你的浏览器。<br>
</span></p>
<p> <span style="font-family:宋体">正向代理 是一个位于客户端和原始服务器</span><span style="font-family:Times New Roman">(origin server)</span><span style="font-family:宋体">之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标</span><span style="font-family:Times New Roman">(</span><span style="font-family:宋体">原始服务器</span><span style="font-family:Times New Roman">)</span><span style="font-family:宋体">,然后代理向原始服务器转交请求并将获得的内容返回给客户端。客户端必须要进行一些特别的设置才能使用正向代理。<br>
</span></p>
<p>反向代理服务器:在服务器端接受客户端的请求,然后把请求分发给具体的服务器进行处理,然后再将服务器的响应结果反馈给客户端。<span style="font-family:Times New Roman">Nginx</span><span style="font-family:宋体">就是其中的一种反向代理服务器软件。<br>
</span></p>
<p>Nginx<span style="font-family:宋体">:</span><span style="font-family:Times New Roman">Nginx ("engine x") </span><span style="font-family:宋体">,</span><span style="font-family:Times New Roman">Nginx (</span><span style="font-family:宋体">“</span><span style="font-family:Times New Roman">engine x</span><span style="font-family:宋体">”</span><span style="font-family:Times New Roman">) </span><span style="font-family:宋体">是俄罗斯人</span><span style="font-family:Times New Roman">Igor Sysoev(</span><span style="font-family:宋体">塞索耶夫</span><span style="font-family:Times New Roman">)</span><span style="font-family:宋体">编写的一款高性能的 </span><span style="font-family:Times New Roman">HTTP </span><span style="font-family:宋体">和反向代理服务器。也是一个</span><span style="font-family:Times New Roman">IMAP/POP3/SMTP</span><span style="font-family:宋体">代理服务器;也就是说,</span><span style="font-family:Times New Roman">Nginx</span><span style="font-family:宋体">本身就可以托管网站,进行</span><span style="font-family:Times New Roman">HTTP</span><span style="font-family:宋体">服务处理,也可以作为反向代理服务器使用。<br>
</span></p>
<p><strong>说明:</strong>客户端必须设置正向代理服务器,当然前提是要知道正向代理服务器的<span style="font-family:Times New Roman">IP</span><span style="font-family:宋体">地址,还有代理程序的端口。</span></p>
<p>反向代理正好与正向代理相反,对于客户端而言代理服务器就像是原始服务器,并且客户端不需要进行任何特别的设置。客户端向反向代理的命名空间<span style="font-family:Times New Roman">(name-space)</span><span style="font-family:宋体">中的内容发送普通请求,接着反向代理将判断向何处</span><span style="font-family:Times New Roman">(</span><span style="font-family:宋体">原始服务器</span><span style="font-family:Times New Roman">)</span><span style="font-family:宋体">转交请求,并将获得的内容返回给客户端。</span></p>
<p>  <img src="https://img-blog.csdn.net/20141129230418562?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvem91eXVqaWUxMTI3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt=""></p>
<p>用户<span style="font-family:Times New Roman">A</span><span style="font-family:宋体">始终认为它访问的是原始服务器</span><span style="font-family:Times New Roman">B</span><span style="font-family:宋体">而不是代理服务器</span><span style="font-family:Times New Roman">Z</span><span style="font-family:宋体">,但实用际上反向代理服务器接</span></p>
<p>受用户<span style="font-family:Times New Roman">A</span><span style="font-family:宋体">的应答,</span></p>
<p>从原始资源服务器<span style="font-family:Times New Roman">B</span><span style="font-family:宋体">中取得用户</span><span style="font-family:Times New Roman">A</span><span style="font-family:宋体">的需求资源,然后发送给用户</span><span style="font-family:Times New Roman">A</span><span style="font-family:宋体">。由于防火墙的作用,只允</span></p>
<p>许代理服务器<span style="font-family:Times New Roman">Z</span><span style="font-family:宋体">访问原始资源服务器</span><span style="font-family:Times New Roman">B</span><span style="font-family:宋体">。尽管在这个虚拟的环境下,防火墙和反向代理的共同</span></p>
<p>作用保护了原始资源服务器<span style="font-family:Times New Roman">B</span><span style="font-family:宋体">,但用户</span><span style="font-family:Times New Roman">A</span><span style="font-family:宋体">并不知情。 </span></p>
<h2><a name="t1"></a>Nginx<span style="font-family:黑体">的应用现状</span></h2>
<p>Nginx <span style="font-family:宋体">已经在俄罗斯最大的门户网站── </span><span style="font-family:Times New Roman">Rambler Media</span><span style="font-family:宋体">(</span><span style="font-family:Times New Roman">www.rambler.ru</span><span style="font-family:宋体">)上运行了</span><span style="font-family:Times New Roman">3</span><span style="font-family:宋体">年时间,同时俄罗斯超过</span><span style="font-family:Times New Roman">20%</span><span style="font-family:宋体">的虚拟主机平台采用</span><span style="font-family:Times New Roman">Nginx</span><span style="font-family:宋体">作为反向代理服务器。</span></p>
<p>在国内,已经有 淘宝、新浪博客、新浪播客、网易新闻、六间房、<span style="font-family:Times New Roman">56.com</span><span style="font-family:宋体">、</span><span style="font-family:Times New Roman">Discuz!</span><span style="font-family:宋体">、水木社区、豆瓣、</span><span style="font-family:Times New Roman">YUPOO</span><span style="font-family:宋体">、海内、迅雷在线 等多家网站使用 </span><span style="font-family:Times New Roman">Nginx </span><span style="font-family:宋体">作为</span><span style="font-family:Times New Roman">Web</span><span style="font-family:宋体">服务器或反向代理服务器。</span></p>
<h2><a name="t2"></a>Nginx<span style="font-family:黑体">的特点</span></h2>
<p>跨平台:Nginx <span style="font-family:宋体">可以在大多数 </span><span style="font-family:Times New Roman">Unix like OS</span><span style="font-family:宋体">编译运行,而且也有</span><span style="font-family:Times New Roman">Windows</span><span style="font-family:宋体">的移植版本。</span></p>
<p>配置异常简单:非常容易上手。配置风格跟程序开发一样,神一般的配置</p>
<p>非阻塞、高并发连接:数据复制时,磁盘<span style="font-family:Times New Roman">I/O</span><span style="font-family:宋体">的第一阶段是非阻塞的。官方<a href="http://lib.csdn.net/base/softwaretest" class="replace_word" title="软件测试知识库" target="_blank" style="color:#df3434; font-weight:bold;">测试</a>能够支撑</span><span style="font-family:Times New Roman">5</span><span style="font-family:宋体">万并发连接,在实际生产环境中跑到</span><span style="font-family:Times New Roman">2</span><span style="font-family:宋体">~</span><span style="font-family:Times New Roman">3</span><span style="font-family:宋体">万并发连接数</span><span style="font-family:Times New Roman">.(</span><span style="font-family:宋体">这得益于</span><span style="font-family:Times New Roman">Nginx</span><span style="font-family:宋体">使用了最新的</span><span style="font-family:Times New Roman">epoll</span><span style="font-family:宋体">模型</span><span style="font-family:Times New Roman">)</span></p>
<p>事件驱动:通信机制采用<span style="font-family:Times New Roman">epoll</span><span style="font-family:宋体">模型,支持更大的并发连接。</span></p>
<h2><a name="t3"></a>Nginx<span style="font-family:黑体">的事件处理机制</span></h2>
<p>对于一个基本的<span style="font-family:Times New Roman">web</span><span style="font-family:宋体">服务器来说,事件通常有三种类型,网络事件、信号、定时器。 </span></p>
<p>首先看一个请求的基本过程:建立连接<span style="font-family:Times New Roman">---</span><span style="font-family:宋体">接收数据</span><span style="font-family:Times New Roman">---</span><span style="font-family:宋体">发送数据 。</span></p>
<p>再次看系统底层的操作 :上述过程(建立连接<span style="font-family:Times New Roman">---</span><span style="font-family:宋体">接收数据</span><span style="font-family:Times New Roman">---</span><span style="font-family:宋体">发送数据)在系统底层就是读写事件。</span></p>
<p> 1<span style="font-family:宋体">)如果采用阻塞调用的方式,当读写事件没有准备好时,必然不能够进行读写事件,那么久只好等待,等事件准备好了,才能进行读写事件。那么请求就会被耽搁 。阻塞调用会进入内核等待,</span><span style="font-family:Times New Roman">cpu</span><span style="font-family:宋体">就会让出去给别人用了,对单线程的</span><span style="font-family:Times New Roman">worker</span><span style="font-family:宋体">来说,显然不合适,当网络事件越多时,大家都在等待呢,</span><span style="font-family:Times New Roman">cpu</span><span style="font-family:宋体">空闲下来没人用,</span><span style="font-family:Times New Roman">cpu</span><span style="font-family:宋体">利用率自然上不去了,更别谈高并发了 。</span></p>
<p>  2<span style="font-family:宋体">)既然没有准备好阻塞调用不行,那么采用非阻塞方式。非阻塞就是,事件,马上返回</span><span style="font-family:Times New Roman">EAGAIN</span><span style="font-family:宋体">,告诉你,事件还没准备好呢,你慌什么,过会再来吧。好吧,你过一会,再来检查一下事件,直到事件准备好了为止,在这期间,你就可以先去做其它事情,然后再来看看事件好了没。虽然不阻塞了,但你得不时地过来检查一下事件的状态,你可以做更多的事情了,但带来的开销也是不小的 </span></p>
<p><strong>小结:</strong>非阻塞通过不断检查事件的状态来判断是否进行读写操作,这样带来的开销很大。 </p>
<p>3<span style="font-family:宋体">)因此才有了异步非阻塞的事件处理机制。具体到系统调用就是像</span><span style="font-family:Times New Roman">select/poll/epoll/kqueue</span><span style="font-family:宋体">这样的系统调用。他们提供了一种机制,让你可以同时监控多个事件,调用他们是阻塞的,但可以设置超时时间,在超时时间之内,如果有事件准备好了,就返回。这种机制解决了我们上面两个问题。 </span></p>
<p>以<span style="font-family:Times New Roman">epoll</span><span style="font-family:宋体">为例:当事件没有准备好时,就放入</span><span style="font-family:Times New Roman">epoll(</span><span style="font-family:宋体">队列</span><span style="font-family:Times New Roman">)</span><span style="font-family:宋体">里面。如果有事件准备好了,那么就去处理;如果事件返回的是</span><span style="font-family:Times New Roman">EAGAIN</span><span style="font-family:宋体">,那么继续将其放入</span><span style="font-family:Times New Roman">epoll</span><span style="font-family:宋体">里面。从而,只要有事件准备好了,我们就去处理她,只有当所有时间都没有准备好时,才在</span><span style="font-family:Times New Roman">epoll</span><span style="font-family:宋体">里面等着。这样,我们就可以并发处理大量的并发了,当然,这里的并发请求,是指未处理完的请求,线程只有一个,所以同时能处理的请求当然只有一个了,只是在请求间进行不断地切换而已,切换也是因为异步事件未准备好,而主动让出的。这里的切换是没有任何代价,你可以理解为循环处理多个准备好的事件,事实上就是这样的。 </span></p>
<p> 4<span style="font-family:宋体">)与多线程的比较:</span></p>
<p>     <span style="font-family:宋体">与多线程相比,这种事件处理方式是有很大的优势的,不需要创建线程,每个请求占用的内存也很少,没有上下文切换,事件处理非常的轻量级。并发数再多也不会导致无谓的资源浪费(上下文切换)。</span></p>
<p><strong>小结:</strong>通过异步非阻塞的事件处理机制,<span style="font-family:Times New Roman">Nginx</span><span style="font-family:宋体">实现由进程循环处理多个准备好的事件,从而实现高并发和轻量级。 </span></p>
<p>master/worker<span style="font-family:宋体">结构:一个</span><span style="font-family:Times New Roman">master</span><span style="font-family:宋体">进程,生成一个或多个</span><span style="font-family:Times New Roman">worker</span><span style="font-family:宋体">进程</span></p>
<p>内存消耗小:处理大并发的请求内存消耗非常小。在<span style="font-family:Times New Roman">3</span><span style="font-family:宋体">万并发连接下,开启的</span><span style="font-family:Times New Roman">10</span><span style="font-family:宋体">个</span><span style="font-family:Times New Roman">Nginx </span><span style="font-family:宋体">进程才消耗</span><span style="font-family:Times New Roman">150M</span><span style="font-family:宋体">内存(</span><span style="font-family:Times New Roman">15M*10=150M</span><span style="font-family:宋体">) 成本低廉:</span><span style="font-family:Times New Roman">Nginx</span><span style="font-family:宋体">为开源软件,可以免费使用。而购买</span><span style="font-family:Times New Roman">F5 BIG-IP</span><span style="font-family:宋体">、</span><span style="font-family:Times New Roman">NetScaler</span><span style="font-family:宋体">等硬件负载均衡交换机则需要十多万至几十万人民币</span></p>
<p>内置的健康检查功能:如果 <span style="font-family:Times New Roman">Nginx Proxy </span><span style="font-family:宋体">后端的某台 </span><span style="font-family:Times New Roman">Web </span><span style="font-family:宋体">服务器宕机了,不会影响前端访问。</span></p>
<p>节省带宽:支持 <span style="font-family:Times New Roman">GZIP </span><span style="font-family:宋体">压缩,可以添加浏览器本地缓存的 </span><span style="font-family:Times New Roman">Header </span><span style="font-family:宋体">头。</span></p>
<p>稳定性高:用于反向代理,宕机的概率微乎其微</p>
<h2><a name="t4"></a>Nginx<span style="font-family:黑体">的不为人知的特点</span></h2>
<p>1<span style="font-family:宋体">、</span><span style="font-family:Times New Roman">nginx</span><span style="font-family:宋体">代理和后端</span><span style="font-family:Times New Roman">web</span><span style="font-family:宋体">服务器间无需长连接;</span></p>
<p>2<span style="font-family:宋体">、接收用户请求是异步的,即先将用户请求全部接收下来,再一次性发送后后端</span><span style="font-family:Times New Roman">web</span><span style="font-family:宋体">服务器,极大的减轻后端</span><span style="font-family:Times New Roman">web</span><span style="font-family:宋体">服务器的压力</span></p>
<p>3<span style="font-family:宋体">、发送响应报文时,是边接收来自后端</span><span style="font-family:Times New Roman">web</span><span style="font-family:宋体">服务器的数据,边发送给客户端的</span></p>
<p>4<span style="font-family:宋体">、网络依赖型低。</span><span style="font-family:Times New Roman">NGINX</span><span style="font-family:宋体">对网络的依赖程度非常低,理论上讲,只要能够</span><span style="font-family:Times New Roman">ping</span><span style="font-family:宋体">通就可以实施负载均衡,而且可以有效区分内网和外网流量</span></p>
<p>5<span style="font-family:宋体">、支持服务器检测。</span><span style="font-family:Times New Roman">NGINX</span><span style="font-family:宋体">能够根据应用服务器处理页面返回的状态码、超时信息等检测服务器是否出现故障,并及时返回错误的请求重新提交到其它节点上</span></p>
<h2><a name="t5"></a>Nginx<span style="font-family:黑体">的内部</span><span style="font-family:Arial">(</span><span style="font-family:黑体">进程</span><span style="font-family:Arial">)</span><span style="font-family:黑体">模型<br>
<img src="https://img-blog.csdn.net/20141129230455298?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvem91eXVqaWUxMTI3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt=""><br>
</span></h2>
<p>nginx<span style="font-family:宋体">是以多进程的方式来工作的,当然</span><span style="font-family:Times New Roman">nginx</span><span style="font-family:宋体">也是支持多线程的方式的</span><span style="font-family:Times New Roman">,</span><span style="font-family:宋体">只是我们主流的方式还是多进程的方式,也是</span><span style="font-family:Times New Roman">nginx</span><span style="font-family:宋体">的默认方式。</span><span style="font-family:Times New Roman">nginx</span><span style="font-family:宋体">采用多进程的方式有诸多好处 </span><span style="font-family:Times New Roman">.</span></p>
<p> (1) nginx<span style="font-family:宋体">在启动后,会有一个</span><span style="font-family:Times New Roman">master</span><span style="font-family:宋体">进程和多个</span><span style="font-family:Times New Roman">worker</span><span style="font-family:宋体">进程。</span><span style="font-family:Times New Roman">master</span><span style="font-family:宋体">进程主要用来管理</span><span style="font-family:Times New Roman">worker</span><span style="font-family:宋体">进程,包含:接收来自外界的信号,向各</span><span style="font-family:Times New Roman">worker</span><span style="font-family:宋体">进程发送信号,监控 </span><span style="font-family:Times New Roman">worker</span><span style="font-family:宋体">进程的运行状态</span><span style="font-family:Times New Roman">,</span><span style="font-family:宋体">当</span><span style="font-family:Times New Roman">worker</span><span style="font-family:宋体">进程退出后</span><span style="font-family:Times New Roman">(</span><span style="font-family:宋体">异常情况下</span><span style="font-family:Times New Roman">)</span><span style="font-family:宋体">,会自动重新启动新的</span><span style="font-family:Times New Roman">worker</span><span style="font-family:宋体">进程。而基本的网络事件,则是放在</span><span style="font-family:Times New Roman">worker</span><span style="font-family:宋体">进程中来处理了 。多个</span><span style="font-family:Times New Roman">worker</span><span style="font-family:宋体">进程之间是对等的,他们同等竞争来自客户端的请求,各进程互相之间是独立的 。一个请求,只可能在一个</span><span style="font-family:Times New Roman">worker</span><span style="font-family:宋体">进程中处理,一个</span><span style="font-family:Times New Roman">worker</span><span style="font-family:宋体">进程,不可能处理其它进程的请求。 </span><span style="font-family:Times New Roman">worker</span><span style="font-family:宋体">进程的个数是可以设置的,一般我们会设置与机器</span><span style="font-family:Times New Roman">cpu</span><span style="font-family:宋体">核数一致,这里面的原因与</span><span style="font-family:Times New Roman">nginx</span><span style="font-family:宋体">的进程模型以及事件处理模型是分不开的 。<br>
</span></p>
<p>(2)Master<span style="font-family:宋体">接收到信号以后怎样进行处理(</span><span style="font-family:Times New Roman">./nginx -s reload </span><span style="font-family:宋体">)</span><span style="font-family:Times New Roman">?</span><span style="font-family:宋体">首先</span><span style="font-family:Times New Roman">master</span><span style="font-family:宋体">进程在接到信号后,会先重新加载配置文件,然后再启动新的进程,并向所有老的进程发送信号,告诉他们可以光荣退休了。新的进程在启动后,就开始接收新的请求,而老的进程在收到来自</span><span style="font-family:Times New Roman">master</span><span style="font-family:宋体">的信号后,就不再接收新的请求,并且在当前进程中的所有未处理完的请求处理完成后,再退出 </span><span style="font-family:Times New Roman">.<br>
</span></p>
<p>(3) worker<span style="font-family:宋体">进程又是如何处理请求的呢?我们前面有提到,</span><span style="font-family:Times New Roman">worker</span><span style="font-family:宋体">进程之间是平等的,每个进程,处理请求的机会也是一样的。当我们提供</span><span style="font-family:Times New Roman">80</span><span style="font-family:宋体">端口的</span><span style="font-family:Times New Roman">http</span><span style="font-family:宋体">服务时,一个连接请求过来,每个进程都有可能处理这个连接,怎么做到的呢?首先,每个</span><span style="font-family:Times New Roman">worker</span><span style="font-family:宋体">进程都是从</span><span style="font-family:Times New Roman">master</span><span style="font-family:宋体">进程</span><span style="font-family:Times New Roman">fork</span><span style="font-family:宋体">过来,在</span><span style="font-family:Times New Roman">master</span><span style="font-family:宋体">进程里面,先建立好需要</span><span style="font-family:Times New Roman">listen</span><span style="font-family:宋体">的</span><span style="font-family:Times New Roman">socket</span><span style="font-family:宋体">之后,然后再</span><span style="font-family:Times New Roman">fork</span><span style="font-family:宋体">出多个</span><span style="font-family:Times New Roman">worker</span><span style="font-family:宋体">进程,这样每个</span><span style="font-family:Times New Roman">worker</span><span style="font-family:宋体">进程都可以去</span><span style="font-family:Times New Roman">accept</span><span style="font-family:宋体">这个</span><span style="font-family:Times New Roman">socket(</span><span style="font-family:宋体">当然不是同一个</span><span style="font-family:Times New Roman">socket</span><span style="font-family:宋体">,只是每个进程的这个</span><span style="font-family:Times New Roman">socket</span><span style="font-family:宋体">会监控在同一个</span><span style="font-family:Times New Roman">ip</span><span style="font-family:宋体">地址与端口,这个在网络协议里面是允许的</span><span style="font-family:Times New Roman">)</span><span style="font-family:宋体">。一般来说,当一个连接进来后,所有在</span><span style="font-family:Times New Roman">accept</span><span style="font-family:宋体">在这个</span><span style="font-family:Times New Roman">socket</span><span style="font-family:宋体">上面的进程,都会收到通知,而只有一个进程可以</span><span style="font-family:Times New Roman">accept</span><span style="font-family:宋体">这个连接,其它的则</span><span style="font-family:Times New Roman">accept</span><span style="font-family:宋体">失败,这是所谓的惊群现象。当然,</span><span style="font-family:Times New Roman">nginx</span><span style="font-family:宋体">也不会视而不见,所以</span><span style="font-family:Times New Roman">nginx</span><span style="font-family:宋体">提供了一个</span><span style="font-family:Times New Roman">accept_mutex</span><span style="font-family:宋体">这个东西,从名字上,我们可以看这是一个加在</span><span style="font-family:Times New Roman">accept</span><span style="font-family:宋体">上的一把共享锁。有了这把锁之后,同一时刻,就只会有一个进程在</span><span style="font-family:Times New Roman">accpet</span><span style="font-family:宋体">连接,这样就不会有惊群问题了。</span><span style="font-family:Times New Roman">accept_mutex</span><span style="font-family:宋体">是一个可控选项,我们可以显示地关掉,默认是打开的。当一个</span><span style="font-family:Times New Roman">worker</span><span style="font-family:宋体">进程在</span><span style="font-family:Times New Roman">accept</span><span style="font-family:宋体">这个连接之后,就开始读取请求,解析请求,处理请求,产生数据后,再返回给客户端,最后才断开连接,这样一个完整的请求就是这样的了。我们可以看到,一个请求,完全由</span><span style="font-family:Times New Roman">worker</span><span style="font-family:宋体">进程来处理,而且只在一个</span><span style="font-family:Times New Roman">worker</span><span style="font-family:宋体">进程中处理。<br>
</span></p>
<p>(4):<span style="font-family:宋体">,</span><span style="font-family:Times New Roman">nginx</span><span style="font-family:宋体">采用这种进程模型有什么好处呢?采用独立的进程,可以让互相之间不会影响,一个进程退出后,其它进程还在工作,服务不会中断,</span><span style="font-family:Times New Roman">master</span><span style="font-family:宋体">进程则很快重新启动新的</span><span style="font-family:Times New Roman">worker</span><span style="font-family:宋体">进程。当然,</span><span style="font-family:Times New Roman">worker</span><span style="font-family:宋体">进程的异常退出,肯定是程序有</span><span style="font-family:Times New Roman">bug</span><span style="font-family:宋体">了,异常退出,会导致当前</span><span style="font-family:Times New Roman">worker</span><span style="font-family:宋体">上的所有请求失败,不过不会影响到所有请求,所以降低了风险。当然,好处还有很多,大家可以慢慢体会。<br>
</span></p>
<p>(5).<span style="font-family:宋体">有人可能要问了,</span><span style="font-family:Times New Roman">nginx</span><span style="font-family:宋体">采用多</span><span style="font-family:Times New Roman">worker</span><span style="font-family:宋体">的方式来处理请求,每个</span><span style="font-family:Times New Roman">worker</span><span style="font-family:宋体">里面只有一个主线程,那能够处理的并发数很有限啊,多少个</span><span style="font-family:Times New Roman">worker</span><span style="font-family:宋体">就能处理多少个并发,何来高并发呢?非也,这就是</span><span style="font-family:Times New Roman">nginx</span><span style="font-family:宋体">的高明之处,</span><span style="font-family:Times New Roman">nginx</span><span style="font-family:宋体">采用了异步非阻塞的方式来处理请求,也就是说,</span><span style="font-family:Times New Roman">nginx</span><span style="font-family:宋体">是可以同时处理成千上万个请求的 </span><span style="font-family:Times New Roman">.</span><span style="font-family:宋体">对于</span><span style="font-family:Times New Roman">IIS</span><span style="font-family:宋体">服务器每个请求会独占一个工作线程,当并发数上到几千时,就同时有几千的线程在处理请求了。这对<a href="http://lib.csdn.net/base/operatingsystem" class="replace_word" title="操作系统知识库" target="_blank" style="color:#df3434; font-weight:bold;">操作系统</a>来说,是个不小的挑战,线程带来的内存占用非常大,线程的上下文切换带来的</span><span style="font-family:Times New Roman">cpu</span><span style="font-family:宋体">开销很大,自然性能就上不去了,而这些开销完全是没有意义的。我们之前说过,推荐设置</span><span style="font-family:Times New Roman">worker</span><span style="font-family:宋体">的个数为</span><span style="font-family:Times New Roman">cpu</span><span style="font-family:宋体">的核数,在这里就很容易理解了,更多的</span><span style="font-family:Times New Roman">worker</span><span style="font-family:宋体">数,只会导致进程来竞争</span><span style="font-family:Times New Roman">cpu</span><span style="font-family:宋体">资源了,从而带来不必要的上下文切换。而且,</span><span style="font-family:Times New Roman">nginx</span><span style="font-family:宋体">为了更好的利用多核特性,提供了</span><span style="font-family:Times New Roman">cpu</span><span style="font-family:宋体">亲缘性的绑定选项,我们可以将某一个进程绑定在某一个核上,这样就不会因为进程的切换带来</span><span style="font-family:Times New Roman">cache</span><span style="font-family:宋体">的失效 </span></p>
<h2><a name="t6"></a>Nginx<span style="font-family:黑体">是如何处理一个请求</span></h2>
<p>首先,<span style="font-family:Times New Roman">nginx</span><span style="font-family:宋体">在启动时,会解析配置文件,得到需要监听的端口与</span><span style="font-family:Times New Roman">ip</span><span style="font-family:宋体">地址,然后在</span><span style="font-family:Times New Roman">nginx</span><span style="font-family:宋体">的</span><span style="font-family:Times New Roman">master</span><span style="font-family:宋体">进程里面,先初始化好这个监控的</span><span style="font-family:Times New Roman">socket(</span><span style="font-family:宋体">创建</span><span style="font-family:Times New Roman">socket</span><span style="font-family:宋体">,设置</span><span style="font-family:Times New Roman">addrreuse</span><span style="font-family:宋体">等选项,绑定到指定的</span><span style="font-family:Times New Roman">ip</span><span style="font-family:宋体">地址端口,再</span><span style="font-family:Times New Roman">listen)</span><span style="font-family:宋体">,然后再</span><span style="font-family:Times New Roman">fork(</span><span style="font-family:宋体">一个现有进程可以调用</span><span style="font-family:Times New Roman">fork</span><span style="font-family:宋体">函数创建一个新进程。由</span><span style="font-family:Times New Roman">fork</span><span style="font-family:宋体">创建的新进程被称为子进程 </span><span style="font-family:Times New Roman">)</span><span style="font-family:宋体">出多个子进程出来,然后子进程会竞争</span><span style="font-family:Times New Roman">accept</span><span style="font-family:宋体">新的连接。此时,客户端就可以向</span><span style="font-family:Times New Roman">nginx</span><span style="font-family:宋体">发起连接了。当客户端与</span><span style="font-family:Times New Roman">nginx</span><span style="font-family:宋体">进行三次握手,与</span><span style="font-family:Times New Roman">nginx</span><span style="font-family:宋体">建立好一个连接后,此时,某一个子进程会</span><span style="font-family:Times New Roman">accept</span><span style="font-family:宋体">成功,得到这个建立好的连接的</span><span style="font-family:Times New Roman">socket</span><span style="font-family:宋体">,然后创建</span><span style="font-family:Times New Roman">nginx</span><span style="font-family:宋体">对连接的封装,即</span><span style="font-family:Times New Roman">ngx_connection_t</span><span style="font-family:宋体">结构体。接着,设置读写事件处理函数并添加读写事件来与客户端进行数据的交换。最后,</span><span style="font-family:Times New Roman">nginx</span><span style="font-family:宋体">或客户端来主动关掉连接,到此,一个连接就寿终正寝了。 </span></p>
<p>当然,<span style="font-family:Times New Roman">nginx</span><span style="font-family:宋体">也是可以作为客户端来请求其它</span><span style="font-family:Times New Roman">server</span><span style="font-family:宋体">的数据的(如</span><span style="font-family:Times New Roman">upstream</span><span style="font-family:宋体">模块),此时,与其它</span><span style="font-family:Times New Roman">server</span><span style="font-family:宋体">创建的连接,也封装在</span><span style="font-family:Times New Roman">ngx_connection_t</span><span style="font-family:宋体">中。作为客户端,</span><span style="font-family:Times New Roman">nginx</span><span style="font-family:宋体">先获取一个</span><span style="font-family:Times New Roman">ngx_connection_t</span><span style="font-family:宋体">结构体,然后创建</span><span style="font-family:Times New Roman">socket</span><span style="font-family:宋体">,并设置</span><span style="font-family:Times New Roman">socket</span><span style="font-family:宋体">的属性( 比如非阻塞)。然后再通过添加读写事件,调用</span><span style="font-family:Times New Roman">connect/read/write</span><span style="font-family:宋体">来调用连接,最后关掉连接,并释放</span><span style="font-family:Times New Roman">ngx_connection_t</span><span style="font-family:宋体">。 </span></p>
<p><strong>说明</strong>:nginx<span style="font-family:宋体">在实现时,是通过一个连接池来管理的,每个</span><span style="font-family:Times New Roman">worker</span><span style="font-family:宋体">进程都有一个独立的连接池,连接池的大小是</span><span style="font-family:Times New Roman">worker_connections</span><span style="font-family:宋体">。这里的连接池里面保存的其实不是真实的连接,它只是一个</span><span style="font-family:Times New Roman">worker_connections</span><span style="font-family:宋体">大小的一个</span><span style="font-family:Times New Roman">ngx_connection_t</span><span style="font-family:宋体">结构的数组。并且,</span><span style="font-family:Times New Roman">nginx</span><span style="font-family:宋体">会通过一个链表</span><span style="font-family:Times New Roman">free_connections</span><span style="font-family:宋体">来保存所有的空闲</span><span style="font-family:Times New Roman">ngx_connection_t</span><span style="font-family:宋体">,每次获取一个连接时,就从空闲连接链表中获取一个,用完后,再放回空闲连接链表里面。 </span></p>
<p>在这里,很多人会误解<span style="font-family:Times New Roman">worker_connections</span><span style="font-family:宋体">这个参数的意思,认为这个值就是</span><span style="font-family:Times New Roman">nginx</span><span style="font-family:宋体">所能建立连接的最大值。其实不然,这个值是表示每个</span><span style="font-family:Times New Roman">worker</span><span style="font-family:宋体">进程所能建立连接的最大值,所以,一个</span><span style="font-family:Times New Roman">nginx</span><span style="font-family:宋体">能建立的最大连接数,应该是</span><span style="font-family:Times New Roman">worker_connections * worker_processes</span><span style="font-family:宋体">。当然,这里说的是最大连接数,对于</span><span style="font-family:Times New Roman">HTTP</span><span style="font-family:宋体">请求本地资源来说,能够支持的最大并发数量是</span><span style="font-family:Times New Roman">worker_connections * worker_processes</span><span style="font-family:宋体">,而如果是</span><span style="font-family:Times New Roman">HTTP</span><span style="font-family:宋体">作为反向代理来说,最大并发数量应该是</span><span style="font-family:Times New Roman">worker_connections * worker_processes/2</span><span style="font-family:宋体">。因为作为反向代理服务器,每个并发会建立与客户端的连接和与后端服务的连接,会占用两个连接。 </span></p>
<h2><a name="t7"></a>Nginx<span style="font-family:黑体">典型的应用场景<br>
<img src="https://img-blog.csdn.net/20141129230549383?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvem91eXVqaWUxMTI3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt=""><br>
</span></h2>
<p>负载均衡技术在现有网络结构之上提供了一种廉价、有效、透明的方法,来扩展网络设备和服务器的带宽、增加吞吐量、加强网络数据处理能力、提高网络的灵活性和可用性。它有两方面的含义:首先,大量的并发访问或数据流量分担到多台节点设备上分别处理,减少用户等待响应的时间;其次,单个重负载的运算分担到多台节点设备上做并行处理,每个节点设备处理结束后,将结果汇总,返回给用户,系统处理能力得到大幅度提高 </p>
<h2><a name="t8"></a>Nginx<span style="font-family:黑体">的应用</span></h2>
<p>1<span style="font-family:宋体">、到官网下载</span><span style="font-family:Times New Roman">Windows</span><span style="font-family:宋体">版本,下载地址:</span><span style="font-family:Times New Roman">http://nginx.org/en/download.html</span></p>
<p>2<span style="font-family:宋体">、解压到磁盘任一目录</span></p>
<p>3<span style="font-family:宋体">、修改配置文件:具体参考备注。</span></p>
<p>4<span style="font-family:宋体">、启动服务:</span></p>
<p>直接运行<span style="font-family:Times New Roman">nginx.exe</span><span style="font-family:宋体">,缺点控制台窗口关闭,服务关闭。</span></p>
<p>守护进程的方式启动:<span style="font-family:Times New Roman">start nginx.exe</span></p>
<p>5<span style="font-family:宋体">、停止服务:</span><span style="font-family:Times New Roman">nginx -s stop</span></p>
<p>重新加载配置:<span style="font-family:Times New Roman">nginx -s  reload</span></p>
<h3><a name="t9"></a>Nginx<span style="font-family:宋体">常见配置说明</span></h3>
<p>worker_processes 8;</p>
<p>#nginx<span style="font-family:宋体">进程数,建议设置为等于</span><span style="font-family:Times New Roman">CPU</span><span style="font-family:宋体">总核心数</span></p>
<p>worker_connections 65535;</p>
<p>#<span style="font-family:宋体">单个进程最大连接数(最大连接数</span><span style="font-family:Times New Roman">=</span><span style="font-family:宋体">连接数</span><span style="font-family:Times New Roman">*</span><span style="font-family:宋体">进程数)</span></p>
<p>client_header_buffer_size 32k; #<span style="font-family:宋体">上传文件大小限制</span></p>
<p>large_client_header_buffers 4 64k; #<span style="font-family:宋体">设定请求缓</span></p>
<p>client_max_body_size 8m; #<span style="font-family:宋体">设定请求缓</span></p>
<p>autoindex on; #<span style="font-family:宋体">开启目录列表访问,合适下载服务器,默认关闭。</span></p>
<p>tcp_nopush on; #<span style="font-family:宋体">防止网络阻塞</span></p>
<p>tcp_nodelay on; #<span style="font-family:宋体">防止网络阻塞</span></p>
<p>keepalive_timeout 120; #<span style="font-family:宋体">长连接超时时间,单位是秒</span></p>
<p>gzip on; #<span style="font-family:宋体">开启</span><span style="font-family:Times New Roman">gzip</span><span style="font-family:宋体">压缩输出</span></p>
<p>gzip_min_length 1k; #<span style="font-family:宋体">最小压缩文件大小</span></p>
<p>gzip_buffers 4 16k; #<span style="font-family:宋体">压缩缓冲区</span></p>
<p>gzip_http_version 1.0; #<span style="font-family:宋体">压缩版本(默认</span><span style="font-family:Times New Roman">1.1</span><span style="font-family:宋体">,前端如果是</span><span style="font-family:Times New Roman">squid2.5</span><span style="font-family:宋体">请使用</span><span style="font-family:Times New Roman">1.0</span><span style="font-family:宋体">)</span></p>
<p>gzip_comp_level 2; #<span style="font-family:宋体">压缩等级</span></p>
<p>upstream blog.ha97.com {</p>
<p>#upstream<span style="font-family:宋体">的负载均衡,</span><span style="font-family:Times New Roman">weight</span><span style="font-family:宋体">是权重,可以根据机器配置定义权重。</span><span style="font-family:Times New Roman">weigth</span><span style="font-family:宋体">参数表示权值,权值越高被分配到的几率越大。</span></p>
<p>server 192.168.80.121:80 weight=3;</p>
<p>server 192.168.80.122:80 weight=2;</p>
<p>server 192.168.80.123:80 weight=3;</p>
<p>}</p>
<p>#<span style="font-family:宋体">虚拟主机的配置</span></p>
<p>server</p>
<p>{</p>
<p>#<span style="font-family:宋体">监听端口</span></p>
<p>listen 80;</p>
<p>#<span style="font-family:宋体">域名可以有多个,用空格隔开</span></p>
<p>server_name www.ha97.com ha97.com;</p>
<p>index index.html index.htm index.<a href="http://lib.csdn.net/base/php" class="replace_word" title="PHP知识库" target="_blank" style="color:#df3434; font-weight:bold;">PHP</a>;</p>
<p>root /data/www/ha97;</p>
<p>location ~ .*.(php|php5)?$</p>
<p>{</p>
<p>fastcgi_pass 127.0.0.1:9000;</p>
<p>fastcgi_index index.php;</p>
<p>include fastcgi.conf;</p>
<p>}</p>
<h3><a name="t10"></a>模块参数</h3>
<div class="dp-highlighter bg_html"><div class="bar"><div class="tools"><b>[html]</b> <a href="#" class="ViewSource" title="view plain" οnclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" οnclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 518px; top: 8081px; width: 29px; height: 14px; z-index: 99;"><embed id="ZeroClipboardMovie_1" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="29" height="14" name="ZeroClipboardMovie_1" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=1&width=29&height=14" wmode="transparent"></div></span><span data-mod="popu_169"> <a href="#" class="PrintSource" title="print" οnclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" οnclick="dp.sh.Toolbar.Command('About',this);return false;">?</a><span class="tracking-ad" data-mod="popu_167"><a href="https://code.csdn.net/snippets/536844" target="_blank" title="在CODE上查看代码片" style="text-indent:0;"><img src="https://code.csdn.net/assets/CODE_ico.png" width="12" height="12" alt="在CODE上查看代码片" style="position:relative;top:1px;left:2px;"></a></span><span class="tracking-ad" data-mod="popu_170"><a href="https://code.csdn.net/snippets/536844/fork" target="_blank" title="派生到我的代码片" style="text-indent:0;"><img src="https://code.csdn.net/assets/ico_fork.svg" width="12" height="12" alt="派生到我的代码片" style="position:relative;top:2px;left:2px;"></a></span></div></div><ol start="1" class="dp-xml"><li class="alt"><span><span>#定义Nginx运行的用户和用户组  </span></span></li><li class=""><span>  </span></li><li class="alt"><span>user www www;  </span></li><li class=""><span>  </span></li><li class="alt"><span>  </span></li><li class=""><span>#nginx进程数,建议设置为等于CPU总核心数。  </span></li><li class="alt"><span>  </span></li><li class=""><span>worker_processes 8;  </span></li><li class="alt"><span>  </span></li><li class=""><span>  </span></li><li class="alt"><span>#全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]  </span></li><li class=""><span>  </span></li><li class="alt"><span>error_log ar/loginx/error.log info;  </span></li><li class=""><span>  </span></li><li class="alt"><span>   </span></li><li class=""><span>#进程文件  </span></li><li class="alt"><span>  </span></li><li class=""><span>pid ar/runinx.pid;  </span></li><li class="alt"><span>  </span></li><li class=""><span>   </span></li><li class="alt"><span>#一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(系统的值ulimit -n)与nginx进程数相除,但是nginx分配请求并不均匀,  </span></li><li class=""><span>所以建议与ulimit -n的值保持一致。  </span></li><li class="alt"><span>  </span></li><li class=""><span>worker_rlimit_nofile 65535;  </span></li><li class="alt"><span>  </span></li><li class=""><span>   </span></li><li class="alt"><span>  </span></li><li class=""><span>#工作模式与连接数上限  </span></li><li class="alt"><span>  </span></li><li class=""><span>events  </span></li><li class="alt"><span>  </span></li><li class=""><span>{  </span></li><li class="alt"><span>  </span></li><li class=""><span>#参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,  </span></li><li class="alt"><span>如果跑在FreeBSD上面,就用kqueue模型。  </span></li><li class=""><span>  </span></li><li class="alt"><span>use epoll;  </span></li><li class=""><span>  </span></li><li class="alt"><span>#单个进程最大连接数(最大连接数=连接数*进程数)  </span></li><li class=""><span>  </span></li><li class="alt"><span>worker_connections 65535;  </span></li><li class=""><span>  </span></li><li class="alt"><span>}  </span></li><li class=""><span>   </span></li><li class="alt"><span>  </span></li><li class=""><span>#设定http服务器  </span></li><li class="alt"><span>  </span></li><li class=""><span>http  </span></li><li class="alt"><span>  </span></li><li class=""><span>{  </span></li><li class="alt"><span>  </span></li><li class=""><span>include mime.types; #文件扩展名与文件类型映射表  </span></li><li class="alt"><span>  </span></li><li class=""><span>default_type application/octet-stream; #默认文件类型  </span></li><li class="alt"><span>  </span></li><li class=""><span>#charset utf-8; #默认编码  </span></li><li class="alt"><span>  </span></li><li class=""><span>server_names_hash_bucket_size 128; #服务器名字的hash表大小  </span></li><li class="alt"><span>  </span></li><li class=""><span>client_header_buffer_size 32k; #上传文件大小限制  </span></li><li class="alt"><span>  </span></li><li class=""><span>large_client_header_buffers 4 64k; #设定请求缓  </span></li><li class="alt"><span>  </span></li><li class=""><span>client_max_body_size 8m; #设定请求缓  </span></li><li class="alt"><span>  </span></li><li class=""><span>sendfile on; #开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,  </span></li><li class="alt"><span>可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。  </span></li><li class=""><span>  </span></li><li class="alt"><span>autoindex on; #开启目录列表访问,合适下载服务器,默认关闭。  </span></li><li class=""><span>  </span></li><li class="alt"><span>tcp_nopush on; #防止网络阻塞  </span></li><li class=""><span>  </span></li><li class="alt"><span>tcp_nodelay on; #防止网络阻塞  </span></li><li class=""><span>  </span></li><li class="alt"><span>keepalive_timeout 120; #长连接超时时间,单位是秒  </span></li><li class=""><span>  </span></li><li class="alt"><span>   </span></li><li class=""><span>  </span></li><li class="alt"><span>#FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。  </span></li><li class=""><span>  </span></li><li class="alt"><span>fastcgi_connect_timeout 300;  </span></li><li class=""><span>  </span></li><li class="alt"><span>fastcgi_send_timeout 300;  </span></li><li class=""><span>  </span></li><li class="alt"><span>fastcgi_read_timeout 300;  </span></li><li class=""><span>  </span></li><li class="alt"><span>fastcgi_buffer_size 64k;  </span></li><li class=""><span>  </span></li><li class="alt"><span>fastcgi_buffers 4 64k;  </span></li><li class=""><span>  </span></li><li class="alt"><span>fastcgi_busy_buffers_size 128k;  </span></li><li class=""><span>  </span></li><li class="alt"><span>fastcgi_temp_file_write_size 128k;  </span></li><li class=""><span>   </span></li><li class="alt"><span>  </span></li><li class=""><span>#gzip模块设置  </span></li><li class="alt"><span>  </span></li><li class=""><span>gzip on; #开启gzip压缩输出  </span></li><li class="alt"><span>  </span></li><li class=""><span>gzip_min_length 1k; #最小压缩文件大小  </span></li><li class="alt"><span>  </span></li><li class=""><span>gzip_buffers 4 16k; #压缩缓冲区  </span></li><li class="alt"><span>  </span></li><li class=""><span>gzip_http_version 1.0; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)  </span></li><li class="alt"><span>  </span></li><li class=""><span>gzip_comp_level 2; #压缩等级  </span></li><li class="alt"><span>  </span></li><li class=""><span>gzip_types text/plain application/x-javascript text/css application/xml;  </span></li><li class="alt"><span>  </span></li><li class=""><span>#压缩类型,默认就已经包含textml,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。  </span></li><li class="alt"><span>  </span></li><li class=""><span>gzip_vary on;  </span></li><li class="alt"><span>  </span></li><li class=""><span>#limit_zone crawler $binary_remote_addr 10m; #开启限制IP连接数的时候需要使用  </span></li><li class="alt"><span>  </span></li><li class=""><span>   </span></li><li class="alt"><span>  </span></li><li class=""><span>upstream blog.ha97.com {  </span></li><li class="alt"><span>  </span></li><li class=""><span>#upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。  </span></li><li class="alt"><span>  </span></li><li class=""><span>server 192.168.80.121:80 <span class="attribute">weight</span><span>=</span><span class="attribute-value">3</span><span>;  </span></span></li><li class="alt"><span>  </span></li><li class=""><span>server 192.168.80.122:80 <span class="attribute">weight</span><span>=</span><span class="attribute-value">2</span><span>;  </span></span></li><li class="alt"><span>  </span></li><li class=""><span>server 192.168.80.123:80 <span class="attribute">weight</span><span>=</span><span class="attribute-value">3</span><span>;  </span></span></li><li class="alt"><span>  </span></li><li class=""><span>}  </span></li><li class="alt"><span>  </span></li><li class=""><span>   </span></li><li class="alt"><span>  </span></li><li class=""><span>#虚拟主机的配置  </span></li><li class="alt"><span>  </span></li><li class=""><span>server  </span></li><li class="alt"><span>  </span></li><li class=""><span>{  </span></li><li class="alt"><span>  </span></li><li class=""><span>#监听端口  </span></li><li class="alt"><span>  </span></li><li class=""><span>listen 80;  </span></li><li class="alt"><span>  </span></li><li class=""><span>#域名可以有多个,用空格隔开  </span></li><li class="alt"><span>  </span></li><li class=""><span>server_name www.ha97.com ha97.com;  </span></li><li class="alt"><span>  </span></li><li class=""><span>index index.html index.htm index.php;  </span></li><li class="alt"><span>  </span></li><li class=""><span>root /data/www/ha97;  </span></li><li class="alt"><span>  </span></li><li class=""><span>location ~ .*.(php|php5)?$  </span></li><li class="alt"><span>  </span></li><li class=""><span>{  </span></li><li class="alt"><span>  </span></li><li class=""><span>fastcgi_pass 127.0.0.1:9000;  </span></li><li class="alt"><span>  </span></li><li class=""><span>fastcgi_index index.php;  </span></li><li class="alt"><span>  </span></li><li class=""><span>include fastcgi.conf;  </span></li><li class="alt"><span>  </span></li><li class=""><span>}  </span></li><li class="alt"><span>  </span></li><li class=""><span>#图片缓存时间设置  </span></li><li class="alt"><span>  </span></li><li class=""><span>location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$  </span></li><li class="alt"><span>  </span></li><li class=""><span>{  </span></li><li class="alt"><span>  </span></li><li class=""><span>expires 10d;  </span></li><li class="alt"><span>  </span></li><li class=""><span>}  </span></li><li class="alt"><span>  </span></li><li class=""><span>#JS和CSS缓存时间设置  </span></li><li class="alt"><span>  </span></li><li class=""><span>location ~ .*.(js|css)?$  </span></li><li class="alt"><span>  </span></li><li class=""><span>{  </span></li><li class="alt"><span>  </span></li><li class=""><span>expires 1h;  </span></li><li class="alt"><span>  </span></li><li class=""><span>}  </span></li><li class="alt"><span>  </span></li><li class=""><span>#日志格式设定  </span></li><li class="alt"><span>  </span></li><li class=""><span>log_format access '$remote_addr - $remote_user [$time_local] "$request" '  </span></li><li class="alt"><span>  </span></li><li class=""><span>'$status $body_bytes_sent "$http_referer" '  </span></li><li class="alt"><span>  </span></li><li class=""><span>'"$http_user_agent" $http_x_forwarded_for';  </span></li><li class="alt"><span>  </span></li><li class=""><span>#定义本虚拟主机的访问日志  </span></li><li class="alt"><span>  </span></li><li class=""><span>access_log ar/loginx/ha97access.log access;  </span></li><li class="alt"><span>  </span></li><li class=""><span>   </span></li><li class="alt"><span>  </span></li><li class=""><span>#对 "/" 启用反向代理  </span></li><li class="alt"><span>  </span></li><li class=""><span>location / {  </span></li><li class="alt"><span>  </span></li><li class=""><span>proxy_pass http://127.0.0.1:88;  </span></li><li class="alt"><span>  </span></li><li class=""><span>proxy_redirect off;  </span></li><li class="alt"><span>  </span></li><li class=""><span>proxy_set_header X-Real-IP $remote_addr;  </span></li><li class="alt"><span>  </span></li><li class=""><span>#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP  </span></li><li class="alt"><span>  </span></li><li class=""><span>proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  </span></li><li class="alt"><span>  </span></li><li class=""><span>#以下是一些反向代理的配置,可选。  </span></li><li class="alt"><span>  </span></li><li class=""><span>proxy_set_header Host $host;  </span></li><li class="alt"><span>  </span></li><li class=""><span>client_max_body_size 10m; #允许客户端请求的最大单文件字节数  </span></li><li class="alt"><span>  </span></li><li class=""><span>client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数,  </span></li><li class="alt"><span>  </span></li><li class=""><span>proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间(代理连接超时)  </span></li><li class="alt"><span>  </span></li><li class=""><span>proxy_send_timeout 90; #后端服务器数据回传时间(代理发送超时)  </span></li><li class="alt"><span>  </span></li><li class=""><span>proxy_read_timeout 90; #连接成功后,后端服务器响应时间(代理接收超时)  </span></li><li class="alt"><span>  </span></li><li class=""><span>proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小  </span></li><li class="alt"><span>  </span></li><li class=""><span>proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的设置  </span></li><li class="alt"><span>  </span></li><li class=""><span>proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)  </span></li><li class="alt"><span>  </span></li><li class=""><span>proxy_temp_file_write_size 64k;  </span></li><li class="alt"><span>  </span></li><li class=""><span>#设定缓存文件夹大小,大于这个值,将从upstream服务器传  </span></li><li class="alt"><span>  </span></li><li class=""><span>}  </span></li><li class="alt"><span>  </span></li><li class=""><span>   </span></li><li class="alt"><span>  </span></li><li class=""><span>#设定查看Nginx状态的地址  </span></li><li class="alt"><span>  </span></li><li class=""><span>location /NginxStatus {  </span></li><li class="alt"><span>  </span></li><li class=""><span>stub_status on;  </span></li><li class="alt"><span>  </span></li><li class=""><span>access_log on;  </span></li><li class="alt"><span>  </span></li><li class=""><span>auth_basic "NginxStatus";  </span></li><li class="alt"><span>  </span></li><li class=""><span>auth_basic_user_file confpasswd;  </span></li><li class="alt"><span>  </span></li><li class=""><span>#htpasswd文件的内容可以用apache提供的htpasswd工具来产生。  </span></li><li class="alt"><span>  </span></li><li class=""><span>}  </span></li><li class="alt"><span>  </span></li><li class=""><span>   </span></li><li class="alt"><span>  </span></li><li class=""><span>#本地动静分离反向代理配置  </span></li><li class="alt"><span>  </span></li><li class=""><span>#所有jsp的页面均交由tomcat或resin处理  </span></li><li class="alt"><span>  </span></li><li class=""><span>location ~ .(jsp|jspx|do)?$ {  </span></li><li class="alt"><span>  </span></li><li class=""><span>proxy_set_header Host $host;  </span></li><li class="alt"><span>  </span></li><li class=""><span>proxy_set_header X-Real-IP $remote_addr;  </span></li><li class="alt"><span>  </span></li><li class=""><span>proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  </span></li><li class="alt"><span>  </span></li><li class=""><span>proxy_pass http://127.0.0.1:8080;  </span></li><li class="alt"><span>  </span></li><li class=""><span>}  </span></li><li class="alt"><span>  </span></li><li class=""><span>#所有静态文件由nginx直接读取不经过tomcat或resin  </span></li><li class="alt"><span>  </span></li><li class=""><span>location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$  </span></li><li class="alt"><span>  </span></li><li class=""><span>{ expires 15d; }  </span></li><li class="alt"><span>  </span></li><li class=""><span>location ~ .*.(js|css)?$  </span></li><li class="alt"><span>  </span></li><li class=""><span>{ expires 1h; }  </span></li><li class="alt"><span>  </span></li><li class=""><span>}  </span></li><li class="alt"><span>  </span></li><li class=""><span>}  </span></li></ol><div class="save_code tracking-ad" data-mod="popu_249" style="display: none;"><a href="javascript:;" target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div><pre code_snippet_id="536844" snippet_file_name="blog_20141129_1_1889054" name="code" class="html" style="display: none;">#定义Nginx运行的用户和用户组

user www www;


#nginx进程数,建议设置为等于CPU总核心数。

worker_processes 8;


#全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]

error_log ar/loginx/error.log info;

 
#进程文件

pid ar/runinx.pid;

 
#一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(系统的值ulimit -n)与nginx进程数相除,但是nginx分配请求并不均匀,
所以建议与ulimit -n的值保持一致。

worker_rlimit_nofile 65535;

 

#工作模式与连接数上限

events

{

#参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,
如果跑在FreeBSD上面,就用kqueue模型。

use epoll;

#单个进程最大连接数(最大连接数=连接数*进程数)

worker_connections 65535;

}
 

#设定http服务器

http

{

include mime.types; #文件扩展名与文件类型映射表

default_type application/octet-stream; #默认文件类型

#charset utf-8; #默认编码

server_names_hash_bucket_size 128; #服务器名字的hash表大小

client_header_buffer_size 32k; #上传文件大小限制

large_client_header_buffers 4 64k; #设定请求缓

client_max_body_size 8m; #设定请求缓

sendfile on; #开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,
可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。

autoindex on; #开启目录列表访问,合适下载服务器,默认关闭。

tcp_nopush on; #防止网络阻塞

tcp_nodelay on; #防止网络阻塞

keepalive_timeout 120; #长连接超时时间,单位是秒

 

#FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。

fastcgi_connect_timeout 300;

fastcgi_send_timeout 300;

fastcgi_read_timeout 300;

fastcgi_buffer_size 64k;

fastcgi_buffers 4 64k;

fastcgi_busy_buffers_size 128k;

fastcgi_temp_file_write_size 128k;
 

#gzip模块设置

gzip on; #开启gzip压缩输出

gzip_min_length 1k; #最小压缩文件大小

gzip_buffers 4 16k; #压缩缓冲区

gzip_http_version 1.0; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)

gzip_comp_level 2; #压缩等级

gzip_types text/plain application/x-javascript text/css application/xml;

#压缩类型,默认就已经包含textml,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。

gzip_vary on;

#limit_zone crawler $binary_remote_addr 10m; #开启限制IP连接数的时候需要使用

 

upstream blog.ha97.com {

#upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。

server 192.168.80.121:80 weight=3;

server 192.168.80.122:80 weight=2;

server 192.168.80.123:80 weight=3;

}

 

#虚拟主机的配置

server

{

#监听端口

listen 80;

#域名可以有多个,用空格隔开

server_name www.ha97.com ha97.com;

index index.html index.htm index.php;

root /data/www/ha97;

location ~ .*.(php|php5)?$

{

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fastcgi.conf;

}

#图片缓存时间设置

location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$

{

expires 10d;

}

#JS和CSS缓存时间设置

location ~ .*.(js|css)?$

{

expires 1h;

}

#日志格式设定

log_format access '$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" $http_x_forwarded_for';

#定义本虚拟主机的访问日志

access_log ar/loginx/ha97access.log access;

 

#对 "/" 启用反向代理

location / {

proxy_pass http://127.0.0.1:88;

proxy_redirect off;

proxy_set_header X-Real-IP $remote_addr;

#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

#以下是一些反向代理的配置,可选。

proxy_set_header Host $host;

client_max_body_size 10m; #允许客户端请求的最大单文件字节数

client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数,

proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间(代理连接超时)

proxy_send_timeout 90; #后端服务器数据回传时间(代理发送超时)

proxy_read_timeout 90; #连接成功后,后端服务器响应时间(代理接收超时)

proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小

proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的设置

proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)

proxy_temp_file_write_size 64k;

#设定缓存文件夹大小,大于这个值,将从upstream服务器传

}

 

#设定查看Nginx状态的地址

location /NginxStatus {

stub_status on;

access_log on;

auth_basic "NginxStatus";

auth_basic_user_file confpasswd;

#htpasswd文件的内容可以用apache提供的htpasswd工具来产生。

}

 

#本地动静分离反向代理配置

#所有jsp的页面均交由tomcat或resin处理

location ~ .(jsp|jspx|do)?$ {

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_pass http://127.0.0.1:8080;

}

#所有静态文件由nginx直接读取不经过tomcat或resin

location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$

{ expires 15d; }

location ~ .*.(js|css)?$

{ expires 1h; }

}

}</pre>
<p>更详细的模块参数请参考:<a target="_blank" href="http://wiki.nginx.org/Main"><span style="color:#00ff">http://wiki.nginx.org/Main</span></a></p>
<h2><a name="t11"></a>案例</h2>
<p>Nginx+IIS<span style="font-family:宋体">服务器搭建服务器集群</span></p>
<p></p>
<p> <img src="https://img-blog.csdn.net/20141129230700578?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvem91eXVqaWUxMTI3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt=""></p>
<h3><a name="t12"></a>配置静态资源</h3>
<div class="dp-highlighter bg_html"><div class="bar"><div class="tools"><b>[html]</b> <a href="#" class="ViewSource" title="view plain" οnclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" οnclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 518px; top: 13709px; width: 29px; height: 14px; z-index: 99;"><embed id="ZeroClipboardMovie_2" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="29" height="14" name="ZeroClipboardMovie_2" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=2&width=29&height=14" wmode="transparent"></div></span><span data-mod="popu_169"> <a href="#" class="PrintSource" title="print" οnclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" οnclick="dp.sh.Toolbar.Command('About',this);return false;">?</a><span class="tracking-ad" data-mod="popu_167"><a href="https://code.csdn.net/snippets/536844" target="_blank" title="在CODE上查看代码片" style="text-indent:0;"><img src="https://code.csdn.net/assets/CODE_ico.png" width="12" height="12" alt="在CODE上查看代码片" style="position:relative;top:1px;left:2px;"></a></span><span class="tracking-ad" data-mod="popu_170"><a href="https://code.csdn.net/snippets/536844/fork" target="_blank" title="派生到我的代码片" style="text-indent:0;"><img src="https://code.csdn.net/assets/ico_fork.svg" width="12" height="12" alt="派生到我的代码片" style="position:relative;top:2px;left:2px;"></a></span></div></div><ol start="1" class="dp-xml"><li class="alt"><span><span>location ~ \.(jpg|png|jpeg|bmp|gif|swf|css)$  </span></span></li><li class=""><span>        {  </span></li><li class="alt"><span>            expires 30d;  </span></li><li class=""><span>             root /nginx-1.4.7;#root:  </span></li><li class="alt"><span>            break;  </span></li><li class=""><span>        }  </span></li></ol><div class="save_code tracking-ad" data-mod="popu_249" style="display: none;"><a href="javascript:;" target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div><pre code_snippet_id="536844" snippet_file_name="blog_20141129_2_1695864" name="code" class="html" style="display: none;">location ~ \.(jpg|png|jpeg|bmp|gif|swf|css)$
        {
            expires 30d;
             root /nginx-1.4.7;#root:
            break;
        }</pre>
<h3><a name="t13"></a>扩展</h3>
<p>负载均衡策略:<span style="font-family:Times New Roman">http://baidutech.blog.51cto.com/4114344/1033718/</span></p>
   
</div>




<!-- Baidu Button BEGIN -->




<div class="bdsharebuttonbox tracking-ad bdshare-button-style0-16" style="float: right;" data-mod="popu_172" data-bd-bind="1479456979304">
<a href="#" class="bds_more" data-cmd="more" style="background-position:0 0 !important; background-image: url(http://bdimg.share.baidu.com/static/api/img/share/icons_0_16.png?v=d754dcc0.png) !important" target="_blank"></a>
<a href="#" class="bds_qzone" data-cmd="qzone" title="分享到QQ空间" style="background-position:0 -52px !important" target="_blank"></a>
<a href="#" class="bds_tsina" data-cmd="tsina" title="分享到新浪微博" style="background-position:0 -104px !important" target="_blank"></a>
<a href="#" class="bds_tqq" data-cmd="tqq" title="分享到腾讯微博" style="background-position:0 -260px !important" target="_blank"></a>
<a href="#" class="bds_renren" data-cmd="renren" title="分享到人人网" style="background-position:0 -208px !important" target="_blank"></a>
<a href="#" class="bds_weixin" data-cmd="weixin" title="分享到微信" style="background-position:0 -1612px !important" target="_blank"></a>
</div>
<script>window._bd_share_config = { "common": { "bdSnsKey": {}, "bdText": "", "bdMini": "1", "bdMiniList": false, "bdPic": "", "bdStyle": "0", "bdSize": "16" }, "share": {} }; with (document) 0[(getElementsByTagName('head')[0] || body).appendChild(createElement('script')).src = 'http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion=' + ~(-new Date() / 36e5)];</script>
<!-- Baidu Button END -->

   <link rel="stylesheet" href="http://static.blog.csdn.net/css/blog_detail.css">

    
<!--172.16.140.11-->

<!-- Baidu Button BEGIN -->
<script type="text/javascript" id="bdshare_js" data="type=tools&uid=1536434" src="http://bdimg.share.baidu.com/static/js/bds_s_v2.js?cdnversion=410961"></script>

<script type="text/javascript">
    document.getElementById("bdshell_js").src = "http://bdimg.share.baidu.com/static/js/shell_v2.js?cdnversion=" + Math.ceil(new Date()/3600000)
</script>
<!-- Baidu Button END -->

 

        <div id="digg" articleid="41605063">
            <dl id="btnDigg" class="digg digg_enable" οnclick="btndigga();">
               
                 <dt>顶</dt>
                <dd>6</dd>
            </dl>
           
              
            <dl id="btnBury" class="digg digg_enable" οnclick="btnburya();">
              
                  <dt>踩</dt>
                <dd>0</dd>               
            </dl>
            
        </div>
     <div class="tracking-ad" data-mod="popu_222"><a href="javascript:void(0);" target="_blank"> </a>   </div>
    <div class="tracking-ad" data-mod="popu_223"> <a href="javascript:void(0);" target="_blank"> </a></div>
    <script type="text/javascript">
                function btndigga() {
                    $(".tracking-ad[data-mod='popu_222'] a").click();
                }
                function btnburya() {
                    $(".tracking-ad[data-mod='popu_223'] a").click();
                }
            </script>

   <ul class="article_next_prev">
                <li class="prev_article"><span οnclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_shangyipian']);location.href='/zouyujie1127/article/details/41412009';">上一篇</span><a href="/zouyujie1127/article/details/41412009" οnclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_shangyipian'])">文件并发(日志处理)--队列--Redis+Log4Net</a></li>
                <li class="next_article"><span οnclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_xiayipian']);location.href='/zouyujie1127/article/details/41801781';">下一篇</span><a href="/zouyujie1127/article/details/41801781" οnclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_xiayipian'])">sqlserver报表统计——参数化动态PIVOT行转列</a></li>
    </ul>

    <div style="clear:both; height:10px;"></div>


        <div class="similar_article" style="">
                <h4>我的同类文章</h4>
                <div class="similar_c" style="margin:20px 0px 0px 0px">
                    <div class="similar_c_t">
                                <label class="similar_cur">
                                    <span style="cursor:pointer" οnclick="GetCategoryArticles('702315','zouyujie1127','foot','41605063');">ASP.NET WebForm开发<em>(77)</em></span>
                                </label>
                                <label class="">
                                    <span style="cursor:pointer" οnclick="GetCategoryArticles('2731949','zouyujie1127','foot','41605063');">ASP.NET 分布式开发<em>(5)</em></span>
                                </label>
                    </div>
                   
                    <div class="similar_wrap tracking-ad" data-mod="popu_141" style="max-height:195px;">
                        <a href="http://blog.csdn.net" style="display:none" target="_blank">http://blog.csdn.net</a>
                        <ul class="similar_list fl"><li><em>•</em><a href="http://blog.csdn.net/zouyujie1127/article/details/44564475" id="foot_aritcle_44564475undefined694692931137979" target="_blank" title="如何禁用Visual Studio 2013的浏览器链接功能">如何禁用Visual Studio 2013的浏览器链接功能</a><span>2015-03-23</span><label><i>阅读</i><b>1700</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/zouyujie1127/article/details/44118321" id="foot_aritcle_44118321undefined5338112295139581" target="_blank" title="Lucene.net站内搜索2—Lucene.Net简介和分词">Lucene.net站内搜索2—Lucene.Net简介和分词</a><span>2015-03-07</span><label><i>阅读</i><b>1563</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/zouyujie1127/article/details/43580711" id="foot_aritcle_43580711undefined8716883163433522" target="_blank" title="6个重要的.NET概念栈,堆,值类型,引用类型,装箱,拆箱">6个重要的.NET概念栈,堆,值类型,引用类型,装箱,拆箱</a><span>2015-02-07</span><label><i>阅读</i><b>764</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/zouyujie1127/article/details/42125587" id="foot_aritcle_42125587undefined3817254544701427" target="_blank" title="aspx页面用一个遮罩层显示正在加载,加载完后隐藏该div">aspx页面用一个遮罩层显示正在加载,加载完后隐藏该div</a><span>2014-12-24</span><label><i>阅读</i><b>4651</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/zouyujie1127/article/details/41383099" id="foot_aritcle_41383099undefined5684253717772663" target="_blank" title="ASP.NET Redis 开发">ASP.NET Redis 开发</a><span>2014-11-22</span><label><i>阅读</i><b>13212</b></label></li> </ul>

                        <ul class="similar_list fr"><li><em>•</em><a href="http://blog.csdn.net/zouyujie1127/article/details/44121713" id="foot_aritcle_44121713undefined35759664326906204" target="_blank" title="Lucene.net站内搜索3—最简单搜索引擎代码">Lucene.net站内搜索3—最简单搜索引擎代码</a><span>2015-03-07</span><label><i>阅读</i><b>2358</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/zouyujie1127/article/details/44066337" id="foot_aritcle_44066337undefined4802414826117456" target="_blank" title="Lucene.net站内搜索1——SEO优化">Lucene.net站内搜索1——SEO优化</a><span>2015-03-04</span><label><i>阅读</i><b>951</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/zouyujie1127/article/details/42194451" id="foot_aritcle_42194451undefined4068393656052649" target="_blank" title="弹出新界面,在新界面执行方法后传参给调用界面,再执行调用界面的方法">弹出新界面,在新界面执行方法后传参给调用界面,再执行调用界面的方法</a><span>2014-12-27</span><label><i>阅读</i><b>1555</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/zouyujie1127/article/details/41411195" id="foot_aritcle_41411195undefined4129489930346608" target="_blank" title="Redis主从复制">Redis主从复制</a><span>2014-11-23</span><label><i>阅读</i><b>5238</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/zouyujie1127/article/details/41382757" id="foot_aritcle_41382757undefined7335972827859223" target="_blank" title="ReportingServies报表开发实例教程">ReportingServies报表开发实例教程</a><span>2014-11-22</span><label><i>阅读</i><b>1811</b></label></li> </ul>
                    <a href="http://blog.csdn.net/zouyujie1127/article/category/702315" class="MoreArticle">更多文章</a></div>
                </div>
            </div>    
    <script type="text/javascript">
        $(function () {
            GetCategoryArticles('702315', 'zouyujie1127','foot','41605063');
        });
    </script>
      
</div>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值