首页
导航
博客
电子书
算法
众创
代码
随贴
关于我们
您好,欢迎来到码863代码分享网! 请
[登录]
/
[注册]
搜 索
标题:
*
140
字
TAG标签:
(用空格隔开)
30
字
恢复历史版本:
请选择分类
html
python
javascript
php
sql
c
c++
c#
java
plain
所有人可见
仅自己可见
编辑器:UEditor
编辑器:TinyMCE
编辑器:Editor.md
HTML转MD
HTML转MD2
<p><a href="https://www.cnblogs.com/lemon-le/p/7804347.html">https://www.cnblogs.com/lemon-le/p/7804347.html</a></p><p><a href="https://www.cnblogs.com/yyxianren/p/10832172.html" _src="https://www.cnblogs.com/yyxianren/p/10832172.html">https://www.cnblogs.com/yyxianren/p/10832172.html</a> </p><p><a href="http://www.jackieathome.net/archives/411.html">http://www.jackieathome.net/archives/411.html</a></p><p><br/></p><p>静态思路,用proxy_cache还是每个请求但可以php加etag,proxy_cache会缓冲etag,这样只匹配后返回304 第二种用proxy_store,然后在用户请求中做删除,用定时器删除太耗资源</p><p><br/></p><p>关键配置<br/></p><p>proxy_headers_hash_max_size 51200;</p><p>proxy_headers_hash_bucket_size 6400;</p><p>proxy_cache_path /home/wwwroot/hjzfx.ma863.net/nginx_cache levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=30g; <span style="white-space: pre;"></span></p><p>location ~ .*\.html$</p><p>{</p><p> <span style="white-space:pre"></span>#expires 5s;</p><p> <span style="white-space:pre"></span>#proxy_store on;</p><p> <span style="white-space:pre"></span>#proxy_store_access user:rw group:rw all:r;</p><p> <span style="white-space:pre"></span>#proxy_temp_path /home/wwwroot/hjzfx.ma863.net/nginx;</p><p> <span style="white-space:pre"></span>#if ( !-e $request_filename) {</p><p> <span style="white-space:pre"></span>#<span style="white-space:pre"></span>proxy_pass http://127.0.0.1:88;</p><p> <span style="white-space:pre"></span>#}</p><p> <span style="white-space:pre"></span>proxy_cache_revalidate on;</p><p> <span style="white-space:pre"></span>proxy_cache cache_one;</p><p> <span style="white-space:pre"></span>proxy_cache_valid 200 304 302 5y;<span style="white-space:pre"></span>#根据响应码设置缓存时间,超过这个时间即使缓存文件中有缓存数据,nginx也会回源请求新数据。</p><p> <span style="white-space:pre"></span>proxy_pass http://127.0.0.1:88;</p><p> <span style="white-space:pre"></span>proxy_cache_lock on;<span style="white-space:pre"></span>#缓存并发锁,当nginx缓存没有命中的时候只有一个请求回源tomcat请求数据,其他请求会等待。非必选配置</p><p> <span style="white-space:pre"></span>proxy_cache_lock_timeout 1s;<span style="white-space:pre"></span>#等待锁超时时间设置 非必选配置</p><p> <span style="white-space:pre"></span>proxy_ignore_headers Set-Cookie Cache-Control Expires;</p><p> <span style="white-space:pre"></span>proxy_hide_header Cache-Control;</p><p> <span style="white-space:pre"></span>proxy_hide_header Set-Cookie;</p><p> <span style="white-space:pre"></span>proxy_hide_header Expires;</p><p> <span style="white-space:pre"></span>proxy_hide_header Pragma;</p><p> <span style="white-space:pre"></span>proxy_hide_header Connection;</p><p> <span style="white-space:pre"></span>proxy_hide_header Referrer-Policy;</p><p> <span style="white-space:pre"></span>proxy_hide_header X-Content-Type-Options;</p><p> <span style="white-space:pre"></span>proxy_hide_header X-XSS-Protection;</p><p> <span style="white-space:pre"></span>#add_header X-Cache '$upstream_cache_status from $server_addr';</p><p> <span style="white-space:pre"></span>#add_header Last-Modified 'Tue, 20 Nov 2019 11:28:46 GMT';</p><p> <span style="white-space:pre"></span>add_header X-Cache '$upstream_cache_status';</p><p> <span style="white-space:pre"></span>#expires 5y;</p><p> <span style="white-space:pre"></span>#add_header Cache-Control "max-age=60,public";</p><p> <span style="white-space:pre"></span>include proxy.conf;</p><p>}</p><p><br/></p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"><span style="font-size: 16px; color: rgb(255, 0, 0);"><strong>实现的需求图:</strong></span></p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"><span style="font-size: 15px; color: rgb(255, 0, 0);"><strong><img src="/ueditor/php/upload/image/20191126/1574768951589905.png" alt="" style="border: 0px; max-width: 900px; height: auto;"/></strong></span></p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"> </p><p> </p><p>环境:</p><p>nginx缓存和反向代理服务器:192.168.0.224</p><p>实际存储数据机器:192.168.0.37</p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"> </p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"><span style="font-size: 16px; color: rgb(255, 0, 0);"><strong><span data-wiz-span="data-wiz-span">一、实现反向代理</span></strong></span></p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"><span style="color: rgb(255, 102, 0);"><strong>1、安装nginx,两台服务器都需要安装</strong></span></p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);">1)安装依赖包</p><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; overflow-wrap: break-word; font-family: " courier="">yum -y install gcc make cmake ncurses-devel libxml2-devel libtool-ltdl-devel gcc-c++ autoconf automake bison zlib-devel pcre-devel openssl openssl-devel</pre><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"> </p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);">2)下载nginx</p><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; overflow-wrap: break-word; font-family: " courier="">wget http://nginx.org/download/nginx-1.8.0.tar.gz</pre><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"> </p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);">3)解压编译</p><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; overflow-wrap: break-word; font-family: " courier="">tar -zxvf nginx-1.8.0.tar.gz ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx makemake install</pre><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"> </p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"><span style="color: rgb(255, 102, 0);"><strong>2、修改配置文件</strong></span></p><p><span class="pln">这里只实现他的功能,并没有对配置文件做更多的修改</span></p><p> </p><p>1)代理服务器:vim /usr/local/nginx/conf/nginx.conf</p><p><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="color: rgb(29, 88, 209); border: none !important;"><img src="/ueditor/php/upload/image/20191126/1574768951551598.gif" alt="复制代码" style="max-width: 900px; height: auto; border: none !important;"/></a></span></p><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; overflow-wrap: break-word; font-family: " courier="">worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; } location / { proxy_pass http://192.168.0.37/; } }</pre><p><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="color: rgb(29, 88, 209); border: none !important;"><img src="/ueditor/php/upload/image/20191126/1574768951551598.gif" alt="复制代码" style="max-width: 900px; height: auto; border: none !important;"/></a></span></p><p> </p><p>配置文件分析:</p><p>proxy_pass指令:用来设置被代理服务器的地址,可以是主机名称,IP地址加端口号等形式</p><p style="margin: 10px auto;"> </p><p style="margin: 10px auto;">2)真实数据服务器:改路径即可vim /usr/local/nginx/conf/nginx.conf</p><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; overflow-wrap: break-word; font-family: " courier="">location /{ root /data; index index.html index.php; }</pre><p>3)创建对应的文件夹</p><p> </p><p>在192.168.0.37真实数据机器上:</p><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; overflow-wrap: break-word; font-family: " courier="">mkdir /dataecho "I am realy server" >/data/index.html</pre><p style="margin: 10px auto;"> </p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"><span style="color: rgb(255, 102, 0);"><strong>3、两台机器上启动nginx</strong></span></p><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; overflow-wrap: break-word; font-family: " courier=""> /usr/local/nginx/sbin/nginx</pre><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"> </p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"><span style="color: rgb(255, 102, 0);"><strong>4、测试</strong></span></p><p>这里输入访问地址,可以看到他去真是的数据机器上取到了数据," location / "表示的是匹配到的任何内容,都去代理服务器上取,若要一部分在代理服务器上,另一部分在真实机器上取,可以根据自己的需求修改配置文件</p><p> </p><p><img src="/ueditor/php/upload/image/20191126/1574768951499557.png" alt="" style="border: 0px; max-width: 900px; height: auto;"/></p><p> </p><p><br/></p><p style="margin: 10px auto;"><span style="font-size: 16px; color: rgb(255, 0, 0);"><strong><span data-wiz-span="data-wiz-span">二、缓存机制</span></strong></span></p><p><span style="color: rgb(0, 128, 0); font-size: 15px;"> <strong><span data-wiz-span="data-wiz-span">基于proxy_cache的缓存</span></strong></span></p><p> </p><p><strong><span style="color: rgb(255, 102, 0);"> </span></strong><span data-wiz-span="data-wiz-span"><strong><span style="color: rgb(255, 102, 0);">1:</span></strong>在反向代理的基础上,打开缓存机制,这样可以加快访问速度,只是添加几行配置,能实现缓存这个功能,其他具体的优化,安全之类的还没有仔细研究,先搭起来理解看怎么回事再细细研究其他的</span></p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"> </p><p><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="color: rgb(29, 88, 209); border: none !important;"><img src="/ueditor/php/upload/image/20191126/1574768951551598.gif" alt="复制代码" style="max-width: 900px; height: auto; border: none !important;"/></a></span></p><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; overflow-wrap: break-word; font-family: " courier="">worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; proxy_cache_path /usr/local/nginx/cache levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=30g; server { listen 80; server_name localhost; } location / { proxy_cache cache_one; proxy_cache_valid 200 304 302 24h; proxy_pass http://192.168.0.37/; } }</pre><p><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="color: rgb(29, 88, 209); border: none !important;"><img src="/ueditor/php/upload/image/20191126/1574768951551598.gif" alt="复制代码" style="max-width: 900px; height: auto; border: none !important;"/></a></span></p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"> </p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"><span data-wiz-span="data-wiz-span"><strong><span style="color: rgb(255, 102, 0);">2:</span></strong>查看他的进程多了缓存</span></p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"><span data-wiz-span="data-wiz-span"><img src="/ueditor/php/upload/image/20191126/1574768952287304.png" alt="" style="border: 0px; max-width: 900px; height: auto;"/></span></p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"> </p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"> </p><p><span data-wiz-span="data-wiz-span"><strong><span style="color: rgb(255, 102, 0);">3:</span></strong>配置文件主要添加了三行:</span></p><p><span data-wiz-span="data-wiz-span"><span style="color: rgb(255, 102, 0);"><strong>proxy_cache_path /usr/local/nginx/cache levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=30g;</strong></span><br/></span></p><p> </p><p>proxy_cache_path:表示缓存文件存放的路径,该路径是预先就要创建好的</p><p>levels=1:2 : 设置在相对于path指定目录的第几级hash目录中缓存数据,levels=1,表示一级hash目录,levels=1:2,表示两级hash目录,目录的名称是基于请求URL通过哈希算法得到的</p><p>keys_zone=cache_one:500m : 设置缓存名字</p><p>inactive=1d : 强制更新时间,在指定时间内没人访问,就删除缓存</p><p>max_size=30g : 设置硬盘中缓存数据的大小限制,最大缓存空间</p><p> </p><p><span style="color: rgb(255, 102, 0);"><strong>proxy_cache cache_one;</strong></span></p><p>表示这个server使用上面设置的那个cache</p><p> </p><p><span data-wiz-span="data-wiz-span"><span style="color: rgb(255, 102, 0);"><strong>proxy_cache_valid 200 304 302 24h;</strong></span><br/></span></p><p>针对不同的HTTP响应状态设置不同的缓存时间,如果不设置,nginx服务器只为HTTP状态代码为200,301和302的响应数据做缓存</p><p>使用any可以表示缓存所有该指令中未设置的其他响应数据,如:</p><p>proxy_cache_valid 200 302 10m;</p><p>proxy_cache_valid 301 1h;</p><p>proxy_cache_valid any 1m;</p><p>表示的是对返回状态为200,302的响应数据缓存10分钟,对返回状态码为301的响应数据缓存1小时,非200,302,301返回状态的响应数据缓存1分钟</p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"> </p><p><span data-wiz-span="data-wiz-span"><span style="color: rgb(255, 102, 0);"><strong>4:</strong></span>测试:</span></p><p> </p><p>1)当你第一次访问目录时,得到的结果是,如下:</p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"> <img src="/ueditor/php/upload/image/20191126/1574768952302068.png" alt="" style="border: 0px; max-width: 900px; height: auto;"/></p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"> </p><p>2)可以进入到缓存目录看一下它的目录结构: </p><p>目录的名称是基于请求URL通过哈希算法获取到的</p><p>/usr/local/nginx/cache<span data-wiz-span="data-wiz-span"><strong>/<span style="color: rgb(255, 0, 0);">d/91</span></strong>/972fbe600d30f1cc92495981969ff<span data-wiz-span="data-wiz-span" style="color: rgb(255, 0, 0);"><strong>91d</strong></span></span></p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"><img src="/ueditor/php/upload/image/20191126/1574768952771984.png" alt="" style="border: 0px; max-width: 900px; height: auto;"/></p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"> </p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);">查看一下具体的内容:</p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"><img src="/ueditor/php/upload/image/20191126/1574768952856386.png" alt="" style="border: 0px; max-width: 900px; height: auto;"/></p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"> </p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);">3)然后把真实数据服务器的nginx给停掉,再一次访问,还是可以得到</p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"><img src="/ueditor/php/upload/image/20191126/1574768952943208.png" alt="" style="border: 0px; max-width: 900px; height: auto;"/></p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"> </p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);">4)然后把代理服务器的缓存数据给删除,再一次访问,得到结果:说明从缓存没有找到,然后代理转发到真是的服务器也没找到,这就说明了第二次访问的是缓存数据的内容</p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"><img src="/ueditor/php/upload/image/20191126/1574768952257392.png" alt="" style="border: 0px; max-width: 900px; height: auto;"/></p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"> </p><p><span data-wiz-span="data-wiz-span"><span style="color: rgb(255, 102, 0);"><strong>5)使用tcpdump抓包</strong></span>看看他的走向:</span></p><p>在客户端192.168.0.222 访问192.168.0.224,可以elinks 192.168.0..224</p><p>在代理服务器这里使用tcpdump抓包,可以看到他确实是从192.168.0.37去取得数据</p><p><span style="color: rgb(0, 128, 0);"><strong>tcpdump -i eno16777736 host 192.168.0.224 and 192.168.0.37 and tcp port 80</strong></span></p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"><img src="/ueditor/php/upload/image/20191126/1574768953421196.png" alt="" style="border: 0px; max-width: 900px; height: auto;"/></p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"> </p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);">第二次再去访问,还是监听这个80端口,可以发现tcpdump没有任何数据,说明它直接从缓存里读取了,你可以把缓存删了,再次在客户端访问,可以看到它又有数据了</p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"> </p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"><span style="color: rgb(0, 128, 0); font-size: 15px;"><strong><span data-wiz-span="data-wiz-span">基于proxy_store的缓存</span></strong></span></p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);">1:修改反向代理的配置文件</p><p><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="color: rgb(29, 88, 209); border: none !important;"><img src="/ueditor/php/upload/image/20191126/1574768951551598.gif" alt="复制代码" style="max-width: 900px; height: auto; border: none !important;"/></a></span></p><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; overflow-wrap: break-word; font-family: " courier="">worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; } location / { root "/web1"; proxy_store on; proxy_store_access user:rw group:rw all:r; proxy_temp_path "/web1"; if ( !-f $request_filename ) { proxy_pass http://192.168.0.37; } } }</pre><p><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="color: rgb(29, 88, 209); border: none !important;"><img src="/ueditor/php/upload/image/20191126/1574768951551598.gif" alt="复制代码" style="max-width: 900px; height: auto; border: none !important;"/></a></span></p><p> </p><p>通过location的 if 条件判断驱动nginx代理服务器与后端服务器通信和web缓存;判断请求的资源在nginx代理服务器上是否存在,如果不存在就通过后端服务器获取数据,然后回传给客户端,同时使用proxy store进行缓存。</p><p> </p><p>用户一访问的时候,主目录为/web1,如果里面没有用户需要的数据,就去代理取,然后缓存到自己的主目录,下次再访问同样的资源的时候,就能在缓存这里得到数据</p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);">创建文件夹:</p><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; overflow-wrap: break-word; font-family: " courier="">mkdir /web1</pre><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"> </p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);">2:真实数据服务器的配置文件</p><p><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="color: rgb(29, 88, 209); border: none !important;"><img src="/ueditor/php/upload/image/20191126/1574768951551598.gif" alt="复制代码" style="max-width: 900px; height: auto; border: none !important;"/></a></span></p><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; overflow-wrap: break-word; font-family: " courier="">worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; } root /data; }</pre><p><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="color: rgb(29, 88, 209); border: none !important;"><img src="/ueditor/php/upload/image/20191126/1574768951551598.gif" alt="复制代码" style="max-width: 900px; height: auto; border: none !important;"/></a></span></p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"> </p><p>3:测试</p><p> </p><p>访问真实机器的数据目录/data下的/hah/a.html</p><p><img src="/ueditor/php/upload/image/20191126/1574768954797315.png" alt="" style="border: 0px; max-width: 900px; height: auto;"/></p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"> </p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);">在代理服务器的缓存目录下可以看到缓存到的数据和真实数据服务器的目录结构一样:(这个与proxy cache不一样)</p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"><img src="/ueditor/php/upload/image/20191126/1574768954104287.png" alt="" style="border: 0px; max-width: 900px; height: auto;"/></p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"> </p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);">把代理缓存的a.html改一下:</p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"><img src="/ueditor/php/upload/image/20191126/1574768954995533.png" alt="" style="border: 0px; max-width: 900px; height: auto;"/></p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"> </p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);">再次访问的时候,返回的数据如下,说明它第二次访问的是缓存</p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"><img src="/ueditor/php/upload/image/20191126/1574768954213892.png" alt="" style="border: 0px; max-width: 900px; height: auto;"/></p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"> </p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);">proxy store 与proxy cache的区别:proxy store不提供缓存过期更新,内存索引建立等功能,缓存文件一直会保存在本地磁盘中。</p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"><br/></p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"><br/></p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"><br/></p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"><br/></p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"><br/></p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"><br/></p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"><br/></p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"><br/></p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"><br/></p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"><br/></p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);">https://www.cnblogs.com/yyxianren/p/10832172.html</p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"><br/></p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"><br/></p><p style="margin: 10px auto; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);"><br/></p><p style="margin: 10px auto; padding: 0px; text-align: justify; font-family: " pingfang="" helvetica="" microsoft="" font-size:="" white-space:="" background-color:="">proxy_cache将从C上获取到的数据根据预设规则存放到B上(内存+磁盘)留着备用,<br style="margin: 0px; padding: 0px;"/>A请求B时,B会把缓存的这些数据直接给A,而不需要再去向C去获取。</p><p style="margin: 10px auto; padding: 0px; text-align: justify; font-family: " pingfang="" helvetica="" microsoft="" font-size:="" white-space:="" background-color:="">proxy_cache相关功能生效的前提是,需要设置proxy_buffering on;</p><p style="margin: 10px auto; padding: 0px; text-align: justify; font-family: " pingfang="" helvetica="" microsoft="" font-size:="" white-space:="" background-color:=""><strong style="margin: 0px; padding: 0px;">proxy_cache主要参数:</strong></p><p><span class="cnblogs_code_copy" style="margin: 0px; padding: 0px 5px 0px 0px; line-height: 1.5 !important;"><a title="复制代码" style="margin: 0px; padding: 0px; text-decoration-line: underline; border: none !important;"><img src="/ueditor/php/upload/image/20191126/1574776473982412.gif" alt="复制代码" style="margin: 0px; padding: 0px; max-width: 660px; height: auto; width: auto; max-height: 100%; border: none !important;"/></a></span></p><pre style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: " courier="">|.66版本开始,proxy_cache机制开启后会检测被代理端的HTTP响应头中的、-Control为no-=levels] keys_zone=name:size [inactive=] [max_size==:,表示有两级子目录,第一个目录名取md5值的倒数第一个值,第二个目录名取md5值的第2和3个值。 keys_zone设置内存zone的名字和大小,如keys_zone=my_zone:10m inactive设置缓存多长时间就失效,当硬盘上的缓存数据在该时间段内没有被访问过,就会失效了,该数据就会被删除,默认为10s。 max_size设置硬盘中最多可以缓存多少数据,当到达该数值时,nginx会删除最少访问的数据。</pre><p><span class="cnblogs_code_copy" style="margin: 0px; padding: 0px 5px 0px 0px; line-height: 1.5 !important;"><a title="复制代码" style="margin: 0px; padding: 0px; text-decoration-line: underline; border: none !important;"><img src="/ueditor/php/upload/image/20191126/1574776473982412.gif" alt="复制代码" style="margin: 0px; padding: 0px; max-width: 660px; height: auto; width: auto; max-height: 100%; border: none !important;"/></a></span></p><p style="margin: 10px auto; padding: 0px; text-align: justify; font-family: " pingfang="" helvetica="" microsoft="" font-size:="" white-space:="" background-color:=""> <strong style="margin: 0px; padding: 0px;">proxy_cache示例:</strong></p><p><span class="cnblogs_code_copy" style="margin: 0px; padding: 0px 5px 0px 0px; line-height: 1.5 !important;"><a title="复制代码" style="margin: 0px; padding: 0px; text-decoration-line: underline; border: none !important;"><img src="/ueditor/php/upload/image/20191126/1574776473982412.gif" alt="复制代码" style="margin: 0px; padding: 0px; max-width: 660px; height: auto; width: auto; max-height: 100%; border: none !important;"/></a></span></p><pre style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: " courier="">http { ...; proxy_cache_path /data/nginx_cache/ levels=1:2 keys_zone=my_zone:10m inactive=300s max_size=5g; ...; server { listen 80; server_name www.aminglinux.com; proxy_buffering on; proxy_buffer_size 4k; proxy_buffers 2 4k; proxy_busy_buffers_size 4k; proxy_temp_path /tmp/nginx_proxy_tmp 1 2; proxy_max_temp_file_size 20M; proxy_temp_file_write_size 8k; location / { proxy_cache my_zone; proxy_pass http://192.168.10.110:8080/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } }</pre><p><span class="cnblogs_code_copy" style="margin: 0px; padding: 0px 5px 0px 0px; line-height: 1.5 !important;"><a title="复制代码" style="margin: 0px; padding: 0px; text-decoration-line: underline; border: none !important;"><img src="/ueditor/php/upload/image/20191126/1574776473982412.gif" alt="复制代码" style="margin: 0px; padding: 0px; max-width: 660px; height: auto; width: auto; max-height: 100%; border: none !important;"/></a></span></p><p style="margin: 10px auto; padding: 0px; text-align: justify; font-family: " pingfang="" helvetica="" microsoft="" font-size:="" white-space:="" background-color:="">说明:核心配置为proxy_cache_path那一行。</p><p><br/></p><p><br/></p><p><br/></p><p><br/></p><p><br/></p><p><a href="http://www.jackieathome.net/archives/411.html">http://www.jackieathome.net/archives/411.html</a></p><p style="background: rgb(255, 255, 255); border: 0px; margin-top: 0px; margin-bottom: 24px; padding: 0px; vertical-align: baseline; color: rgb(51, 51, 51); font-family: Georgia, " bitstream="" white-space:="">nginx的版本</p><pre 10="" style="background-color: rgb(247, 247, 247); border: 0px; margin-top: 0px; margin-bottom: 24px; padding: 1.5em; vertical-align: baseline; font-family: " courier="" color:="" line-height:="" overflow:="" text-size-adjust:="" font-size:="">./nginx -v nginx version: nginx/1.9.15</pre><h2 style="background: rgb(255, 255, 255); border: 0px; margin: 0px 0px 20px; padding: 0px; vertical-align: baseline; clear: both; font-weight: normal; line-height: 1.5em; font-family: Georgia, " bitstream="" white-space:="">需求</h2><p style="background: rgb(255, 255, 255); border: 0px; margin-top: 0px; margin-bottom: 24px; padding: 0px; vertical-align: baseline; color: rgb(51, 51, 51); font-family: Georgia, " bitstream="" white-space:="">要求nginx缓存静态资源,如js、css、图片等,避免对静态资源的访问直接穿透到后端的j2ee应用侧,提高后端j2ee应用的运行效率。</p><h2 style="background: rgb(255, 255, 255); border: 0px; margin: 0px 0px 20px; padding: 0px; vertical-align: baseline; clear: both; font-weight: normal; line-height: 1.5em; font-family: Georgia, " bitstream="" white-space:="">配置方法</h2><p style="background: rgb(255, 255, 255); border: 0px; margin-top: 0px; margin-bottom: 24px; padding: 0px; vertical-align: baseline; color: rgb(51, 51, 51); font-family: Georgia, " bitstream="" white-space:="">针对js、css、图片文件</p><ul style="list-style-type: square;" class=" list-paddingleft-2"><li><p>分别增加缓存路径的指令,比如对于图片,增加如下的指令:</p><pre 10="" style="background-color: rgb(247, 247, 247); border: 0px; margin-top: 0px; margin-bottom: 24px; padding: 1.5em; vertical-align: baseline; font-family: " courier="" color:="" line-height:="" overflow:="" text-size-adjust:="" font-size:="">proxy_cache_path proxy_cache_image levels=1:2 keys_zone=cache_image:100m;</pre></li><li><p>分别增加路径转发配置的指令,比如对于图片,增加如下的指令:</p><pre 10="" style="background-color: rgb(247, 247, 247); border: 0px; margin-top: 0px; margin-bottom: 24px; padding: 1.5em; vertical-align: baseline; font-family: " courier="" color:="" line-height:="" overflow:="" text-size-adjust:="" font-size:="">location ~ \.(gif|jpg|jpeg|png)$ { proxy_cache cache_image; proxy_cache_key $uri$is_args$args; proxy_cache_valid 200 206 14d; proxy_pass http://127.0.0.1:18080; }</pre></li><li><p>增加如下指令,在HTTP响应消息的头部中增加资源请求命中的标识。</p><pre 10="" style="background-color: rgb(247, 247, 247); border: 0px; margin-top: 0px; margin-bottom: 24px; padding: 1.5em; vertical-align: baseline; font-family: " courier="" color:="" line-height:="" overflow:="" text-size-adjust:="" font-size:="">add_header X-Cache $upstream_cache_status; # $upstream_cache_status表示资源缓存的状态,可选值有HIT MISS EXPIRED三种状态</pre><p style="background: transparent; border: 0px; margin-top: 0px; margin-bottom: 24px; padding: 0px; vertical-align: baseline;">使用浏览器调试面板中的网络视图,查看浏览器收到的响应消息的头部,如下所示</p><pre 10="" style="background-color: rgb(247, 247, 247); border: 0px; margin-top: 0px; margin-bottom: 24px; padding: 1.5em; vertical-align: baseline; font-family: " courier="" color:="" line-height:="" overflow:="" text-size-adjust:="" font-size:="">Connection:keep-alive Content-Encoding:gzip Content-Type:text/css Date:Sat, 07 Jan 2017 12:00:37 GMT ETag:W/"5576-1457782914000"Last-Modified:Sat, 12 Mar 2016 11:41:54 GMT Server:nginx Transfer-Encoding:chunked Vary:Accept-Encoding X-Cache:HIT # 表明缓存命中</pre></li></ul><h2 style="background: rgb(255, 255, 255); border: 0px; margin: 0px 0px 20px; padding: 0px; vertical-align: baseline; clear: both; font-weight: normal; line-height: 1.5em; font-family: Georgia, " bitstream="" white-space:="">完整的样例配置</h2><pre 10="" style="background-color: rgb(247, 247, 247); border: 0px; margin-top: 0px; margin-bottom: 24px; padding: 1.5em; vertical-align: baseline; font-family: " courier="" color:="" line-height:="" overflow:="" text-size-adjust:="" font-size:="">worker_processes 1;error_log logs/error.log;events { worker_connections 1024; }http { include mime.types; default_type application/octet-stream; server_tokens off; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; sendfile on; tcp_nopush on; keepalive_timeout 65; gzip on; gzip_vary on; gzip_min_length 1024; gzip_buffers 128 32k; gzip_comp_level 6; gzip_http_version 1.1; gzip_proxied expired no-cache no-store private auth; gzip_types text/plain text/css text/xml application/xml application/json text/javascript application/javascript application/x-javascript; charset utf-8; proxy_cache_path proxy_cache_image levels=1:2 keys_zone=cache_image:100m; proxy_cache_path proxy_cache_js levels=1:2 keys_zone=cache_js:100m; proxy_cache_path proxy_cache_css levels=1:2 keys_zone=cache_css:100m; server { listen 8080; server_name 192.168.0.107; location ~ \.js { proxy_cache cache_js; proxy_cache_key $uri$is_args$args; proxy_cache_valid 200 206 14d; proxy_pass http://127.0.0.1:18080; } location ~ \.css { proxy_cache cache_css; proxy_cache_key $uri$is_args$args; proxy_cache_valid 200 206 14d; proxy_pass http://127.0.0.1:18080; } location ~ \.(gif|jpg|jpeg|png)$ { proxy_cache cache_image; proxy_cache_key $uri$is_args$args; proxy_cache_valid 200 206 14d; proxy_pass http://127.0.0.1:18080; } location ~ / { index index.jsp; proxy_pass http://127.0.0.1:18080; proxy_set_header referer ''; include proxy.conf; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location ~ /\. { deny all; access_log off; log_not_found off; } } }</pre><h3 style="background: rgb(255, 255, 255); border: 0px; margin: 0px 0px 20px; padding: 0px; vertical-align: baseline; clear: both; font-weight: normal; line-height: 1.5em; font-family: Georgia, " bitstream="" white-space:="">对proxy_cache_key参数的简单说明</h3><p style="background: rgb(255, 255, 255); border: 0px; margin-top: 0px; margin-bottom: 24px; padding: 0px; vertical-align: baseline; color: rgb(51, 51, 51); font-family: Georgia, " bitstream="" white-space:="">上述样例中,使用到了如下指令</p><pre 10="" style="background-color: rgb(247, 247, 247); border: 0px; margin-top: 0px; margin-bottom: 24px; padding: 1.5em; vertical-align: baseline; font-family: " courier="" color:="" line-height:="" overflow:="" text-size-adjust:="" font-size:="">proxy_cache_key $uri$is_args$args;</pre><ul style="list-style-type: square;" class=" list-paddingleft-2"><li><p>$uri,请求中的当前URI,例如/foo/bar.html。</p></li><li><p>$is_args,如果$args设置,值为”?”,否则为””。</p></li><li><p>$args,这个变量等于GET请求中的参数。例如,foo=123&bar=blahblah。</p></li></ul><p style="background: rgb(255, 255, 255); border: 0px; margin-top: 0px; margin-bottom: 24px; padding: 0px; vertical-align: baseline; color: rgb(51, 51, 51); font-family: Georgia, " bitstream="" white-space:="">使用上述参数,可以有效完成对如下资源样式的区分</p><pre 10="" style="background-color: rgb(247, 247, 247); border: 0px; margin-top: 0px; margin-bottom: 24px; padding: 1.5em; vertical-align: baseline; font-family: " courier="" color:="" line-height:="" overflow:="" text-size-adjust:="" font-size:=""><br/></pre><h2 style="background: rgb(255, 255, 255); border: 0px; margin: 0px 0px 20px; padding: 0px; vertical-align: baseline; font-weight: normal; line-height: 1.5em;" bitstream="" white-space:=""><br/></h2>
CopyRight 2002~2023 精通2100网 联系邮箱:qqtxt@163.com
版权所有:精通2100网
湘ICP备2023018646号-1
MYSQl共执行 4 个查询,用时 0.0022578239440918 秒,PHP脚本用时 0.005553 秒,占用内存 0.898 MB,Gzip 已启用