nginx动静分离与资源分离

nginx动静分离

[root@pingweb01 ~]# cd /mm/
[root@pingweb01 mm ]# cat mm.php 
<?php
$arr=range(1,10);
shuffle($arr);
foreach($arr as $values)
{
 echo $values." ";
}
?>
[root@pingweb01 ~]# cd /mm/
[root@web01 mm]# cat index.php 
<?php
echo '<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Title</title></head><body style="color: red" background="/5.pg"><h1>Hello(嗨),我是陈阳,大家可以叫我阳哥!</h1><ul><li>上海</li><li>北京</li><li>杭州</li></ul></body></html>';
<?php
$arr=range(1,10);
shuffle($arr);
foreach($arr as $values)
{
 echo $values."<br/> ";
}
?>
# 1.创建图片文件
[root@pingweb01 mm]# cd picture/
[root@pingweb01 picture]# ll
total 116
-rw-r--r-- 1 www www 24864 May  5 22:42 100.jpg
-rw-r--r-- 1 www www 86756 Mar 27 18:02 5.png

# 2.nginx文件调用
[root@web01 mm]# vim /etc/nginx/php.params 
  location ~* \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
# 3.编写配置文件

[root@web01 mm]# vim /etc/nginx/conf.d/test.conf 
server {
    listen 80;
    server_name -;
    root /mm/;
    location / {
		index index.php index.html;
    }
     location ~* \.(jpg|gif|png|jpeg)$ {
        root /mm/picture;
    }
    include php.params;
}

# 4.重启nginx
[root@web01 mm]# systemctl restart nginx
# 5.本地hosts访问
192.168.15.7


# 1.静态资源在另一台机器
[root@db01 ~] yum -y install nginx
# 2.最好配置成官方源nginx
# 3.上传文件
 [root@db01 ~] cd /usr/share/nginx/html
 [root@db01 html] rz
 [root@db01 html]# ll
total 116
-rw-r--r-- 1 www www 24864 May  5 22:42 100.jpg
-rw-r--r-- 1 www www 86756 Mar 27 18:02 5.png
# 4.配置文件
[root@db01 html]# cat /etc/nginx/conf.d/picture.conf 
server {
	listen 80;
	server_name _;
	root /usr/share/nginx/html;
	location / {
		index index.png;
	}
}
# 4.重启nginx
[root@db01 html]# systemctl restart nginx

# 1.web端测试访问
[root@web01 conf.d]# cat test.conf 
upstream picture {
	server 172.16.1.51:80;
}
server {
	listen 80;
	server_name _;
	root /www/test;	
	location / {
		index index.php index.html;
	}
	location ~* \.(gif|jpg|png)$ {
		proxy_pass http://picture;
	}
	include php.params;	
}
#2.重启nginx
[root@web01 conf.d]# systemctl restart nginx
# 3.本地hosts访问
192.168.15.7

nginx资源分离

按照访问的资源的不同,访问的结果也不同。

按照不同的设备,返回不同的页面,这种类型叫做资源隔离
1.配置站点
[root@web01 conf.d]# mkdir /mm/{android,pc,iphone}
[root@web01 conf.d]# echo "我是Android" > /mm/android/index.html
[root@web01 conf.d]# echo "我是Iphone" > /mm/iphone/index.html
[root@web01 conf.d]# echo "我是computer" > /mm/pc/index.htm
[root@web01 conf.d]# chown -R www.www /mm/
2.代理配置
[root@web01 conf.d]# cat iphone.conf 
server {
	listen 8082;
	server_name _;
	location / {
		root /www/resources/iphone;
		index index.html;
	}
}

[root@web01 conf.d]# cat android.conf 
server {
	listen 8081;
	server_name _;
	location / {
		root /mm/android;
		index index.html;
	}
}

[root@web01 conf.d]# cat pc.conf 
server {
	listen 8083;
	server_name _;
	location / {
		root /mm/pc;
		index index.html;
	}
}

# 重启nginx
[root@lb01 conf.d]# systemctl restart nginx
3、配置服务器文件
[root@web01 conf.d]# cat all.conf 
upstream android {
    server 172.16.1.7:8081;
}

upstream pc {
    server 172.16.1.7:8083;
}

upstream iphone {
    server 172.16.1.7:8082;
}

server {
    listen 80;
    server_name _;
    location / {
       if ($http_user_agent ~* "Android" ) {
	   proxy_pass http://android;
       }
       if ($http_user_agent ~* "iphone" ) {
           proxy_pass http://iphone;
       }
       if ($http_user_agent ~* "Trident" ) {
           return 403;
       }
       proxy_pass http://pc;
    }
}
4.本地hosts访问测试
192.168.15.7 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

FikL-09-19

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

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

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

打赏作者

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

抵扣说明:

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

余额充值