nginx配置实现反向代理&负载均衡

来源:互联网 发布:手机怎么登淘宝电脑版 编辑:程序博客网 时间:2024/06/04 18:26

两个域名指向同一台nginx服务器,用户访问不同的域名显示不同的网页内容。

两个域名是www.sian.com.cn和www.sohu.com(需要修改本地hosts文件)

在windows系统中,hosts文件的路径为(C:\Windows\System32\drivers\etc)配置方式为:ip地址 对应域名

比如:192.168.25.148 www.taobao.com

nginx服务器使用虚拟机192.168.101.3 


配置步骤如下:

第一步:安装两个tomcat,分别运行在8080和8081端口。

第二步:启动两个tomcat。

第三步:反向代理服务器的配置


upstream tomcat1 {server 192.168.25.148:8080;    }    server {        listen       80;        server_name  www.sina.com.cn;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {            proxy_pass   http://tomcat1;            index  index.html index.htm;        }    }    upstream tomcat2 {server 192.168.25.148:8081;    }    server {        listen       80;        server_name  www.sohu.com;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {            proxy_pass   http://tomcat2;            index  index.html index.htm;        }    }

第四步:nginx重新加载配置文件(linux下指令为:sbin/nginx -s reload)

第五步:配置域名

在hosts文件中添加域名和ip的映射关系

192.168.25.148 www.sina.com.cn

192.168.25.148 www.sohu.com


要实现负载均衡(以www.sina.com.cn为例),只需在nginx.xml修改upstream tomcat2配置即可

如下所示

 upstream tomcat2 {server 192.168.25.148:8081;server 192.168.25.148:8082 weight=2;    }

weight为权重,默认是1


原创粉丝点击