Nginx 动静分离 -02

Nginx 动静分离 ---- --------切记 10.10.0.7

一、单台机器动静分离
[root@web01 ~]# cat /etc/nginx/conf.d/linux12.wp.com.conf 
server {
    listen 80;
    server_name linux12.wp.com;

    location / {
		root /mm/wordpress;
		index index.php;
    }

    location ~* \.(jpg|png|gif)$ {
		root /mm/wordpress;
    }

    location ~* \.php$ {
		fastcgi_pass 127.0.0.1:9000;
		fastcgi_param SCRIPT_FILENAME /mm/wordpress/$fastcgi_script_name;
		include fastcgi_params;
    }
}
2.多台机器的动静分离

在这里插入图片描述

1.准备环境
环境准备

 主机	     IP	        主机角色              服务              条件 

web01	10.0.0.7	  静态资源             nginxt               关闭防火墙和selinux
 
web02	10.0.0.8	  动态资源             nginx                关闭防火墙和selinux
  
lb01    10.10.0.5     负载均衡   		   comcat	            关闭防火墙和selinux

1、配置web01的静态资源
1.配置nginx
[root@web01 conf.d]# vi linux12.dj.com.conf
server {
    listen 80;
    server_name linux12.dj.com;

    location ~* \.(jpg|png|mp4|gif)$ {
        root /mm/picture;
    }
}
## 检查nginx -t
2.上传静态资源
 ## 创建目录 mkdir /mm/picture
[root@web01 ~]# mkdir /mm/picture

[root@web01 ~]# cd /mm/picture/
[root@web01 picture]# rz上传
total 116
-rw-r--r-- 1 root root 86756 Mar 27 18:02 5.png
-rw-r--r-- 1 root root 25836 Mar 27 18:22 7.png

### 授权
[root@web01 ~]# chown -R www.www /mm/

## 检查nginx -t 并重启
[root@web01 ~]# systemctl restart nginx
3.测试静态资源并访问
1.本地配置hosts
    
    10.10.0.7 linux12.dj.com
    
2、访问静态资源
    http://linux12.dj.com/5.png
## 1.配置web02的动态资源
# 1.安装tomcat
[root@web02 ~]# yum install -y tomcat
2.配置动态资源
[root@web02 ~]# cd /usr/share/tomcat/webapps

[root@web02 webapps]# mkdir ROOT
[root@web02 webapps]# vi ROOT/java_test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<HTML>
    <HEAD>
        <TITLE>测试动态的资源</TITLE>
    </HEAD>
    <BODY>
        <%
            Random rand = new Random();
            out.println("<h1>随机数:<h1>");
            out.println(rand.nextInt(99)+100);
        %>
    </BODY>
</HTML>

## 检查nginx -t 并重启
[root@web01 ~]# systemctl restart tomcat
3.测试动态资源并访问
1、配置本地hosts
	10.10.0.8 linux12.dj.com
	
2、访问动态资源
	http://linux12.dj.com:8080/java_test.jsp
1、配置负载均衡
# 1.配置文件  --01
[root@lb01 ~]# vi /etc/nginx/conf.d/linux12.dj.com.conf 
upstream dt {
    server 10.10.0.8:8080;
    ### 可以配置多台  server 10.0.0.18:8080;
}

upstream jt {
    server 10.10.0.7;
    ### 可以配置多台  server 10.0.0.11;
}

server {
    listen 80;
    server_name linux12.dj.com;

    location / {
        root /mm/dj;
        index index.html;
    }

    location ~* \.(jpg|png|gif)$ {
        proxy_pass http://jt;
        include proxy_params;
    }

        proxy_pass http://dt;
        include proxy_params;
    }
}
# 1.配置文件  --02
[root@lb01 dj]# cat /etc/nginx/conf.d/linux12.dj.com.conf 
server {
    listen 80;
    server_name linux12.dj.com;

    location / {
        root /mm/dj;
        index index.html;
    }

    location ~* \.(jpg|png|gif)$ {
        proxy_pass http://10.10.0.7;
        include proxy_params;
    }

    location ~* \.(php|jsp)$ {
        proxy_pass http://10.10.0.8:8080;
        include proxy_params;
    }
}


## 检查nginx -t 并重启
[root@lb01 ~]# systemctl restart nginx
3.测试动静分离资源并访问
1、配置本地hosts
	10.10.0.5 linux12.dj.com
	
2、访问
	http://linux12.dj.com/java_test.jsp
	http://linux12.dj.com/5.png
4、整合静态资源和动态资源
1.1创建站点目录
[root@lb01 ~]# mkdir -p /mm/dj 
1.2.编辑html文件
[root@lb01 ~]# vim /mm/dj/index.html
[root@lb01 dj]# cat index.html 
<head>
        <meta charset="UTF-8" />
        <title>测试ajax和跨域访问</title>
        <script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
