nginx负载均衡_请求分发策略研究

本文适合有一定nginx基础或者web开发基础的人员阅读

 

不同域名访问同一主机: 

步骤1、在C:\Windows\System32\drivers\etc\hosts中配置两个域名

 127.0.0.1       xx
 127.0.0.1       yy

步骤2、用myeclipse新建两个工程,在index.jsp文件中写入下面代码:

 <body>
    xx <br>
       <%
        request.setCharacterEncoding("utf-8");
    	out.println("request.getParameterMap:<br />");
    	Map mp=request.getParameterMap();
    	for(Object obj:mp.keySet())
    	out.println(obj+":"+request.getParameter(obj.toString())+"<br />");
     %>
  </body>


部署到tomcat中,tomcat的端口是8080

步骤3、配置nginx

在%nginx_home%/conf/niginx.conf中加入:

http{

    include server/xx.conf;
    include server/yy.conf;

}

在conf下创建server目录

分别创建xx.conf和yy.conf,文件内容分别如下:

xx.conf:

    server {
        listen       80;      
        server_name  xx;
        location / {
            proxy_pass        http://xx:8080/xx/;
            proxy_set_header  X-Real-IP  $remote_addr;
            rewrite /file/(.*) /?fileID=$1;
            rewrite /article/(.*) /?articleID=$1;
        }
    }

yy.conf

    server {
        listen       80;      
        server_name  yy;
        location / {
            root   html;
            index yy.html;
        }
    }

步骤4、测试结果

重启nginx,

在浏览器中输入:http://xx/,显示如下:

xx 
 request.getParameterMap:

在浏览器中输入:http://yy/,(yy.html是我放在’%nginx_home%/html/‘目录下的一个文件)显示如下:

Welcome to nginx!
my server name is 'yy'

至此,不同域名访问同一主机不同工程演示完成。 

niginx中URL重写和伪静态化配置

配置说明

xx.conf

   proxy_pass        http://xx:8080/xx/;表示,将请求交给tomcat的xx工程代理

   rewrite /file/(.*) /?fileID=$1; 表示把http://xx/file/12345 这个链接交给http://xx:8080/xx?fileID=12345处理

测试结果

 在浏览器中输入:http://xx/file/12345,显示如下:

xx 
 request.getParameterMap:
 fileID:12345

至此nginx的url重写已经完成。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值