路人战队|阿里P7资深架构师Tomcat笔记分享( 三 )


路人战队|阿里P7资深架构师Tomcat笔记分享Snip20160811_58.png
不过 , 对 SHOP++ 的访问中 , css、js 文件并没有被缓存 , 不知什么原因 , 只有图片被缓存了 , 多次刷新 , tomcat 服务器都收到如下请求:
路人战队|阿里P7资深架构师Tomcat笔记分享Snip20160811_59.png
另外我们可以看看缓存目录:
[root@lamp1 nginx]# ll /nginx/cache/total 40drwx------ 3 nginx nginx 4096 Aug 11 09:24 0drwx------ 3 nginx nginx 4096 Aug 11 15:58 3drwx------ 3 nginx nginx 4096 Aug 11 09:17 4drwx------ 4 nginx nginx 4096 Aug 11 15:58 8drwx------ 4 nginx nginx 4096 Aug 11 09:24 9drwx------ 5 nginx nginx 4096 Aug 11 16:00 adrwx------ 3 nginx nginx 4096 Aug 11 15:58 bdrwx------ 5 nginx nginx 4096 Aug 11 16:01 cdrwx------ 5 nginx nginx 4096 Aug 11 09:24 edrwx------ 3 nginx nginx 4096 Aug 11 16:00 f[root@lamp1 nginx]# du -sh /nginx/cache/328K/nginx/cache/可看到是有缓存内容的 。 好了到这里我们的nginx缓存服务就配置完成了 , 下面我们看一下如何实现动静分离 。
4.Nginx将请求实现动静分离首先 , 我们来说一下我们要实现的效果 , 上面我们已经将静态内容缓存在nginx服务器上 , 我们想让用户请求的静态内容到nginx去取 , 动态内容到tomcat服务器上去取 , 这就能实现动静分享效果 。 同样的首先我们来修改配置文件 ,
[root@lamp1 nginx]# cat conf.d/default.confserver {listen80;server_namelocalhost;#charset koi8-r;#access_log/var/log/nginx/log/host.access.logmain;location ~* "\.(jsp|do)$" {proxy_pass http://backend;}location / {rewrite ^(/.*)$ $1/index.jsp;}location ~* "\.(jpg|jpeg|png|gif|html|css|js)$" {proxy_pass http://backend;proxy_cache first;proxy_cache_valid 200 24h;proxy_cache_valid 302 10m;add_header X-Cache-Status $upstream_cache_status;}#error_page404/404.html;# redirect server error pages to the static page /50x.html#error_page500 502 503 504/50x.html;location = /50x.html {root/usr/share/nginx/html;}}对于 location 的查找 , 修饰符 = 表示 URI 与 前缀字符串 必须精确匹配 。 如果能够精确匹配 , 则结束查找 。 如果对于 / 的请求很频繁 , 可为 location / 添加 = 修饰符 , 这样可以加速处理过程 , 因为一次匹配查找即可结束 。 这里只是提一下 , 配置里没有这样定义 。
在上面的配置中 , 对于以 / 起始的请求做内部重定向 , 比如访问, 被内部重定向到 /index.jsp;访问被内部重定向到 /index.jsp 。 访问与 / 是等效的 。
重定向是这样定义的:
location / {rewrite ^(/.*)$ $1/index.jsp;}动态内容交给后端 tomcat 服务器:
location ~* "\.(jsp|do)$" {proxy_pass http://backend;}静态内容有本地缓存处理 , 对于第一次访问请求 , 内容还是从后端取得的:
location ~* "\.(jpg|jpeg|png|gif|html|css|js)$" {proxy_pass http://backend;proxy_cache first;proxy_cache_valid 200 24h;proxy_cache_valid 302 10m;add_header X-Cache-Status $upstream_cache_status;}到此这篇关于文章就结束了!
点关注 , 不迷路!如果本文对你有帮助的话不要忘记点赞支持哦!
路人战队|阿里P7资深架构师Tomcat笔记分享上述2020阿里面试集锦&最新2020整理收集的一些面试题(都整理成文档) , 有需要的可以私信回复:“2020” 即可免费领取 。


推荐阅读