nginx!

一、端口

通过配置nginx下的nginx.conf 文件中的端口,达到重复使用同一个ip的目的。

二、域名

通过配置nginx下的nginx.conf 文件中的80端口,配置server_name 为不用的域名 如:

    server {
        listen       80;
        server_name  www.baidu.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   htmltaotao;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
      }
达到使用不同域名,同一端口访问不同网站的目的。

此时通过使用switchhosts 配置本地host文件后,在访问www.baidu.com就是访问给定IP下的资源。

三、反向代理

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;

        }

    }

其中,蓝色字体处需填写一致,且需要在本地host中添加映射关系。此时访问www.sina.com.cn或者www.sohu.com,访问的就是所配置的ip+端口下的tomcat首页。

四、负载均衡

如果一个服务由多台服务器提供,需要把负载分配到不同的服务器处理,需要负载均衡。

 upstream tomcat2 {

        server 192.168.25.148:8081;

        server 192.168.25.148:8082;

  }

1、默认的负载均衡的策略就是轮询的方式。(如果第一次访问转发的是8081,那么第二次就是8082,第三次又到8081,...,2n-1次为8081,2n次为8082,此为轮询方式)

2、根据服务器的实际使用情况调整权重(weight)。权重越高分配的请求越多,权重越低,请求越少。默认是都是1

 upstream tomcat2 {

        server 192.168.25.148:8081;

        server 192.168.25.148:8082 weight=2;

    }

3、其他的负载均衡的策略:1.通过IP地址的hash值 做映射。2.通过URL的方式计算出Hash值 。3.随机策略。4.最少并发量。

五、高可用

使用keepalived+nginx实现主备。

(备份服务器发送心跳包)

猜你喜欢

转载自blog.csdn.net/qq_34480270/article/details/85112360