Nginx反向代理和负载均衡

Nginx通过proxy模块实现反向代理功能。在作为web反向代理服务器时,nginx负责接收客户请求,并能够根据URI、客户端参数或其它的处理逻辑将用户请求调度至上游服务器上(upstream server)。nginx在实现反向代理功能时的最重要指令为proxy_pass,它能够将location定义的某URI代理至指定的上游服务器(组)上。

环境:(玩不转的请自觉关闭防火墙和SELinux,~_~!)
Nginx 192.168.66.129 部署Nginx
server1 192.168.66.130 部署httpd,用来充当后端服务器
server2 192.168.66.131 部署httpd,用来充当后端服务器

Nginx安装
1.Nginx官网http://nginx.org/en/download.html下载地址,下载所需要的安装包。

[root@localhost ~]# yum install -y gcc openssl-devel pcre-devel zlib-devel  //安装依赖包
[root@localhost ~]# tar zxvf /usr/local/src/nginx-1.10.3.tar.gz -C /usr/local/
[root@localhost ~]# useradd -s /sbin/nologin nginx
[root@localhost ~]# cd /usr/local/nginx-1.10.3
[root@localhost nginx-1.10.3]# ./configure --prefix=/usr/local/ --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre
[root@localhost nginx-1.10.3]# make ;make install

server1、2上直接yum安装httpd,并分别设置测试网页,测试内容为server1、server2。
在server1、2上分别执行:

[root@server1 ~]# yum install -y httpd
[root@server1 ~]# echo server1 > /var/www/html/index.html
[root@server1 ~]# systemctl start httpd

下面开始Nginx反向代理负载均衡

[root@localhost ~]# cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak  //备份
[root@localhost ~]# vim /etc/nginx/nginx.conf
#user  nobody;        //Nginx运行的用户
worker_processes  1;  //设置Nginx的进程数,一般个CPU核数相同

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;   //每个进程所能链接的最大值
}


http {
    include       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  logs/access.log  main;  //用户访问日志

    sendfile        on;  //Linux内存 操作系统和驱动程序运行在内核层,应用程序运行在用户层

    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;   //超时时间

    gzip  on;        //压缩开启


    upstream loyal {   //配置负载均衡
        server 192.168.66.130 weight=1 max_fails=3 fail_timeout=2;
        server 192.168.66.131 weight=1 max_fails=3 fail_timeout=2;
    }

    server {   //虚拟主机配置
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://loyal;   //代理配置
            proxy_set_header X-Real-IP $remote_addr;
            #root   html;
            #index  index.html index.htm;
        }
......
[root@localhost ~]# nginx -t  //测试配置文件修改是否有问题
[root@localhost ~]# nginx    //启动Nginx,nginx -s reload重新加载(重启)
[root@localhost ~]# curl 192.168.66.129
server1
[root@localhost ~]# curl 192.168.66.129
server2     //访问Nginx,发现得到的是server1、2测试页面,并且每个页面轮换出现

查看server1、2上/var/log/httpd/access.log日志,可以看到访问的IP是Nginx的IP,但访问获得的页面却是server1和server2。由此可以说明,Nginx在代理server1、2。在上面upstream中,权重分别都是1,所以两个页面会轮换出现。

在负载调度中,有以下几种调度算法:

  • Round Robin: 对所有的backend轮训发送请求,算是最简单的方式了,也是默认的分配方式;
  • Least Connections(least_conn): 跟踪和backend当前的活跃连接数目,最少的连接数目说明这个backend负载最轻,将请求分配给他,这种方式会考虑到配置中给每个upstream分配的weight权重信息;
  • Least Time(least_time): 请求会分配给响应最快和活跃连接数最少的backend;
  • IP Hash(ip_hash): 对请求来源IP地址计算hash值,IPv4会考虑前3个octet,IPv6会考虑所有的地址位,然后根据得到的hash值通过某种映射分配到backend;
  • Generic Hash(hash): 以用户自定义资源(比如URL)的方式计算hash值完成分配,其可选consistent关键字支持一致性hash特性;

猜你喜欢

转载自blog.csdn.net/weini1111/article/details/77939903