zoukankan      html  css  js  c++  java
  • nginx动静分离

    (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:WindowsSystem32driversetchosts文件添加一条记录
    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;						//其它浏览器默认访问规则
    		}
    	}
    }
    

    (5)基于目录调度

    1.方式一

    upstream static {
            server 192.9.191.31:8081;
            }
    upstream upload {
            server 192.9.191.31:8082;
            }
    upstream default {
            server 192.9.191.31:8083;
            }
    server {
            listen 80;
            server_name 192.9.191.30;
            location / {
                    proxy_pass http://default;
                    }
            location /static/ {
                    proxy_pass http://static;
                    }
            location /upload/ {
                    proxy_pass http://upload;
                    }
            }
    

    2.方式二

    upstream static {
            server 192.9.191.31:8081;
            }
    upstream upload {
            server 192.9.191.31:8082;
            }
    upstream default {
            server 192.9.191.31:8083;
            }
    server {
            listen 80;
            server_name 192.9.191.30;
            location / {
                    if ($request_uri ~* "^/static/(.*)$") {
                            proxy_pass http://static;
                            }
    
                    if ($request_uri ~* "^/upload/(.*)$") {
                            proxy_pass http://upload;
                            }
                    proxy_pass http://default;
                    }
            }
    
  • 相关阅读:
    2019.10.31 答辩回来后;
    2019.10.22
    2019.10.21 上周总结+今天
    2019.10.20 没有人可以阻挡自己努力,男朋友也不可以。少情感,多学习;;多夸夸他肯定他
    2019.10.19 干什么? 不要纠结了于下手什么了 趁还年轻 去做吧!!
    2019.10.16 每天问自己 三遍 或者更多:我收获了什么?我收获了什么?我收获了什么???
    Java 错误:Constructor call must be the first statement in a constructor
    2019.10.14 解决讨好型人格;时间管理划分等级+设定时间上限
    2019.10.14 今天看到的业务大佬的肺腑之言
    放几张吴悠校园行
  • 原文地址:https://www.cnblogs.com/lovelinux199075/p/9063187.html
Copyright © 2011-2022 走看看