nginx学习网站的动静分离

  • 什么是网站的动静分离

  1. 就是将一个系统的动态资源和静态资源分离,不会部署在同一台服务器上。

动态资源  就是: 不同用户不同场景访问,都不一样的页面,如百度的搜索结果,淘宝的商品列表(jsp、servlet)

静态资源  就是:几乎不变的页面(或者变化频率很低),如首页等html、css、img、js......

伪静态技术:就是将动态资源伪装成静态资源,使用模板引如: freemaker、thymeleaf......

  • 动静分离原理

  1. 使用tomcat做动态服务器
  2. 使用nginx做静态服务器  (nginx做静态服务器比tomcat性能高
  • 使用nginx静态资源服务器

  1. window下在C:\Windows\System32\drivers\etc\hosts文件末尾加上  127.0.0.1   static.hope.com
  2. 进入nginx的conf/nginx.conf文件修改配置

server {
        listen       8888;                                     # 监听的端口号
        server_name  static.hope.com;            #  访问的域名

        location /logo/logo {                              # location配置的路径   使用域名+端口+location路径+图片名即可访问
            root    C:/Users/123456/Desktop/;    # root目录  
            index  index.html index.htm;
        }
    }

 如图下

      3.  使用http://static.hope.com:8888/logo/logo/logo.png即可访问你要代理的图片 

           该图片的路径是 C:\Users\123456\Desktop\logo\logo\logo.png

  •  使用nginx代理动态资源服务器

          静态资源服务器访问成功后,

  1. window下在C:\Windows\System32\drivers\etc\hosts文件末尾加上  127.0.0.1   www.hope.com
  2. 进入nginx的conf/nginx.conf文件修改配置  注:一个nginx可同时做静态服务器和动态服务器

server {
        listen       8888;
        server_name  www.hope.com;

        location / {
            proxy_pass   http://127.0.0.1:2018;
            index  index.html index.htm;
        }
    }

图如下

     3.  在tomcat2018中root目录index.html页面加  静态服务器的资源图片路径

           <img alt=""  src="http://static.hope.com:8888/logo/logo/logo.png" />

     4.  使用www.hope.com:8888即可访问该tomcat

猜你喜欢

转载自blog.csdn.net/xiaobo5264063/article/details/86561443