路人战队|阿里P7资深架构师Tomcat笔记分享( 三 )
Snip20160811_58.png
不过 , 对 SHOP++ 的访问中 , css、js 文件并没有被缓存 , 不知什么原因 , 只有图片被缓存了 , 多次刷新 , 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;}
到此这篇关于文章就结束了!
点关注 , 不迷路!如果本文对你有帮助的话不要忘记点赞支持哦!
上述2020阿里面试集锦&最新2020整理收集的一些面试题(都整理成文档) , 有需要的可以私信回复:“2020” 即可免费领取 。
推荐阅读
- 五商文化资讯微软选择“沉海”,华为却深藏贵州大山!阿里亚马逊也纷纷布局
- “1元店”背后,阿里与拼多多的“真香战”谁能赢?
- 阿里巴巴|壕!阿里又斥资280亿买了一家公司,它旗下的超市你肯定知道!
- 南宁邦|当街跪地被母亲骂:“这辈子你去坐牢啊!”,惨!南宁一学生开摩托撞死路人
- 爱游戏的甲子|G2战队“杀人诛心”,阿P用两句话嘲讽LCK战队,DWG被逼入绝境
- 老猫要有腹肌|AG超玩会战队人气位列榜首,久诚人气不及一诺
- 财经女记者部落|老板却坚决拒绝粉丝经济,选手只招大学生,电竞战队斩获《炉石传说》联赛冠军
- 电竞小肥仔|和平精英:战队专属皮肤推荐合集,最后JDE队服堪比至尊金龙!
- 海军陆战队|美两大军种争资源,抢着对抗中国?美专家:对手导弹太多,应互补
- 4am战队|绝地求生秋季赛决赛第一日-rng登顶,4am稳定,ifty、tianba拉闸