前端必须 - 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&tp=webp&wxfrom=5&wx_lazy=1&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;"> status 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 stop 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 <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 -y <span style="color: black;">install</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">gcc gcc-c++ autoconf pcre pcre-devel make automake</p>yum -y <span style="color: black;">install</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> wget httpd-tools 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>指引,将安装 nginx php mysql 可进入lnmp官网查看更<span style="color: black;">仔细</span>的过程</span><span style="color: black;">#默认安装目录/usr/local</span>wget -c http://soft.vpser.net/lnmp/lnmp1.4.tar.gz && tar zxf lnmp1.4.tar.gz &&<span style="color: black;">cd</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> lnmp1.4 && ./install.sh 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;"> /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 <span style="color: black;">#设置nginx服务的系统<span style="color: black;">运用</span>用户</span>worker_processes <span style="color: black;">#工作进程数 <span style="color: black;">通常</span><span style="color: black;">状况</span>与CPU核数保持一致</span>error_log <span style="color: black;">#nginx的错误日志</span>pid <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 {</p> <span style="color: black;">worker_connections</span> <span style="color: black;">#<span style="color: black;">每一个</span>进程<span style="color: black;">准许</span>最大连接数</span> use <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 {</p> <span style="color: black;">...</span> <span style="color: black;">...</span> <span style="color: black;">#后面再<span style="color: black;">仔细</span>介绍 http 配置项目</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> server {</p> listen <span style="color: black;">80</span> <span style="color: black;">#监听端口;</span> server_name localhost <span style="color: black;">#<span style="color: black;">位置</span></span> location / { <span style="color: black;">#<span style="color: black;">拜访</span>首页路径</span> root /xxx/xxx/index.html <span style="color: black;">#默认目录</span>index index.html index.htm<span style="color: black;">#默认文件</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> error_page <span style="color: black;">500</span> <span style="color: black;">504</span> /50x.html <span style="color: black;">#当<span style="color: black;">显现</span>以上状态码时从新定义到50x.html</span> location = /50x.html { <span style="color: black;">#当<span style="color: black;">拜访</span>50x.html时</span> root /xxx/xxx/html <span style="color: black;">#50x.html 页面所在位置</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>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> server {</p> <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;"> }</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;"> {</p> <span style="color: black;">sendfile</span> <span style="color: black;">on</span> <span style="color: black;">#<span style="color: black;">有效</span>传输文件的模式 <span style="color: black;">必定</span>要开启</span> keepalive_timeout <span style="color: black;">65</span> <span style="color: black;">#客户端服务端请求超时时间</span>log_format main XXX<span style="color: black;">#定义日志格式 代号为main</span> access_log /usr/local/access.log main <span style="color: black;">#日志<span style="color: black;">保留</span><span style="color: black;">位置</span> 格式代码 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 -V</p><span style="color: black;"># 查看此配置文件 <span style="color: black;">是不是</span>存在语法错误</span>nginx -tc /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 {</p> sendfile <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;">#在 sendfile 开启的<span style="color: black;">状况</span>下,开启 tcp_nopush <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 {</p> sendfile <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>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">#tcp_nodelay 开启实时传输,传输方式与 tcp_nopush 相反,追求实时性,<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 {</p> sendfile <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> tcp_nodelay <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>的文件压缩传输 (减少文件资源<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 ~ .*\.(gif|jpg)$ {</p> gzip <span style="color: black;">on</span>; <span style="color: black;">#开启</span> gzip_http_version <span style="color: black;">1.1</span>; <span style="color: black;">#服务器传输版本</span>gzip_comp_level<span style="color: black;">2</span>; <span style="color: black;">#压缩比,越高压缩越多,压缩越高可能会消耗服务器性能</span>gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss image/jpeg image/gif image/png;<span style="color: black;">#压缩文件类型</span> root /opt/app/code; <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 ~ load^/download {</p> gzip_static <span style="color: black;">on</span> <span style="color: black;">#开启;</span> 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>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> root /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;">浏览器请求 -> 无缓存 -> 请求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;">呈现</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;">浏览器请求 -> 有缓存 -> 校验本地缓存时间<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;">若过期从新请求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> <span style="color: black;">~ .*\.(html|htm)$</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> {</p> <span style="color: black;">expires</span> <span style="color: black;">12h</span>; <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> ,浏览器<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> <span style="color: black;">~ .*\.(html|htm)$</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> {</p> <span style="color: black;">add_header</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> Access-Control-Allow-Origin *;</p> <span style="color: black;">add_header</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS;</p> <span style="color: black;">#Access-Control-Allow-Credentials true #<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>跨域内容可参考 Laravel 跨域功能中间件 <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> <span style="color: black;">#<span style="color: black;">暗示</span>当前请求上一次页面<span style="color: black;">拜访</span>的<span style="color: black;">位置</span>,换句话说,<span style="color: black;">拜访</span> www.baidu.com 主页,这是<span style="color: black;">第1</span>次<span style="color: black;">拜访</span>,<span style="color: black;">因此</span> $http_referer 为空,<span style="color: black;">然则</span> <span style="color: black;">拜访</span>此页面的时候还需要获取一张首页<span style="color: black;">照片</span>,再请求这张<span style="color: black;">照片</span>的时候 $http_referer 就为 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> <span style="color: black;">~ .*\.(jpg|gif)$</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> {</p> <span style="color: black;">#valid_referers <span style="color: black;">暗示</span><span style="color: black;">咱们</span><span style="color: black;">准许</span><span style="color: black;">那些</span> $http_referer 来<span style="color: black;">拜访</span></span> <span style="color: black;">#none <span style="color: black;">暗示</span><span style="color: black;">无</span>带 $http_referer,如<span style="color: black;">第1</span>次<span style="color: black;">拜访</span>时 $http_referer 为空</span> <span style="color: black;">#blocked <span style="color: black;">暗示</span> $http_referer 不是标准的<span style="color: black;">位置</span>,非正常域名等</span> <span style="color: black;">#只<span style="color: black;">准许</span>此ip</span> <span style="color: black;">valid_referers</span> <span style="color: black;">none</span> <span style="color: black;">blocked</span> <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> if (<span style="color: black;">$invalid_referer</span>) { <span style="color: black;">#不满足<span style="color: black;">状况</span>下变量值为1</span> <span style="color: black;">return</span> <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;"> }</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 {</p> listen <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;"> location / {</p> proxy_pass http:<span style="color: black;">//127.0.0.1:8080/;</span> proxy_redirect <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 Host $http_host;</p> proxy_set_header X-Real-IP $remote_addr; <span style="color: black;">#获取客户端真实IP</span> proxy_connect_timeout <span style="color: black;">30</span>; <span style="color: black;">#超时时间</span> proxy_send_timeout <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 <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 <span style="color: black;">32</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">k;</p> proxy_buffering on; <span style="color: black;">#开启缓冲区,减少磁盘io</span> proxy_buffers <span style="color: black;">4</span> <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> proxy_max_temp_file_size <span style="color: black;">256</span>k; <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;"> }</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> 。将客户的请求<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>为一个 <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 <span style="color: black;">#自定义组名 {</span> server x1.baidu.com; <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 x2.baidu.com;</p> <span style="color: black;">#server x3.baidu.com</span> <span style="color: black;">#down 不参与负载<span style="color: black;">平衡</span></span> <span style="color: black;">#weight=5; 权重,越高分配越多</span> <span style="color: black;">#backup; 预留的备份服务器</span> <span style="color: black;">#max_fails <span style="color: black;">准许</span>失败的次数</span> <span style="color: black;">#fail_timeout 超过失败次数后,服务暂停时间</span> <span style="color: black;">#max_coons 限制最大的接受的连接数</span> <span style="color: black;">#<span style="color: black;">按照</span>服务器性能<span style="color: black;">区别</span>,配置适合的参数</span> <span style="color: black;">#server 106.xx.xx.xxx; <span style="color: black;">能够</span>是ip</span> <span style="color: black;">#server 106.xx.xx.<span style="color: black;">xxx:</span>8080; <span style="color: black;">能够</span>带端口号</span> <span style="color: black;">#server unix:/tmp/xxx; 支出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;">新建文件 </span><span style="color: black;">proxy.conf</span><span style="color: black;">,内容如下,上一章介绍的反向代理配置</span></p>proxy_redirect <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 Host $http_host;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">proxy_set_header X-Real-IP $remote_addr;</p>proxy_connect_timeout <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 <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 <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 <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 on;</p>proxy_buffers <span style="color: black;">4</span> <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>proxy_max_temp_file_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><span style="color: black;">#服务器A的配置</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">http {</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;"> upstream xxx {</p> server <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> server <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;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> server {</p> liseten <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;"> server_name localhost;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> location / {</p> proxy_pass http:<span style="color: black;">//xxx #upstream 对应自定义名<span style="color: black;">叫作</span></span> <span style="color: black;">include</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> proxy.conf;</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;">}</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 {</p> liseten <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;"> server_name localhost;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> location / {</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> index index.html</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;"><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;"> 配置</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> upstream xxx {</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> ip_hash;</p> <span style="color: black;">server</span> <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> <span style="color: black;">server</span> <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;"> }</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;"> 配置</span></p> <span style="color: black;">upstream</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> xxx {</p> <span style="color: black;">hash</span> <span style="color: black;">$request_uri</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p> <span style="color: black;">server</span> <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> <span style="color: black;">server</span> <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;"> }</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 {</p> proxy_cache_path /var/www/cache <span style="color: black;">#缓存<span style="color: black;">位置</span></span> levels=1:2 <span style="color: black;">#目录分级</span> keys_zone=test_cache:10m <span style="color: black;">#开启的keys空间名字:空间<span style="color: black;">体积</span>(1m<span style="color: black;">能够</span>存放8000个key)</span> max_size=10g <span style="color: black;">#目录最大<span style="color: black;">体积</span>(超过时,不常用的将被删除)</span> inactive=60m <span style="color: black;">#60分钟内<span style="color: black;">无</span>被<span style="color: black;">拜访</span>的缓存将清理</span> use_temp_path=pff; <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;"> server {</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;"> location / {</p>proxy_cache test_cache;<span style="color: black;">#开启缓存对应的名<span style="color: black;">叫作</span>,在keys_zone命名好</span> proxy_cache_valid 200 304 12h; <span style="color: black;">#状态码为200 304的缓存12小时</span> proxy_cache_valid any 10m; <span style="color: black;">#其他状态缓存10小时</span> proxy_cache_key <span style="color: black;">$host$uri$is_args$args</span>; <span style="color: black;">#设置key值</span> add_header Nginx-Cache <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;"> }</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 style="color: black;">咱们</span>不需要缓存的时候,在上面配置的内容中加入以下配置</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">server {</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> ...</p> <span style="color: black;">if</span> (<span style="color: black;">$request_uri</span> ~ ^/(login|register) ) { <span style="color: black;">#当请求<span style="color: black;">位置</span>有login或register时</span> <span style="color: black;">set</span> <span style="color: black;">$nocache</span> = 1; <span style="color: black;">#设置一个自定义变量为true</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 / {</p> proxy_no_cache <span style="color: black;">$nocache</span> <span style="color: black;">$arg_nocache</span> <span style="color: black;">$arg_comment</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p> proxy_no_cache <span style="color: black;">$http_pragma</span> <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;"> }</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> 语法: slice size; <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;">默认: slice 0;</p> 位置: 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;"> {</p> <span style="color: black;">listen</span> <span style="color: black;">80</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p> <span style="color: black;">server_name</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 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 {</p> <span style="color: black;">listen</span> <span style="color: black;">80</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p> <span style="color: black;">server_name</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 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;">...</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> = <span style="color: black;">#进行普通字符精确匹配,完全匹配</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><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 / {</p> try_files <span style="color: black;">$uri</span> <span style="color: black;">$uri</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/ /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 / {</p> root /<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> try_files <span style="color: black;">$uri</span> @<span style="color: black;">test</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>location @<span style="color: black;">test</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_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> / 时,查看 /test/index.html 文件<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 <span style="color: black;">/request_path/image/</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> {</p> root <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> http://xxx.com/request_path/image/cat.png时</span><span style="color: black;">#将<span style="color: black;">拜访</span> http://xxx.com/request_path/image/local_path/image/cat.png 下的文件</span>location <span style="color: black;">/request_path/image/</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> {</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> http://xxx.com/request_path/image/cat.png时</span><span style="color: black;">#将<span style="color: black;">拜访</span> http://xxx.com/local_path/image/cat.png 下的文件</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> <span style="color: black;">set</span> x_real_ip=<span style="color: black;">$remote_addr</span><span style="color: black;">#最后一个代理服务器中获取</span> <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> Request Entity Too Large <span style="color: black;">#上传文件过大,设置 client_max_body_size</span><span style="color: black;">503</span> bad gateway <span style="color: black;">#后端服务无响应</span><span style="color: black;">504</span> Gateway Time-<span style="color: black;">out</span> <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>
大势所趋,用于讽刺一些制作目的就是为了跟风玩梗,博取眼球的作品。 软文发布平台 http://www.fok120.com/
页:
[1]