u1jodi1q 发表于 2024-11-2 16:44:30

前端必须 - Nginx 负载平衡和缓存实践


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/z5Pdm7u7moZX3aibUfaJQFvdywKzIOhHpfWDQrOcFAbESs3rNlEfvbQCPlaxt7OhaZBhvdgEYtKobBfEFOmb0lw/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><span style="color: black;">基本</span>篇</span></h3>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><span style="color: black;">1、</span>环境</span></h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">服务器版本:CentOS 7.2</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">为了<span style="color: black;">保准</span><strong style="color: blue;">学习<span style="color: black;">周期</span></strong>不遇到奇怪的事情,请<span style="color: black;">保准</span>以下四点(大神<span style="color: black;">选取</span>性无视)</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">确认系统网络</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">确认yum可用</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">确认关闭iptables</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">确认停用selinux</span></p><span style="color: black;">#查看iptables状态</span><span style="color: black;">systemctl</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;status&nbsp;firewalld.service</p><span style="color: black;">#关闭防火墙(临时关闭)</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">systemctl&nbsp;stop&nbsp;firewalld.service</p><span style="color: black;">#查看SELinux状态</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">getenforce</p><span style="color: black;">#临时关闭SELinux</span>setenforce&nbsp;<span style="color: black;">0</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">安装<span style="color: black;">有些</span>系统基本工具,正常<span style="color: black;">状况</span>系统都会自带(<span style="color: black;">无</span>在装哦)</span></p>yum&nbsp;-y&nbsp;<span style="color: black;">install</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">gcc&nbsp;gcc-c++&nbsp;autoconf&nbsp;pcre&nbsp;pcre-devel&nbsp;make&nbsp;automake</p>yum&nbsp;-y&nbsp;<span style="color: black;">install</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;wget&nbsp;httpd-tools&nbsp;vim</p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><span style="color: black;">2、</span>Nginx是什么?</span></h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Nginx是一个开源且高性能、<span style="color: black;">靠谱</span>的HTTP中间件、代理服务</p>其他的HTTP服务:

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">HTTPD-Apache基金会</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">IIS-微软</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">GWS-Google(不对外开放)</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">近几年,Nginx的市场占有率越来越高,一度飙升,<span style="color: black;">为何</span>呢?接下来<span style="color: black;">咱们</span>就<span style="color: black;">晓得</span>了!</span></p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><span style="color: black;">3、</span><span style="color: black;">咱们</span><span style="color: black;">为何</span><span style="color: black;">选取</span>Nginx?</span></h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">1. IO多路复用epoll(IO复用)</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">怎样</span>理解呢?举个例子吧!</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">有A、B、C三个老师,<span style="color: black;">她们</span>都遇到一个<span style="color: black;">困难</span>,要<span style="color: black;">帮忙</span>一个班级的学生<span style="color: black;">处理</span>课堂作业。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">老师A采用从<span style="color: black;">第1</span>排<span style="color: black;">起始</span>一个学生一个学生轮流解答的方式去回答问题,老师A浪费了<span style="color: black;">非常多</span>时间,并且有的学生作业还<span style="color: black;">无</span>完成呢,老师就来了,反反复复效率极慢。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">老师B是一个忍者,他<span style="color: black;">发掘</span>老师A的<span style="color: black;">办法</span>行不通,于是他<span style="color: black;">运用</span>了影分身术,分身出好几个自己同一时间去帮好几个<span style="color: black;">朋友</span>回答问题,最后还没回答完,老师B消耗光了能量累倒了。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">老师C比较精明,他告诉学生,谁完<span style="color: black;">成为了</span>作业举手,有举手的<span style="color: black;">朋友</span>他才去<span style="color: black;">指点</span>问题,他让学生主动发声,<span style="color: black;">掰开</span>了“并发”。</p>这个老师C<span style="color: black;">便是</span>Nginx。

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">2. 轻量级</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">功能模块少 - Nginx仅<span style="color: black;">保存</span>了HTTP需要的模块,其他都用插件的方式,后天添加</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">代码模块化 - 更适合二次<span style="color: black;">研发</span>,如阿里巴巴Tengine</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">3. CPU亲和</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">把CPU核心和Nginx工作进程绑定,把<span style="color: black;">每一个</span>worker进程固定在一个CPU上执行,减少切换CPU的cache miss,从而<span style="color: black;">加强</span>性能。</span></p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><span style="color: black;">3、</span>安装与目录</span></h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">自己</span><span style="color: black;">运用</span>了鸟哥的lnmp集成包 https://lnmp.org,简单方便-<span style="color: black;">举荐</span>!</span></p><span style="color: black;">#执行这句语句,<span style="color: black;">按照</span>指引,将安装&nbsp;nginx&nbsp;php&nbsp;mysql&nbsp;可进入lnmp官网查看更<span style="color: black;">仔细</span>的过程</span><span style="color: black;">#默认安装目录/usr/local</span>wget&nbsp;-c&nbsp;http://soft.vpser.net/lnmp/lnmp1.4.tar.gz&nbsp;&amp;&amp;&nbsp;tar&nbsp;zxf&nbsp;lnmp1.4.tar.gz&nbsp;&amp;&amp;<span style="color: black;">cd</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;lnmp1.4&nbsp;&amp;&amp;&nbsp;./install.sh&nbsp;lnmp</p><span style="color: black;">#默认安装目录</span>/usr/<span style="color: black;">local</span>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><span style="color: black;">4、</span>基本配置</span></h2><span style="color: black;">#打开主配置文件,若你是用lnmp环境安装</span><span style="color: black;">vim</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;/usr/local/nginx/conf/nginx.conf</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">----------------------------------------</p>user&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#设置nginx服务的系统<span style="color: black;">运用</span>用户</span>worker_processes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#工作进程数&nbsp;<span style="color: black;">通常</span><span style="color: black;">状况</span>与CPU核数保持一致</span>error_log&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#nginx的错误日志</span>pid&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#nginx<span style="color: black;">起步</span>时的pid</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">events&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">worker_connections</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#<span style="color: black;">每一个</span>进程<span style="color: black;">准许</span>最大连接数</span>&nbsp;&nbsp;&nbsp;&nbsp;use&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#nginx<span style="color: black;">运用</span>的内核模型</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">咱们</span><span style="color: black;">运用</span> nginx 的 http 服务,在配置文件 nginx.conf 中的 http 区域<strong style="color: blue;">内</strong>,配置无数个 server ,每一个 server 对应这一个虚拟主机<span style="color: black;">或</span>域名</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">http&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">...</span>&nbsp;<span style="color: black;">...</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#后面再<span style="color: black;">仔细</span>介绍&nbsp;http&nbsp;配置项目</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;server&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;listen&nbsp;<span style="color: black;">80</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#监听端口;</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server_name&nbsp;localhost&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#<span style="color: black;">位置</span></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;location&nbsp;/&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#<span style="color: black;">拜访</span>首页路径</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;root&nbsp;/xxx/xxx/index.html&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#默认目录</span>index&nbsp;index.html&nbsp;index.htm<span style="color: black;">#默认文件</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;error_page&nbsp;&nbsp;<span style="color: black;">500</span>&nbsp;<span style="color: black;">504</span>&nbsp;&nbsp;&nbsp;/50x.html&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#当<span style="color: black;">显现</span>以上状态码时从新定义到50x.html</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;location&nbsp;=&nbsp;/50x.html&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#当<span style="color: black;">拜访</span>50x.html时</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;root&nbsp;/xxx/xxx/html&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#50x.html&nbsp;页面所在位置</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;server&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">...</span>&nbsp;<span style="color: black;">...</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">一个 server <span style="color: black;">能够</span><span style="color: black;">显现</span>多个 location ,<span style="color: black;">咱们</span>对<span style="color: black;">区别</span>的<span style="color: black;">拜访</span>路径进行<span style="color: black;">区别</span><span style="color: black;">状况</span>的配置</p><span style="color: black;">咱们</span>再来<span style="color: black;">瞧瞧</span> http 的配置详情
    <span style="color: black;">http</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">sendfile</span>&nbsp;&nbsp;<span style="color: black;">on</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#<span style="color: black;">有效</span>传输文件的模式&nbsp;<span style="color: black;">必定</span>要开启</span>&nbsp;&nbsp;&nbsp;&nbsp;keepalive_timeout&nbsp;&nbsp;&nbsp;<span style="color: black;">65</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#客户端服务端请求超时时间</span>log_format&nbsp;&nbsp;main&nbsp;&nbsp;&nbsp;XXX<span style="color: black;">#定义日志格式&nbsp;代号为main</span>&nbsp;&nbsp;&nbsp;&nbsp;access_log&nbsp;&nbsp;/usr/local/access.log&nbsp;&nbsp;main&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#日志<span style="color: black;">保留</span><span style="color: black;">位置</span>&nbsp;格式代码&nbsp;main</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><span style="color: black;">5、</span>模块</span></h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">查看 nginx 已开启和编联进去的模块,模块太多了,就不在这长篇大论,有需要<span style="color: black;">自动</span>百度吧~</span></p><span style="color: black;">#大写V查看所有模块,小写v查看版本</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">nginx&nbsp;-V</p><span style="color: black;">#&nbsp;查看此配置文件&nbsp;<span style="color: black;">是不是</span>存在语法错误</span>nginx&nbsp;-tc&nbsp;/usr/<span style="color: black;">local</span>/nginx/conf/nginx.conf<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">场景实现篇</span></h3>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><span style="color: black;">1、</span>静态资源WEB服务</span></h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">1.静态资源类型</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">非服务器动态运行生成的文件,换句话说,<span style="color: black;">便是</span><span style="color: black;">能够</span>直接在服务器上找到对应文件的请求</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">浏览器端渲染:HTML,CSS,JS</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">照片</span>:JPEG,GIF,PNG</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">视频:FLV,MPEG</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">文件:TXT,任意下载文件</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">2.静态资源服务场景-CDN</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">什么是CDN?例如一个北京用户要请求一个文件,而文件放在的新疆的资源存储中心,<span style="color: black;">倘若</span>直接请求新疆距离太远,延迟久。<span style="color: black;">运用</span>nginx静态资源回源,分发给北京的资源存储中心,让用户请求的动态定位到北京的资源存储中心请求,实现传输延迟的最小化</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">2.nginx静态资源配置</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">配置域:http、server、location</p><span style="color: black;">#文件高速读取</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">http&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sendfile&nbsp;&nbsp;&nbsp;<span style="color: black;">on</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">#在&nbsp;sendfile&nbsp;开启的<span style="color: black;">状况</span>下,开启&nbsp;tcp_nopush&nbsp;<span style="color: black;">加强</span>网络包传输效率</span><span style="color: black;">#tcp_nopush 将文件一次性<span style="color: black;">一块</span>传输给客户端,就<span style="color: black;">好似</span>你有十个包裹,快递员一次送一个,来回十趟,开启后,快递员讲等待你十个包裹都派件,一趟<span style="color: black;">一块</span>送给你</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">http&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sendfile&nbsp;&nbsp;&nbsp;<span style="color: black;">on</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tcp_nopush&nbsp;<span style="color: black;">on</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">#tcp_nodelay&nbsp;开启实时传输,传输方式与&nbsp;tcp_nopush&nbsp;相反,追求实时性,<span style="color: black;">然则</span>它<span style="color: black;">仅有</span>在长连接下才生效</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">http&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sendfile&nbsp;&nbsp;&nbsp;<span style="color: black;">on</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>tcp_nopush<span style="color: black;">on</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tcp_nodelay&nbsp;<span style="color: black;">on</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">#将<span style="color: black;">拜访</span>的文件压缩传输&nbsp;(减少文件资源<span style="color: black;">体积</span>,<span style="color: black;">加强</span>传输速度)</span><span style="color: black;">#当<span style="color: black;">拜访</span>内容以gif或jpg结尾的资源时</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">location&nbsp;~&nbsp;.*\.(gif|jpg)$&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;gzip&nbsp;<span style="color: black;">on</span>;&nbsp;<span style="color: black;">#开启</span>&nbsp;&nbsp;&nbsp;&nbsp;gzip_http_version&nbsp;<span style="color: black;">1.1</span>;&nbsp;<span style="color: black;">#服务器传输版本</span>gzip_comp_level<span style="color: black;">2</span>;&nbsp;<span style="color: black;">#压缩比,越高压缩越多,压缩越高可能会消耗服务器性能</span>gzip_types&nbsp;&nbsp;&nbsp;text/plain&nbsp;application/javascript&nbsp;application/x-javascript&nbsp;text/javascript&nbsp;text/css&nbsp;application/xml&nbsp;application/xml+rss&nbsp;image/jpeg&nbsp;image/gif&nbsp;image/png;<span style="color: black;">#压缩文件类型</span>&nbsp;&nbsp;&nbsp;&nbsp;root&nbsp;/opt/app/code;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#对应目录(去该目录下寻找对应文件)</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">#直接<span style="color: black;">拜访</span>已压缩文件</span><span style="color: black;">#当<span style="color: black;">拜访</span>路径以download开头时,如www.baidu.com/download/test.img</span><span style="color: black;">#去/opt/app/code目录下寻找test.img.gz文件,返回到前端时<span style="color: black;">已经是</span><span style="color: black;">能够</span>浏览的img文件</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">location&nbsp;~&nbsp;load^/download&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;gzip_static&nbsp;<span style="color: black;">on</span>&nbsp;<span style="color: black;">#开启;</span>&nbsp;&nbsp;&nbsp;&nbsp;tcp_nopush&nbsp;<span style="color: black;">on</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;root&nbsp;/opt/app/code;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><span style="color: black;">2、</span>浏览器缓存</span></h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">HTTP协议定义的缓存机制(如:Expires; Cache-control等 )</p>减少服务端的消耗,降低延迟

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">1.浏览器无缓存</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">浏览器请求 -&gt; 无缓存 -&gt; 请求WEB服务器 -&gt; 请求相应 -&gt; 呈现</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在<strong style="color: blue;">呈现</strong><span style="color: black;">周期</span>会<span style="color: black;">按照</span>缓存的设置在浏览器中生成缓存</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">2.浏览器有缓存</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">浏览器请求 -&gt; 有缓存 -&gt; 校验本地缓存时间<span style="color: black;">是不是</span>过期 -&gt; <span style="color: black;">无</span>过期 -&gt; 呈现</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">若过期从新请求WEB服务器</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">3.语法配置</strong></span></p><span style="color: black;">location</span>&nbsp;<span style="color: black;">~&nbsp;.*\.(html|htm)$</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">expires</span>&nbsp;<span style="color: black;">12h</span>;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#缓存12小时</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">服务器响应静态文件时,请求头信息会带上 etag 和 last_modified_since 2个标签值,浏览器下次去请求时,头信息发送这两个标签,服务器检测文件有<span style="color: black;">无</span><span style="color: black;">出现</span>变化,如无,直接头信息返 etag 和last_modified_since,状态码为<strong style="color: blue;">304</strong>&nbsp;,浏览器<span style="color: black;">晓得</span>内容无改变,于是直接调用本地缓存,这个过程<span style="color: black;">亦</span>请求了服务,<span style="color: black;">然则</span>传着的内容极少</span></p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><span style="color: black;">3、</span>跨站<span style="color: black;">拜访</span></span></h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">研发</span>nginx跨站<span style="color: black;">拜访</span>设置</span></p><span style="color: black;">location</span>&nbsp;<span style="color: black;">~&nbsp;.*\.(html|htm)$</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">add_header</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;Access-Control-Allow-Origin&nbsp;*;</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">add_header</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;Access-Control-Allow-Methods&nbsp;GET,POST,PUT,DELETE,OPTIONS;</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#Access-Control-Allow-Credentials&nbsp;true&nbsp;#<span style="color: black;">准许</span>cookie跨域</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在响应中指定 Access-Control-Allow-Credentials 为 true 时,Access-Control-Allow-Origin <span style="color: black;">不可</span>指定为 *,需要指定到<span style="color: black;">详细</span>域名</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">关联</span>跨域内容可参考&nbsp;Laravel 跨域功能中间件&nbsp;<span style="color: black;">运用</span>代码实现跨域,原理与nginx跨域配置相同</span></p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><span style="color: black;">4、</span>防盗链</span></h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">防止服务器内的静态资源被其他网站所套用</p>此处介绍的 nginx 防盗链为<span style="color: black;">基本</span>方式,其它更加深入的方式将在之后的<span style="color: black;">文案</span>介绍

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">首要</span>,需要理解一个nginx变量</span></p><span style="color: black;">$http_referer</span>&nbsp;<span style="color: black;">#<span style="color: black;">暗示</span>当前请求上一次页面<span style="color: black;">拜访</span>的<span style="color: black;">位置</span>,换句话说,<span style="color: black;">拜访</span>&nbsp;www.baidu.com&nbsp;主页,这是<span style="color: black;">第1</span>次<span style="color: black;">拜访</span>,<span style="color: black;">因此</span>&nbsp;$http_referer&nbsp;为空,<span style="color: black;">然则</span>&nbsp;<span style="color: black;">拜访</span>此页面的时候还需要获取一张首页<span style="color: black;">照片</span>,再请求这张<span style="color: black;">照片</span>的时候&nbsp;$http_referer&nbsp;就为&nbsp;www.baidu.com</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">而后</span>配置</span></p><span style="color: black;">location</span>&nbsp;<span style="color: black;">~&nbsp;.*\.(jpg|gif)$</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#valid_referers&nbsp;<span style="color: black;">暗示</span><span style="color: black;">咱们</span><span style="color: black;">准许</span><span style="color: black;">那些</span>&nbsp;$http_referer&nbsp;来<span style="color: black;">拜访</span></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#none&nbsp;<span style="color: black;">暗示</span><span style="color: black;">无</span>带&nbsp;$http_referer,如<span style="color: black;">第1</span>次<span style="color: black;">拜访</span>时&nbsp;$http_referer&nbsp;为空</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#blocked&nbsp;<span style="color: black;">暗示</span>&nbsp;$http_referer&nbsp;不是标准的<span style="color: black;">位置</span>,非正常域名等</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#只<span style="color: black;">准许</span>此ip</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">valid_referers</span>&nbsp;<span style="color: black;">none</span>&nbsp;<span style="color: black;">blocked</span>&nbsp;<span style="color: black;">127</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.xxx.xxx.xx</p>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(<span style="color: black;">$invalid_referer</span>)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#不满足<span style="color: black;">状况</span>下变量值为1</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">return</span>&nbsp;<span style="color: black;">403</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><span style="color: black;">5、</span>HTTP代理服务</span></h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Nginx<span style="color: black;">能够</span>实现多种代理方式</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">HTTP</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">ICMPPOPIMAP</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">HTTPS</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">RTMP</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">1. 代理区别</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">区别在于代理的对象不<span style="color: black;">同样</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">正向代理代理的对象是客户端</p>反向代理代理的对象是服务端

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">2. 反向代理</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">语法:proxy_pass URL</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">默认:——</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">位置:loaction</p><span style="color: black;">#代理端口</span><span style="color: black;">#场景:服务器80端口开放,8080端口对外关闭,客户端需要<span style="color: black;">拜访</span>到8080</span><span style="color: black;">#在nginx中配置proxy_pass代理转发时,<span style="color: black;">倘若</span>在proxy_pass后面的url加/,<span style="color: black;">暗示</span>绝对根路径;<span style="color: black;">倘若</span><span style="color: black;">无</span>/,<span style="color: black;">暗示</span>相对路径,把匹配的路径部分<span style="color: black;">亦</span>给代理走</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">server&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;listen&nbsp;<span style="color: black;">80</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;location&nbsp;/&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_pass&nbsp;http:<span style="color: black;">//127.0.0.1:8080/;</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_redirect&nbsp;<span style="color: black;">default</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_set_header&nbsp;Host&nbsp;$http_host;</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_set_header&nbsp;X-Real-IP&nbsp;$remote_addr;&nbsp;<span style="color: black;">#获取客户端真实IP</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_connect_timeout&nbsp;<span style="color: black;">30</span>;&nbsp;<span style="color: black;">#超时时间</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_send_timeout&nbsp;<span style="color: black;">60</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_read_timeout&nbsp;<span style="color: black;">60</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_buffer_size&nbsp;<span style="color: black;">32</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">k;</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_buffering&nbsp;on;&nbsp;<span style="color: black;">#开启缓冲区,减少磁盘io</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_buffers&nbsp;<span style="color: black;">4</span>&nbsp;<span style="color: black;">128</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">k;</p>proxy_busy_buffers_size<span style="color: black;">256</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">k;</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_max_temp_file_size&nbsp;<span style="color: black;">256</span>k;&nbsp;<span style="color: black;">#当超过内存<span style="color: black;">准许</span>储蓄<span style="color: black;">体积</span>,存到文件</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>}<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">负载<span style="color: black;">平衡</span>和缓存服务</span></h3>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><span style="color: black;">1、</span>负载<span style="color: black;">平衡</span></span></h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">负载<span style="color: black;">平衡</span>的实现<span style="color: black;">办法</span><span style="color: black;">便是</span><span style="color: black;">咱们</span>上章介绍的<strong style="color: blue;">反向代理</strong>&nbsp;。将客户的请求<span style="color: black;">经过</span> nginx 分发(反向代理)到一组多台<span style="color: black;">区别</span>的服务器上</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这一组服务器<span style="color: black;">咱们</span><span style="color: black;">叫作</span>为<strong style="color: blue;">服务池(upstream server)</strong>,池内的每一个服务器<span style="color: black;">叫作</span>为一个&nbsp;<strong style="color: blue;">单元</strong>,服务池内将对每一个单元进行请求轮训,实现负载<span style="color: black;">平衡</span></span></p><span style="color: black;">#配置</span>语法:upstream name <span style="color: black;">...</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">默认:——</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">位置:http</p>upstream&nbsp;<span style="color: black;">#自定义组名&nbsp;{</span>&nbsp;&nbsp;&nbsp;&nbsp;server&nbsp;x1.baidu.com;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#<span style="color: black;">能够</span>是域名</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">server&nbsp;x2.baidu.com;</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#server&nbsp;x3.baidu.com</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#down&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;不参与负载<span style="color: black;">平衡</span></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#weight=5;&nbsp;&nbsp;&nbsp;&nbsp;权重,越高分配越多</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#backup;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;预留的备份服务器</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#max_fails&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">准许</span>失败的次数</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#fail_timeout&nbsp;超过失败次数后,服务暂停时间</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#max_coons&nbsp;&nbsp;&nbsp;&nbsp;限制最大的接受的连接数</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#<span style="color: black;">按照</span>服务器性能<span style="color: black;">区别</span>,配置适合的参数</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#server&nbsp;106.xx.xx.xxx;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">能够</span>是ip</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#server&nbsp;106.xx.xx.<span style="color: black;">xxx:</span>8080;&nbsp;&nbsp;&nbsp;<span style="color: black;">能够</span>带端口号</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#server unix:/tmp/xxx;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;支出socket方式</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">假设<span style="color: black;">咱们</span>有三台服务器,并且假设它们的IP<span style="color: black;">位置</span>,前端负载<span style="color: black;">平衡</span>服务器A(127.0.0.1),后台服务器B(127.0.0.2),后台服务器C(127.0.0.3)</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">新建文件&nbsp;</span><span style="color: black;">proxy.conf</span><span style="color: black;">,内容如下,上一章介绍的反向代理配置</span></p>proxy_redirect&nbsp;<span style="color: black;">default</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">proxy_set_header&nbsp;Host&nbsp;$http_host;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">proxy_set_header&nbsp;X-Real-IP&nbsp;$remote_addr;</p>proxy_connect_timeout&nbsp;<span style="color: black;">30</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>proxy_send_timeout&nbsp;<span style="color: black;">60</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>proxy_read_timeout&nbsp;<span style="color: black;">60</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>proxy_buffer_size&nbsp;<span style="color: black;">32</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">k;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">proxy_buffering&nbsp;on;</p>proxy_buffers&nbsp;<span style="color: black;">4</span>&nbsp;<span style="color: black;">128</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">k;</p>proxy_busy_buffers_size&nbsp;<span style="color: black;">256</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">k;</p>proxy_max_temp_file_size&nbsp;<span style="color: black;">256</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">k;</p><span style="color: black;">#服务器A的配置</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">http&nbsp;{</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;...</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;upstream&nbsp;xxx&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server&nbsp;<span style="color: black;">127.0.0.2</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server&nbsp;<span style="color: black;">127.0.0.3</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;server&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;liseten&nbsp;<span style="color: black;">80</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server_name&nbsp;localhost;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;location&nbsp;/&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_pass&nbsp;http:<span style="color: black;">//xxx&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#upstream&nbsp;对应自定义名<span style="color: black;">叫作</span></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">include</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;proxy.conf;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">#服务器B、服务器C的配置</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">server&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;liseten&nbsp;<span style="color: black;">80</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;server_name&nbsp;localhost;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;location&nbsp;/&nbsp;{</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;index&nbsp;&nbsp;index.html</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">调度算法</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">轮训:按时间<span style="color: black;">次序</span>逐一分配到<span style="color: black;">区别</span>的后端服务器</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">加权轮训:weight值越大,分配到的几率越高</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">ip_hash:<span style="color: black;">每一个</span>请求按<span style="color: black;">拜访</span>IP的hash结果分配,<span style="color: black;">这般</span>来自同一个IP固定<span style="color: black;">拜访</span>一个后端服务器</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">least_conn:最少链接数,哪个<span style="color: black;">设备</span>连接数少就分发给谁</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">url_hash:<span style="color: black;">根据</span><span style="color: black;">拜访</span>的URL的hash结果来分配请求,每一个URL定向到同一个后端服务器</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">hash关键数值:hash自定义key</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">ip_hash</span><span style="color: black;">&nbsp;配置</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;upstream&nbsp;xxx&nbsp;{</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ip_hash;</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">server</span>&nbsp;<span style="color: black;">127.0.0.2</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">server</span>&nbsp;<span style="color: black;">127.0.0.3</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">ip_hash存在缺陷,当前端服务器再多一层时,将获取不到用户的正确IP,获取的将是前一个前端服务器的IP,<span style="color: black;">因此呢</span> nginx1.7.2版本推出了 url_hash</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">url_hash</span><span style="color: black;">&nbsp;配置</span></p>&nbsp;&nbsp;<span style="color: black;">upstream</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;xxx&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">hash</span>&nbsp;<span style="color: black;">$request_uri</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">server</span>&nbsp;<span style="color: black;">127.0.0.2</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">server</span>&nbsp;<span style="color: black;">127.0.0.3</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;}</p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><span style="color: black;">2、</span>缓存服务</span></h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">1. 缓存类型</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">服务端缓存:缓存存储在后端服务器,如redis,memcache</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">代理缓存:缓存存储在代理服务器<span style="color: black;">或</span>中间件上,它的内容是从后端服务器获取的,<span style="color: black;">然则</span><span style="color: black;">保留</span>在自己本地</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">客户端缓存:缓存在浏览器内的</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">2. nginx 代理缓存</strong>客户端请求nginx,nginx查看本地<span style="color: black;">是不是</span>有缓存数据,若有直接返回给客户端,若<span style="color: black;">无</span>再去后端服务器请求</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">http&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;proxy_cache_path&nbsp;&nbsp;&nbsp;&nbsp;/var/www/cache&nbsp;<span style="color: black;">#缓存<span style="color: black;">位置</span></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;levels=1:2&nbsp;<span style="color: black;">#目录分级</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;keys_zone=test_cache:10m&nbsp;<span style="color: black;">#开启的keys空间名字:空间<span style="color: black;">体积</span>(1m<span style="color: black;">能够</span>存放8000个key)</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;max_size=10g&nbsp;<span style="color: black;">#目录最大<span style="color: black;">体积</span>(超过时,不常用的将被删除)</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inactive=60m&nbsp;<span style="color: black;">#60分钟内<span style="color: black;">无</span>被<span style="color: black;">拜访</span>的缓存将清理</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;use_temp_path=pff;&nbsp;<span style="color: black;">#<span style="color: black;">是不是</span>开启存放临时文件目录,关闭默认存储在缓存<span style="color: black;">位置</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;server&nbsp;{</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;location&nbsp;/&nbsp;{</p>proxy_cache&nbsp;test_cache;<span style="color: black;">#开启缓存对应的名<span style="color: black;">叫作</span>,在keys_zone命名好</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_cache_valid&nbsp;200&nbsp;304&nbsp;12h;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#状态码为200&nbsp;304的缓存12小时</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_cache_valid&nbsp;any&nbsp;10m;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#其他状态缓存10小时</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_cache_key&nbsp;<span style="color: black;">$host$uri$is_args$args</span>;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#设置key值</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add_header&nbsp;Nginx-Cache&nbsp;<span style="color: black;">"<span style="color: black;">$upstream_cache_status</span>"</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">当有个特定请求<span style="color: black;">咱们</span>不需要缓存的时候,在上面配置的内容中加入以下配置</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">server&nbsp;{</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;...</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">if</span>&nbsp;(<span style="color: black;">$request_uri</span>&nbsp;~&nbsp;^/(login|register)&nbsp;)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#当请求<span style="color: black;">位置</span>有login或register时</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">set</span>&nbsp;<span style="color: black;">$nocache</span>&nbsp;=&nbsp;1;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#设置一个自定义变量为true</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;location&nbsp;/&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_no_cache&nbsp;<span style="color: black;">$nocache</span>&nbsp;<span style="color: black;">$arg_nocache</span>&nbsp;<span style="color: black;">$arg_comment</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_no_cache&nbsp;<span style="color: black;">$http_pragma</span>&nbsp;<span style="color: black;">$http_authoriztion</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">3. 分片请求</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">初期</span>版本 nginx 对大文件的分片请求不支持缓存,1.9版本后slice模块实现了这个功能</p>前端发起请求,nginx去获取这个请求文件的<span style="color: black;">体积</span>,若超过<span style="color: black;">咱们</span>的定义slice的<span style="color: black;">体积</span>,会进行切片,分割成多个小的请求去请求后端,到前端就<span style="color: black;">作为</span>一个一个独立的缓存文件

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">优良</span>:<span style="color: black;">每一个</span>子请求收到的数据都会形成独立文件,一个请求中断了,其他请求不受影响,<span style="color: black;">本来</span><span style="color: black;">状况</span>请求中断,再次请求文件将从头<span style="color: black;">起始</span>,而开启分片请求,就接下去获取未请求的小文件</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">劣势:当文件很大<span style="color: black;">或</span>slice很小时,可能会<span style="color: black;">引起</span>文件描述符耗尽等<span style="color: black;">状况</span></span></p>&nbsp;语法:&nbsp;&nbsp; slice size;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#当大文件请求时,设置size为<span style="color: black;">每一个</span>小文件的<span style="color: black;">体积</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">默认:&nbsp;&nbsp; slice 0;</p>&nbsp;位置:&nbsp;&nbsp; http/server/location<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><span style="color: black;">平常</span>问题</span></h3>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><span style="color: black;">1、</span>相同 server_name 多个虚拟主机优先级</span></h2><span style="color: black;">#当<span style="color: black;">显现</span>虚拟主机域名相同的<span style="color: black;">状况</span>,重启nginx时,会<span style="color: black;">显现</span>警告⚠️处理,<span style="color: black;">然则</span>并不不会阻止nginx继续<span style="color: black;">运用</span></span><span style="color: black;">server</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">listen</span>&nbsp;<span style="color: black;">80</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">server_name</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;www.baidu.com</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">...</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">server&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">listen</span>&nbsp;<span style="color: black;">80</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">server_name</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;www.baidu.com</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;...</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">...</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">优先<span style="color: black;">选取</span>最新读取到的配置文件,当多个文件是<span style="color: black;">经过</span>include时,文件排序越靠前,越早被读取</p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><span style="color: black;">2、</span>location 匹配优先级</span></h2>&nbsp;=&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#进行普通字符精确匹配,完全匹配</span>&nbsp;^~&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#进行普通字符匹配,当前<span style="color: black;">暗示</span>前缀匹配</span>~\~*<span style="color: black;">#<span style="color: black;">暗示</span>执行一个正则匹配()</span><span style="color: black;">#当程序<span style="color: black;">运用</span>精确匹配时,一但匹配成功,将停止其他匹配</span><span style="color: black;">#当正则匹配成功时,会继续接下来的匹配,寻找<span style="color: black;">是不是</span>还有更<span style="color: black;">精细</span>的匹配</span>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><span style="color: black;">3、</span>try_files的<span style="color: black;">运用</span></span></h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">按<span style="color: black;">次序</span><span style="color: black;">检测</span>文件<span style="color: black;">是不是</span>存在</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">location&nbsp;/&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;try_files&nbsp;<span style="color: black;">$uri</span>&nbsp;<span style="color: black;">$uri</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/&nbsp;/index.php;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">#先<span style="color: black;">查询</span>$uri下<span style="color: black;">是不是</span>有文件存在,若存在直接返回给用户</span><span style="color: black;">#若$url下<span style="color: black;">无</span>文件存在,再次<span style="color: black;">拜访</span>$uri/的路径<span style="color: black;">是不是</span>有文件存在</span><span style="color: black;">#还是<span style="color: black;">无</span>文件存在,交给index.php处理</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">例:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">location&nbsp;/&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;root&nbsp;/<span style="color: black;">test</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/index.html</p>&nbsp;&nbsp;&nbsp;&nbsp;try_files&nbsp;<span style="color: black;">$uri</span>&nbsp;@<span style="color: black;">test</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>location&nbsp;@<span style="color: black;">test</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;{</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">proxy_pass http://127.0.0.1:9090;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">#<span style="color: black;">拜访</span>&nbsp;/&nbsp;时,查看&nbsp;/test/index.html&nbsp;文件<span style="color: black;">是不是</span>存在</span><span style="color: black;">#若不存在,让9090端口的程序去处理这个请求</span>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><span style="color: black;">4、</span>alias和root的区别</span></h2>location&nbsp;<span style="color: black;">/request_path/image/</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;root&nbsp;<span style="color: black;">/local_path/image/</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">#当<span style="color: black;">咱们</span><span style="color: black;">拜访</span>&nbsp;http://xxx.com/request_path/image/cat.png时</span><span style="color: black;">#将<span style="color: black;">拜访</span>&nbsp;http://xxx.com/request_path/image/local_path/image/cat.png&nbsp;下的文件</span>location&nbsp;<span style="color: black;">/request_path/image/</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;{</p>alias<span style="color: black;">/local_path/image/</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">#当<span style="color: black;">咱们</span><span style="color: black;">拜访</span>&nbsp;http://xxx.com/request_path/image/cat.png时</span><span style="color: black;">#将<span style="color: black;">拜访</span>&nbsp;http://xxx.com/local_path/image/cat.png&nbsp;下的文件</span>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><span style="color: black;">5、</span><span style="color: black;">倘若</span>用户真实IP</span></h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">当一个请求<span style="color: black;">经过</span>多个代理服务器时,用户的IP将会被代理服务器IP覆盖</span></p><span style="color: black;">#在<span style="color: black;">第1</span>个代理服务器中设置</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">set</span>&nbsp;x_real_ip=<span style="color: black;">$remote_addr</span><span style="color: black;">#最后一个代理服务器中获取</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$x_real_ip</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">=IP1</p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><span style="color: black;">6、</span>Nginx <span style="color: black;">平常</span>错误码</span></h2><span style="color: black;">413</span>&nbsp;Request&nbsp;Entity&nbsp;Too&nbsp;Large&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#上传文件过大,设置&nbsp;client_max_body_size</span><span style="color: black;">503</span>&nbsp;bad&nbsp;gateway&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#后端服务无响应</span><span style="color: black;">504</span>&nbsp;Gateway&nbsp;Time-<span style="color: black;">out</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#后端服务执行超时</span><span style="color: black;">来自:SegmentFault,作者:海岛</span><span style="color: black;">链接:https://segmentfault.com/a/1190000014893012</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">扫码关注,不<span style="color: black;">错失</span>任何一个干货</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">-- 前端技术江湖 --</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">点赞是最大的支持</span><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></span></p>




m5k1umn 发表于 2024-11-10 14:37:06

大势所趋,用于讽刺一些制作目的就是为了跟风玩梗,博取眼球的作品。
页: [1]
查看完整版本: 前端必须 - Nginx 负载平衡和缓存实践