码迷,mamicode.com
首页 > 其他好文 > 详细

nginx动静分离

时间:2018-05-20 14:21:44      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:ext   访问规则   tar.gz   lang   准备   dex   coding   enc   oid   

(1)静态资源站点配置

1.安装nginx 省略
2.配置nginx

#vim /usr/local/nginx/conf/nginx.conf 
http {
    include /usr/local/nginx/conf.d/*.conf;
    }
#mkdir /usr/local/nginx/conf.d 
#vim /usr/local/nginx/conf.d/www.test.com.conf
server {
        listen 8003;
        root /webroot/www;
        location / {
                index index.html;
                }
        location ~ .*\.(jpg|bmp|png|gif)$ {
                root /webroot/www/magic;
                }
    }

3.准备目录和资源

mkdir /webroot/www/magic -pv 
wget -O /webroot/www/magic/nginx.png http://nginx.org/nginx.png

4.重新加载

nginx -t 
nginx -s reload 
ss -antlup | grep 8003

5.验证
192.9.191.31:8003/nginx.png

(2)动态资源站点

1.安装java和tomcat

yum install java  -y 
cd /tmp 
wget http://mirrors.hust.edu.cn/apache/tomcat/tomcat-9/v9.0.8/bin/apache-tomcat-9.0.8.tar.gz
tar xf apache-tomcat-9.0.8.tar.gz -C /usr/local/
ln -sv /usr/local/apache-tomcat-9.0.8 /usr/local/tomcat 

2.准备资源

#vi /usr/local/tomcat/webapps/ROOT/test.jsp
<%@     page    language="java" import="java.util.*"    pageEncoding="utf-8"%>
<HTML>
<HEAD>
<TITLE>JSP      Test    Page</TITLE>
</HEAD>
<BODY>
<%
        Random  rand    =       new     Random();
        out.println("<h1>Random number:</h1>");
        out.println(rand.nextInt(99)+100);
%>
</BODY>
</HTML>

3.启动服务

/usr/local/tomcat/bin/catalina.sh start
ss -anltup | grep 8080
192.9.191.31:8080/test.jsp 

(3)nginx动静分离之通过扩展名,代理服务器配置

1.配置nginx,后缀名为.jpg转发到static组,后缀名.jsp转发到dynamic组

#vim /usr/local/nginx/conf/nginx.conf 
http {
    include /usr/local/nginx/conf.d/*.conf;
    }
#mkdir /usr/local/nginx/conf.d 
#vim /usr/local/nginx/conf.d/www.test.com.conf
upstream dynamic {
        server 192.9.191.31:8080;
        }
upstream static {
        server 192.9.191.31:8003;
        }
server {
        listen 80;
        server_name www.test.com;
        location ~ .*\.(jpg|bmp|png|gif)$ {
                proxy_pass http://static;
                }
        location ~ .*\.jsp$ {
                proxy_pass http://dynamic;
                }
    }

2.重新加载

nginx -t
nginx -s reload 
修改windows的hosts文件C:\Windows\System32\drivers\etc\hosts文件添加一条记录
192.9.191.30    www.test.com 

3.测试

http://www.test.com/test.jsp
http://www.test.com/nginx.png

(4)nginx动静分离之根据浏览器和手机类型调度

#vim  /usr/local/nginx/conf/nginx/nginx.conf 
http {
upstream firefox {
    server 192.9.191.31:8081;
    }
upstream chrome {
    server 192.9.191.31:8082;
    }
upstream iphone {
    server 192.9.191.31:8083;
    }
upstream android {
    server 192.9.191.31:8084;
    }
upstream other {
    server 192.9.191.31:8085;
    }
server {
    listen 80;
    server_name  192.9.191.30;
    location / {
        if ($http_user_agent ~* "Firefox"){             //匹配firefox浏览器
        proxy_pass http://firefox;
            }
        if ($http_user_agent ~* "Chrome"){              //匹配chrome谷歌浏览器
        proxy_pass http://chrome;
            }
        if ($http_user_agent ~* "iphone"){              //匹配iphone手机
        proxy_pass http://iphone;
            }
        if ($http_user_agent ~* "android"){             //匹配安卓手机
        proxy_pass http://android;
            }
        proxy_pass http://other;                        //其它浏览器默认访问规则
        }
    }
}

nginx动静分离

标签:ext   访问规则   tar.gz   lang   准备   dex   coding   enc   oid   

原文地址:https://www.cnblogs.com/lovelinux199075/p/9063187.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!