nginx反向代理和使用tomcat实现负载均衡

一:使用同一个端口号跳转到不同服务器:
1,配置两个tomcat服务器,也可配置自定义域名
2,打开nginx中F:\nginx\nginx-1.8.1\conf 中的nginx.conf文件:
在最下面建立两个虚拟主机:
server {
        listen       80;
        server_name  8080.wangchi.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass   http://127.0.0.1:8080;
            index  index.html index.htm;
                   }
        }

server {
        listen       80;
        server_name  8081.wangchi.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass   http://127.0.0.1:8081;
            index  index.html index.htm;
                   }
        }
默认监听80端口,server_name表示域名,可以自定义, proxy_pass表示跳转到指定的地址

即可使用nginx反向代理到另一台服务器

nginx优缺点:
1,占内存小,可以使用高并发连接,处理速度快
2,可以实现http服务器,虚拟主机,反向代理,负载均衡
3,配置简单,
4,不暴露真实服务器地址
二:使用nginx+tomcat实现负载均衡:
nginx作为负载均衡服务器,用户请求先到达nginx,再由nginx根据负载均衡的配置请求转发到tomcat服务器
1,添加本地域名:
C:\Windows\System32\drivers\etc  路径下hosts中的文件添加:
127.0.0.1          www.wangchi.com
2,在nginx的配置文件nginx.conf的文件中配置负载均衡的权重,并添加另一个端口:
upstream tomcatserver{
 server 127.0.0.1:8080 weight=2;
 server 127.0.0.1:8081 weight=1;
 } 
 server {
        listen       80;
        server_name  www.wangchi.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass   http://tomcatserver;
            index  index.html index.htm;
                   }
        }
weight表示权重,默认为1权重越高表示请求的次数越多

猜你喜欢

转载自blog.csdn.net/wjc2013481273/article/details/80813634