经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Nginx » 查看文章
Nginx怎么实现实现动静分离
来源:cnblogs  作者:1naonao  时间:2019/8/29 8:34:05  对本文有异议

nginx 实现动静分离

Nginx动静分离基本概述

动静分离,通过中间件将动静分离和静态请求进行分离;
通过中间件将动态请求和静态请求分离,可以建上不必要的请求消耗,同事能减少请求的延时。
通过中间件将动态请求和静态请求分离,逻辑图如下:

动静分离只有好处:动静分离后,即使动态服务不可用,但静态资源不会受到影响。

Nginx动静分离场景实践

  1. location / {
  2. root /code/wordpress;
  3. index.php;
  4. }
  5. location ~* \.(png|jpg|mp4|)${
  6. root /code/wordpress/images;
  7. gzip on;
  8. .....
  9. }
  10. location ~ \.php$ {
  11. fastcgi_pass 127.0.0.1:9000;
  12. .....
  13. }

多台服务器实现动静分离

1.环境准备

系统 作用 服务 地址
Centos7.5 负载均衡 nginx proxy 10.0.0.5
Centos7.5 静态资源 nginx static 10.0.0.7
Centos7.5 动态资源 tomcat server 10.0.0.8

2.web配置静态资源

  1. [root@web01 ~]# cd /etc/nginx/conf.d/
  2. [root@web01 conf.d]# cat ds_oldboy.conf
  3. server {
  4. listen 80;
  5. server_name pic.drz.com;
  6. root /code;
  7. index index.html;
  8. location ~* .*\.(jpg|png|gif)$ {
  9. root /code/images;
  10. }
  11. }
  12. #配置一个主页
  13. [root@web01 conf.d]# echo "zls_test_web01" > /code/index.html
  14. #创建图片目录
  15. [root@web01 conf.d]# mkdir /code/images/
  16. #上传一个静态文件
  17. [root@web01 conf.d]# cd /code/images/
  18. [root@web01 images]# rz cjk.gif
  19. [root@web01 conf.d]# nginx -t
  20. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  21. nginx: configuration file /etc/nginx/nginx.conf test is successful
  22. [root@web01 conf.d]# nginx -s reload

3.验证

打开浏览器访问:http://pic.drz.com/

打开浏览器访问:http://pic.drz.com/cjk.gif

4.web02配置动态资源

  1. [root@web02 ~]# yum install -y tomcat
  2. [root@web02 ~]# mkdir /usr/share/tomcat/webapps/ROOT
  3. [root@web02 ~]# cat /usr/share/tomcat/webapps/ROOT/java_test.jsp
  4. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  5. <HTML>
  6. <HEAD>
  7. <TITLE>曾老湿JSP Page</TITLE>
  8. </HEAD>
  9. <BODY>
  10. <%
  11. Random rand = new Random();
  12. out.println("<h1>曾老湿随机数:<h1>");
  13. out.println(rand.nextInt(99)+100);
  14. %>
  15. </BODY>
  16. </HTML>
  17. [root@web02 webapps]# systemctl start tomcat

打开浏览器,访问:http://10.0.0.8:8080/java_test.jsp

img?

5.负载均衡上调度

  1. [root@lb01 conf.d]# cat proxy_ds.conf
  2. upstream static {
  3. server 172.16.1.7:80;
  4. }
  5. upstream java {
  6. server 172.16.1.8:8080;
  7. }
  8. server {
  9. listen 80;
  10. server_name pic.drz.com;
  11. location ~* \.(jpg|png|gif)$ {
  12. proxy_pass http://static;
  13. proxy_set_header Host $http_host;
  14. }
  15. location ~ \.jsp {
  16. proxy_pass http://java;
  17. proxy_set_header Host $http_host;
  18. }
  19. }
  20. [root@lb01 conf.d]# nginx -t
  21. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  22. nginx: configuration file /etc/nginx/nginx.conf test is successful
  23. [root@lb01 conf.d]# nginx -s reload

2.5 配置本地hosts,通过负载访问动态与静态资源
动态资源 ↓

静态资源 ↓

网站主页 ↓

