web前端

反向代理与负载均衡

2022-05-23  本文已影响0人  姜治宇

反向代理

当客户端输入一个网址时,先需要进行dns解析,找到对应的ip服务器地址,到达nginx服务器,然后nginx服务器将请求转发到相应的node应用服务器。
当node服务器处理完请求后,将资源返回到nginx服务器,然后由nginx将资源再回传给客户端。
从这个过程可以看出,nginx本身不处理业务,只是充当了中介代理的作用,因此叫反向代理。
nginx配置反向代理非常简单,利用proxy_pass可以配置代理服务器,可以是http://www.site.com这样的域名,也可以是ip地址。

worker_processes  1;
events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;

        location / {
            proxy_pass http://192.168.1.133; #如果是域名一定要带www,否则会是302,不支持https
            # root   html; #配置了反向代理proxy_pass,root这些就没用了
            # index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

}

负载均衡

常用的负载均衡策略是轮询,配置高的主机可以给予较高权重,这样轮询的次数也会增加。

worker_processes  1;
events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    upstream mysite {
       server 192.168.1.100 weight=8;
       server 192.168.1.101 weight=3;
      server 192.168.1.102  weight=1 backup;  #备份服务器,当前面两台服务器崩掉后转发到这里
      
    }
    server {
        listen       80;
        server_name  localhost;

        location / {
            proxy_pass http://mysite; #随便起名
            #root   html;
            #index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

}

上一篇下一篇

猜你喜欢

热点阅读