Nginx反向代理与负载均衡

介绍一下使用Nginx反向代理来做负载均衡,只需要配置upstream并选择负载均衡策略,再配置server反向代理功能就全部搞定了。

1.架设Nginx服务

直接使用Docker启动一台虚拟Nginx服务器。

$ sudo docker run --net=pub_net --ip=192.168.1.13 --name macvlan_ngx1 --restart always -d nginx

 

2.配置Nginx服务——反向代理与负载均衡

Nginx的主配置文件位于/etc/nginx/nginx.conf中。默认内容如下:

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

 

3.

以下给出一份配置文件,主要用于NginxTomcat转发的负载均衡。我在Docker中虚拟出了5台Tomcat服务器(192.168.1.[19-23])。其中,由两台Tomcat服务器组成了Web Server 1服务器组(采用ip_hash算法,就是根据ip做转发)。用3台Tomcat组成了Web Server 2服务器组(设置服务器转发权重)。
e.g.

upstream webserver1 {
    ip_hash;
    server 192.168.1.19:8080;
    server 192.168.1.20:8080;
}

upstream webserver2 {
    server 192.168.1.21:8080 weight=5;
    server 192.168.1.22:8080 weight=3;
    server 192.168.1.23:8080 weight=2;
}

server {
    listen 80;
    server_name tomcat1.com tomcat2.com;

    location / {
        proxy_pass http://webserver1;
    }
    location /hello {
        proxy_pass http://webserver1/hello;
    }
}

server {
    listen 80;
    server_name tomcat3.com tomcat4.com tomcat5.com;

    location / {
        proxy_pass http://webserver2;
    }
    location /world {
        proxy_pass http://webserver2/world;
    }
}

server {
    listen 80;
    server_name pc.com;

    location / {
		proxy_pass https://www.baidu.com/;
    }
}

 

可以很明显的看到/etc/nginx/conf.d/*.conf中的所有.conf后缀的都会被自动加载作为配置文件。为此,自定义配置文件时,直接在conf.d目录下新建后缀为.conf的文件即可。

4.

一些注意事项

有几点需要注意的,Nginx收到请求之后,会获取请求的Host字段,然后根据Host内容匹配server_name然后转发给不同的server。转发时直接使用proxy_pass指令即可,路径中可以写upstream以实现负载均衡。

5.

配置hosts文件

由于,这里只启动了一台IP为192.168.1.13的Nginx服务器,那么在我的开发机(Windows 10,192.168.1.2)上的C:\Windows\System32\drivers\etc\hosts文件中可以做以下映射。

192.168.1.13 tomcat1.com
192.168.1.13 tomcat2.com
192.168.1.13 tomcat3.com
192.168.1.13 tomcat4.com
192.168.1.13 tomcat5.com
192.168.1.13 pc.com

6.

浏览器访问Nginx服务器

好了,测试Nginx配置文件的有效性(这里-t后不需要添加任何内容,不要写成nginx -t /etc/nginx/nginx.conf):

# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重新载入Nginx

# nginx -s reload
2018/10/28 14:18:44 [notice] 24#24: signal process started

此时,就可以在开发机的浏览器中访问Nginx服务器,查看具体的反向代理与负载均衡效果了:

http://tomcat1.com/
http://tomcat2.com/hello
http://tomcat3.com/world
http://pc.com/

 

小结:

正向代理隐藏真实客户端,反向代理隐藏真实服务端。

nginx 的配置

常用三个命令 
start nginx 
nginx -s reload #修改配置后载入新的配置 
nginx -s stop

1.单域名映射到多服务

server {
        listen       80;
        server_name  www.demo.com;
        location /sewage/ {
            proxy_pass http://192.168.0.66:32322/;
        }
        location /sewageserver/ {
            proxy_pass http://192.168.0.66:32321/;
        } 
        location /abc/ {
            proxy_pass http://192.168.0.66:9004/;
        }
        location / {
            proxy_pass http://192.168.0.66:9000/;
        }       
    }
 

2.多域名映射到多服务

 server {
        listen       80;
        server_name  pro.letv.com;

        location / {
            proxy_pass http://192.168.0.66:9000/;
        }     
    }
    server {
        listen       80;
        server_name  vid.atm.youku.com;

        location / {
            proxy_pass http://192.168.0.66:9999/;
        }     
    }
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值