6.负载均衡上整合动态和静态的html文件

  1. #编辑配置文件
  2. [root@lb01 ~]# cat /etc/nginx/conf.d/proxy_ds.conf
  3. upstream static {
  4. server 172.16.1.7:80;
  5. }
  6. upstream java {
  7. server 172.16.1.8:8080;
  8. }
  9. server {
  10. listen 80;
  11. server_name pic.drz.com;
  12. location / {
  13. root /code;
  14. index index.html;
  15. }
  16. location ~* \.(jpg|png|gif)$ {
  17. proxy_pass http://static;
  18. proxy_set_header Host $http_host;
  19. }
  20. location ~ \.jsp {
  21. proxy_pass http://java;
  22. proxy_set_header Host $http_host;
  23. }
  24. }
  25. [root@lb01 ~]# mkdir -p /code
  26. #编辑整合后的index.html
  27. [root@lb01 ~]# cat /code/index.html
  28. <html lang="en">
  29. <head>
  30. <meta charset="UTF-8" />
  31. <title>曾老湿测试ajax和跨域访问</title>
  32. <script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
  33. </head>
  34. <script type="text/javascript">
  35. $(document).ready(function(){
  36. $.ajax({
  37. type: "GET",
  38. url: "http://pic.drz.com/java_test.jsp",
  39. success: function(data){
  40. $("#get_data").html(data)
  41. },
  42. error: function() {
  43. alert("哎呦喂,失败了,回去检查你服务去~");
  44. }
  45. });
  46. });
  47. </script>
  48. <body>
  49. <h1>曾老湿带你测试动静分离</h1>
  50. <img src="http://pic.drz.com/cjk.gif"># 上传的图片名
  51. <div id="get_data"></div>
  52. </body>
  53. </html>

7.浏览器访问测试动静分离是否成功

可以尝试关掉静态或者动态的服务,测试是否互不影响

Nginx资源分离场景实践

Nginx通过负载均衡实现手机与PC调度至不通的后端节点应用案例

根据iphone、安卓,pc跳转不通的页面环境规划

系统版本 主机角色 外网IP 内网IP 提供端口
CentOS7.5 负载均衡 10.0.0.5 172.16.1.5 80
CentOS7.5 提供Android页面 172.16.1.7 9090
CentOS7.5 提供Iphone页面 172.16.1.7 9091
CentOS7.5 提供pc页面 172.16.1.7 9092

1.配置后端WEB节点的Nginx配置

  1. [root@web01 conf.d]# vim sj.conf
  2. server {
  3. listen 9090;
  4. location / {
  5. root /code/android;
  6. index index.html;
  7. }
  8. }
  9. server {
  10. listen 9091;
  11. location / {
  12. root /code/iphone;
  13. index index.html;
  14. }
  15. }
  16. server {
  17. listen 9092;
  18. location / {
  19. root /code/pc;
  20. index index.html;
  21. }
  22. }

2.为后端WEB节点配置对应的网站目录及代码

  1. [root@web01 conf.d]# mkdir /code/{android,iphone,pc}
  2. [root@web01 conf.d]# echo "我是安卓" > /code/android/index.html
  3. [root@web01 conf.d]# echo "我是iphone" > /code/iphone/index.html
  4. [root@web01 conf.d]# echo "我是computer" > /code/pc/index.html

3.配置负载均衡服务,根据不同的浏览器调度到不同的资源地

  1. [root@lb01 conf.d]# vim /etc/nginx/conf.d/proxy_sj.conf
  2. upstream android {
  3. server 172.16.1.7:9090;
  4. }
  5. upstream iphone {
  6. server 172.16.1.7:9091;
  7. }
  8. upstream pc {
  9. server 172.16.1.7:9092;
  10. }
  11. server {
  12. listen 80;
  13. server_name sj.drz.com;
  14. charset 'utf-8';
  15. location / {
  16. #如果客户端来源是Android则跳转到Android的资源;
  17. if ($http_user_agent ~* "Android") {
  18. proxy_pass http://android;
  19. }
  20. #如果客户端来源是Iphone则跳转到Iphone的资源;
  21. if ($http_user_agent ~* "Iphone") {
  22. proxy_pass http://iphone;
  23. }
  24. #如果客户端是IE浏览器则返回403错误;
  25. if ($http_user_agent ~* "MSIE") {
  26. return 403;
  27. }
  28. #默认跳转pc资源;
  29. proxy_pass http://pc;
  30. }
  31. }

4.使用浏览器访问,查看结果

实际上的配置

  1. server {
  2. listen 80;
  3. server_name www.drz.com;
  4. if ($http_user_agent ~* "Android|Iphone") { #修改负载,wel上也改
  5. rewrite ^/$ https://sj.drz.com redirect;
  6. }
  7. }

原文链接:http://www.cnblogs.com/1naonao/p/11426461.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号