• 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;
                    }
            }
    
  • 相关阅读:
    Centos 6.8安装配置KVM
    使用明小子进行网页渗透测试-XYCMS企业建站系统2.3(鑫跃教育)
    VSFTP2.3.4(笑脸漏洞)渗透测试
    MS08_067smb漏洞利用渗透测试
    css基本概念与css核心语法介绍
    HTML块级、行级元素,特殊字符,嵌套规则
    网页DIV+CSS布局与ifame传统布局对比
    html基本标签表单实现交互原理,单选框,复选框,下拉框介绍
    HTML基础标签图片文本超链接列表表格介绍
    html网页基本结构
  • 原文地址:https://www.cnblogs.com/lovelinux199075/p/9063187.html
Copyright © 2020-2023  润新知