1、互联网动静分离架构
动静分离是指,静态页面与动态页面分开不同系统访问的架构设计方法。
2、Nginx实现动静分离
①项目准备
我们起一个springboot项目,端口9093,写一个动态方法,链接到index.html。在index.html里,引入静态js,但是工程里不引入js,将js放到静态资源服务器,目录:/home/soft/static。代码如下:
@SpringBootApplication@Controllerpublic class NginxDemoApplication {public static void main(String[] args) {SpringApplication.run(NginxDemoApplication.class, args);}@GetMapping("/index")public String index() {return "index";}}


②配置Nginx
静态资源,引用我们静态服务器的静态资源路径:/home/soft/static
server {listen 80;server_name 47.104.146.31;#拦截动态请求location / {proxy_pass http://localhost:9093;proxy_set_header X-Real-IP $remote_addr;}#拦截静态资源location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|js|css)$ {root /home/soft/static;}}
③启动工程
访问http://47.104.146.31/,我们会发现,工程里面没有jquery.js,但是实际效果却引用成功了。这是因为Nginx将静态资源路径指向了:/home/soft/static。由此,动静分离小Demo成功。
