013.Nginx动静分离
一 动静分离概述
1.1 动静分离介绍
1.2 动静分离原理
二 动静分离实现–根据文件后缀
2.1 环境准备
2.2 创建静态站点
1 [root@nginx02 ~]# mkdir /usr/share/nginx/staticrs/ 2 [root@nginx02 ~]# echo '<h1>Static_Web</h1>' > /usr/share/nginx/staticrs/index.html 3 [root@nginx02 ~]# ll /usr/share/nginx/staticrs/ #上传示例图片静态资源 4 total 16K 5 -rw-r--r-- 1 root root 20 Jun 20 14:32 index.html 6 -rw-r--r-- 1 root root 11K Jun 20 14:35 nginx.jpg 7 [root@nginx02 ~]# mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak
1 [root@nginx02 ~]# vi /etc/nginx/conf.d/staticrs.conf 2 server { 3 listen 80; 4 server_name staticrs.linuxds.com; 5 access_log /var/log/nginx/staticrs.access.log main; 6 error_log /var/log/nginx/staticrs.error.log warn; 7 location / { 8 root /usr/share/nginx/staticrs; 9 index index.html; 10 } 11 }
1 [root@nginx02 ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件 2 [root@nginx02 ~]# nginx -s reload #重载配置文件
2.3 创建动态站点
1 [root@nginx03 ~]# yum install -y tomcat 2 [root@nginx03 ~]# mkdir -p /usr/share/tomcat/webapps/ROOT
1 [root@nginx03 ~]# vi /usr/share/tomcat/webapps/ROOT/javatest.jsp #构建动态测试页面 2 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 3 <HTML> 4 <HEAD> 5 <TITLE>JSP Test Page</TITLE> 6 </HEAD> 7 8 <BODY> 9 <% 10 Random rand = new Random(); 11 out.println("<h1>随机数:<h1>"); 12 out.println(rand.nextInt(99)+100); 13 %> 14 </BODY> 15 </HTML>
1 [root@nginx03 ~]# systemctl start tomcat.service #启动tomcat
2.4 配置前端动静分离
1 [root@nginx01 ~]# mkdir -p /usr/share/nginx/dss 2 [root@nginx01 ~]# ll /usr/share/nginx/dss/ 3 total 4.0K 4 -rw-r--r-- 1 root root 1.9K Jun 20 18:10 test.css #模拟css
1 [root@nginx01 ~]# vi /etc/nginx/conf.d/dss.conf #配置Dynamic-Static Separation 2 upstream static_server { 3 server 172.24.10.22; 4 } 5 upstream tomcat_server { 6 server 172.24.10.23:8080; 7 } 8 9 server { 10 listen 80; 11 server_name dss.linuxds.com; 12 access_log /var/log/nginx/dss.access.log main; 13 error_log /var/log/nginx/dss.error.log warn; 14 proxy_set_header X-Real-IP $remote_addr; 15 proxy_set_header Host $host; 16 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 17 proxy_set_header X-Forwarded-Proto $scheme; 18 # location / { 19 # root html; 20 # index index.html; 21 # } 22 location / { 23 proxy_pass http://static_server; 24 } 25 location ~ .*\.(css)$ { 26 root /usr/share/nginx/dss; 27 } 28 location ~ .*\.(htm|html|gif|jpg|jpeg|png|gif|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma) { 29 proxy_pass http://static_server; 30 expires 5d; 31 } 32 location ~ .*\.jsp$ { 33 proxy_pass http://tomcat_server; 34 expires 1h; 35 } 36 error_page 500 502 503 504 /50x.html; 37 location = /50x.html { 38 root html; 39 } 40 }
1 [root@nginx01 ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件 2 [root@nginx01 ~]# nginx -s reload #重载配置文件
2.5 访问测试
三 动静分离实现–根据文件后缀
3.1 环境准备
3.2 创建静态站点
1 [root@nginx02 ~]# mkdir /usr/share/nginx/staticrs/ 2 [root@nginx02 ~]# echo '<h1>Static_Web</h1>' > /usr/share/nginx/staticrs/index.html 3 [root@nginx02 ~]# ll /usr/share/nginx/staticrs/ #上传示例图片静态资源 4 total 16K 5 -rw-r--r-- 1 root root 20 Jun 20 14:32 index.html 6 -rw-r--r-- 1 root root 11K Jun 20 14:35 nginx.jpg 7 [root@nginx02 ~]# mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak
1 [root@nginx02 ~]# vi /etc/nginx/conf.d/staticrs.conf 2 server { 3 listen 80; 4 server_name staticrs.linuxds.com; 5 access_log /var/log/nginx/staticrs.access.log main; 6 error_log /var/log/nginx/staticrs.error.log warn; 7 location /static { 8 alias /usr/share/nginx/staticrs; 9 index index.html; 10 } 11 }
1 [root@nginx02 ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件 2 [root@nginx02 ~]# nginx -s reload #重载配置文件
3.3 创建动态站点
1 [root@nginx03 ~]# yum install -y tomcat 2 [root@nginx03 ~]# mkdir -p /usr/share/tomcat/webapps/ROOT/dynamic
1 [root@nginx03 ~]# vi /usr/share/tomcat/webapps/ROOT/dynamic/javatest.jsp 2 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 3 <HTML> 4 <HEAD> 5 <TITLE>JSP Test Page</TITLE> 6 </HEAD> 7 8 <BODY> 9 <% 10 Random rand = new Random(); 11 out.println("<h1>随机数:<h1>"); 12 out.println(rand.nextInt(99)+100); 13 %> 14 </BODY> 15 </HTML>
1 [root@nginx03 ~]# systemctl start tomcat.service #启动tomcat
3.4 配置前端动静分离
1 [root@nginx01 ~]# mkdir -p /usr/share/nginx/dss 2 [root@nginx01 ~]# ll /usr/share/nginx/dss/ 3 total 4.0K
1 [root@nginx01 ~]# vi /etc/nginx/conf.d/dss.conf #配置Dynamic-Static Separation 2 upstream static_server { 3 server 172.24.10.22; 4 } 5 upstream tomcat_server { 6 server 172.24.10.23:8080; 7 } 8 9 server { 10 listen 80; 11 server_name dss.linuxds.com; 12 access_log /var/log/nginx/dss.access.log main; 13 error_log /var/log/nginx/dss.error.log warn; 14 proxy_set_header X-Real-IP $remote_addr; 15 proxy_set_header Host $host; 16 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 17 proxy_set_header X-Forwarded-Proto $scheme; 18 # location / { 19 # root html; 20 # index index.html; 21 # } 22 location / { 23 proxy_pass http://static_server; 24 } 25 location ~ .*\.(css)$ { 26 root /usr/share/nginx/dss; 27 } 28 location /static/ { 29 proxy_pass http://static_server; 30 expires 5d; 31 } 32 location /dynamic/ { 33 proxy_pass http://tomcat_server; 34 expires 1h; 35 } 36 error_page 500 502 503 504 /50x.html; 37 location = /50x.html { 38 root html; 39 } 40 }
1 [root@nginx01 ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件 2 [root@nginx01 ~]# nginx -s reload #重载配置文件
3.5 访问测试