</head>
<script type="text/javascript">
$(document).ready(function(){
        $.ajax({
        type: "GET",
        url: "http://linux12.dj.com/java_test.jsp",
        success: function(data){
                $("#get_data").html(data)
        },
        error: function() {
                alert("小姐姐,断网了,重新检查网络再来哦~");
        }
        });
});
</script>
        <body>
                <h1>测试动静分离---霉霉</h1>
                <img src="http://linux12.dj.com/5.png">
                <div id="get_data"></div>
        </body>
</html>
1.3.授权目录
[root@lb01 ~]# chown -R www.www /mm/
1.4. 访问域名测试
结论:静态资源出现问题不影响动态资源,动态资源出问题不影响静态资源
二、nginx资源分离 --------切记 10.10.0.8
1.准备环境
环境准备

 主机	     IP	                    主机角色                                  条件 
  
web01	10.10.0.7	             Android页面                           关闭防火墙和selinux
  
web02	10.10.0.8	             iPhone页面                            关闭防火墙和selinux
 
web03	172.16.1.9	             PC端页面                              关闭防火墙和selinux
  
lb01    10.10.0.5                负载均衡   		                   关闭防火墙和selinux
        172.16.1.5 
2.配置web01服务器
# 1、配置nginx
[root@web01 ~]# vim /etc/nginx/conf.d/linux12.sj.com.conf
server {
    listen 80;
    server_name linux12.sj.com;
	charset utf8;

    location / {
        root /mm/android;
        index index.html;
    }
}

## 检查nginx -t 并重启
[root@web01 ~]# systemctl restart nginx
2、创建站点目录
[root@web01 ~]# mkdir -p /mm/android
[root@web01 ~]# echo "我是android" >> /mm/android/index.html
[root@web01 ~]# chown -R www.www /mm/android/
3、访问测试
# 1.配置hosts
10.10.0.7 linux12.sj.com
3.配置web02服务器
1、配置nginx
[root@web02 ~]# vim /etc/nginx/conf.d/linux12.sj.com.conf
server {
    listen 80;
    server_name linux12.sj.com;
    charset utf8;

    location / {
        root /mm/iphone;
        index index.html;
    }
}

## 检查nginx -t 并重启
[root@web01 ~]# systemctl restart nginx
2、创建站点文件
[root@web02 ~]# mkdir -p /mm/iphone
[root@web02 ~]# echo "我是Iphone" >> /mm/iphone/index.html
[root@web02 ~]# chown -R www.www /mm/iphone/
3、访问测试
# 1.配置hosts
10.10.0.8 linux12.sj.com
4.配置web03服务器
# 1、配置nginx
[root@web03 ~]# vim /etc/nginx/conf.d/linux12.sj.com.conf
server {
    listen 80;
    server_name linux12.sj.com;
    charset utf8;

    location / {
        root /mm/pc;
        index index.html;
    }
}

## 检查nginx -t 并重启
[root@web01 ~]# systemctl restart nginx
2、创建站点文件
[root@web03 ~]# mkdir -p /mm/pc 
[root@web03 ~]# echo "我是pc端" >> /mm/pc/index.html
[root@web03 ~]# chown -R www.www /mm/
3、访问测试
# 1.配置hosts
10.10.0.9 linux12.sj.com
5.配置负载均衡
# 1.配置nginx  -01
[root@lb01 ~]# vim /etc/nginx/conf.d/linux12.sj.com.conf
upstream android {
    server 10.10.0.7;
}

upstream iphone {
    server 10.10.0.8;
}

upstream pc {
    server 10.10.0.9;
}

server {
    listen 80;
    server_name linux12.sj.com;

    location / {
        if ($http_user_agent ~* "Android") { #判断如果是安卓端
            proxy_pass http://android;		 #代理到android虚拟主机池
        }
        if ($http_user_agent ~* "iPhone") {	 #判断如果是苹果端
            proxy_pass http://iphone;		#代理到iphone虚拟主机池
        }
        if ($http_user_agent ~* "WOW64") {	#判断如果是IE浏览器
            return 403;					   #直接返回403
        }
        proxy_pass http://pc;				#如果没有匹配到以上内容,默认都代理到pc虚拟主机池
        include proxy_params;
    }
}

# 1.配置nginx  -02
[root@lb01 conf.d]# cat linux12.sj.com.conf 
server {
    listen 80;
    server_name linux12.sj.com;

    location / {
        if ($http_user_agent ~* "Android") { #判断如果是安卓端
            proxy_pass http://10.10.0.7;		 #代理到android虚拟主机池
        }
        if ($http_user_agent ~* "iPhone") {	 #判断如果是苹果端
            proxy_pass http://10.10.0.8;		#代理到iphone虚拟主机池
        }
        if ($http_user_agent ~* "WOW64") {	#判断如果是IE浏览器
            return 403;					   #直接返回403
        }
        proxy_pass http://10.10.0.9;				#如果没有匹配到以上内容,默认都代理到pc虚拟主机池
        include proxy_params;
    }
}


## 检查nginx -t 并重启
[root@web01 ~]# systemctl restart nginx
2、访问测试
# 1.配置hosts
10.10.0.5 linux12.sj.com

# 2.访问

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CY19Wm8R-1617788292492)(C:\Users\17155\Desktop\下载图片\1617721464626.png)]


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FikL-09-19

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值