【Web 集群实战】19_Nginx 反向代理与负载均衡

版权声明: https://blog.csdn.net/weixin_42061048/article/details/82973272

【Web 集群实战】19_Nginx 反向代理与负载均衡

标签(空格分隔): Web集群实战


1. 集群简介

1.1 集群特点

  • 高性能
  • 价格有效性
  • 可伸缩性(Scalability)
  • 高可用性(Availability)
  • 透明性(Transparency)
  • 可管理性(Manageability)
  • 可编程性(Programmability)

1.2 集群的常见分类

  • 负载均衡集群(Load Balancing clusters),简称 LBC 或 LB
  • 高可用性集群(High-availability(HA) clusters),简称 HAC
  • 高性能计算集群(High-performance(HP) clusters),简称 HPC
  • 网格计算(Grid computing)集群

2. Nginx 负载均衡集群

2.1 反向代理与负载均衡概念简介

  • 严格地说,Nginx 仅仅是作为 Nginx Proxy 反向代理使用的,因为这个反向代理功能表现的效果是负载均衡集群的效果,所以称为 Nginx 负载均衡。

  • 普通的负载均衡软件,如 LVS,其实现的功能只是对请求数据包的转发(也可能会改写数据包)、传递,其中 DR 模式从负载均衡下面的节点服务器来看,接收到的请求还是来自访问负载均衡器的客户端的真实用户,而反向代理接收访问用户的请求后,会代理用户重新发起请求代理下的节点服务器,最后把数据返回给客户端用户,在节点服务器看来,访问的节点服务器的客户端用户就是反向代理服务器了,而非真实的网站访问用户。

  • 简单的说,LVS 等的负载均衡是转发用户请求的数据包,而 Nginx 反向代理是接收用户的请求然后重新发起请求去请求其后面的节点。

2.2 实现 Nginx 负载均衡的组件

Nginx http 功能模块 模块说明
ngx_http_proxy_module proxy 代理模块,用于把请求后抛给服务器节点或 upstream 服务器池
ngx_http_upstream_module 负载均衡模块,可实现网站的负载均衡功能及节点的健康检查

2.3 快速实践 Nginx 负载均衡准备

  • 硬件准备
HOSTNAME IP 说明
lb001 192.168.2.147 Nginx 主负载均衡器
lb002 192.168.2.148 Nginx 辅负载均衡器
web001 192.168.2.145 web001 服务器
web002 192.168.2.146 web002 服务器
web003 192.168.2.150 web003 服务器
  • 安装 Nginx 软件

【Web 集群实战】06_Nginx 安装与虚拟主机配置

  • Nginx web001 和 web002 的配置如下:
[root@web002 conf]# cat nginx.conf
worker_processes  1;
error_log logs/error.log;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
include extra/bbs.conf;
include extra/www.conf;
}

[root@web002 conf]# cat extra/www.conf
server {
        listen       80;
        server_name  www.yangyangyang.org;
    location / {
        root   html/www;
        index  index.html index.htm;
    }
        access_log logs/access_www.log;
}

[root@web002 conf]# cat extra/bbs.conf
#bbs virtualhost by ylt
server {
        listen       80;
        server_name  bbs.yangyangyang.org;
    location / {
        root   html/bbs;
        index  index.html index.htm;
    }
}
  • 配置完成后检查语法,并启动 Nginx 服务
[root@web002 conf]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.14.0//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.14.0//conf/nginx.conf test is successful
[root@web002 conf]# /application/nginx/sbin/nginx
  • 填充测试文件数据
[root@web001 html]# cat www/index.html
192.168.2.145 www
[root@web001 html]# cat bbs/index.html
192.168.2.145 bbs

[root@web002 html]# cat bbs/index.html
192.168.2.146 bbs
[root@web002 html]# cat www/index.html
192.168.2.146 www
  • 测试 web001
[root@lb001 ~]# tail -1 /etc/hosts
192.168.2.145 www.yangyangyang.org bbs.yangyangyang.org
[root@lb001 ~]# curl www.yangyangyang.org
192.168.2.145 www
[root@lb001 ~]# curl bbs.yangyangyang.org
192.168.2.145 bbs
  • 测试 web002
[root@lb001 ~]# tail -1 /etc/hosts
192.168.2.146 www.yangyangyang.org bbs.yangyangyang.org
[root@lb001 ~]# curl www.yangyangyang.org
192.168.2.146 www
[root@lb001 ~]# curl bbs.yangyangyang.org
192.168.2.146 bbs

2.4 实现一个简单的负载均衡

[root@lb001 conf]# cat nginx.conf
worker_processes  1;
error_log logs/error.log;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream www_server_pools {
                server 192.168.2.145:80 weight=1;
                server 192.168.2.146:80 weight=1;
        }
        server {
                listen 80;
                server_name www.yangyangyang.org;
                location / {
                        proxy_pass http://www_server_pools;
                }
        }
}
  • 配置完成后检查语法,并启动 Nginx 服务
[root@web002 conf]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.14.0//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.14.0//conf/nginx.conf test is successful
[root@web002 conf]# /application/nginx/sbin/nginx
  • 检查负载均衡测试结果
[root@lb001 conf]# tail -1 /etc/hosts
192.168.2.147 www.yangyangyang.org 

[root@lb001 conf]# curl www.yangyangyang.org
192.168.2.145 bbs
[root@lb001 conf]# curl www.yangyangyang.org
192.168.2.146 bbs
[root@lb001 conf]# curl www.yangyangyang.org
192.168.2.145 bbs
[root@lb001 conf]# curl www.yangyangyang.org
192.168.2.146 bbs

2.5 反向代理多虚拟主机节点

  • 上述示例反向代理向下面节点重新发起请求时,默认没有在请求头里告诉节点服务器要找哪台虚拟主机,所以,web 节点服务器接收到请求后就把节点服务器的第一个虚拟主机发给反向代理了。

  • 在 Nginx 代理 WWW 服务虚拟主机配置里增加如下一行配置即可:

proxy_set_header X-Forwarded-For $remote_addr;
worker_processes  1;
error_log logs/error.log;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream www_server_pools {
                server 192.168.2.145:80 weight=1;
                server 192.168.2.146:80 weight=1;
        }
        server {
                listen 80;
                server_name www.yangyangyang.org;
                location / {
                        proxy_pass http://www_server_pools;
                        proxy_set_header Host $host;
                }
        }
}
  • 重新加载 Nginx 服务,并用 curl 测试检查:
[root@web002 conf]# /application/nginx/sbin/nginx -s reload
[root@lb001 conf]# curl www.yangyangyang.org
192.168.2.145 www
[root@lb001 conf]# curl www.yangyangyang.org
192.168.2.146 www

2.6 反向代理后的节点服务器记录用户 IP

  • 节点服务器对应的 WWW 虚拟主机的访问日志的第一个字段记录的并不是客户端的 IP,而是反向代理服务器的 IP
[root@web001 logs]# tail -2 access_www.log
192.168.2.147 - - [10/Oct/2018:22:45:50 +0800] "GET / HTTP/1.0" 200 36 "-" "curl/7.29.0"
192.168.2.147 - - [10/Oct/2018:22:45:52 +0800] "GET / HTTP/1.0" 200 36 "-" "curl/7.29.0"
  • 需要增加一行参数
    proxy_set_header X-Forwarded-For $remote_addr;
worker_processes  1;
error_log logs/error.log;
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"';
    sendfile        on;
    keepalive_timeout  65;
    upstream www_server_pools {
                server 192.168.2.145:80 weight=1;
                server 192.168.2.146:80 weight=1;
        }
        server {
                listen 80;
                server_name www.yangyangyang.org;
                location / {
                        proxy_pass http://www_server_pools;
                        proxy_set_header Host $host;
                        proxy_set_header X-Forwarded-For $remote_addr;
                }
        }
}
  • 重新加载 Nginx 反向代理服务:
[root@lb001 conf]# /application/nginx/sbin/nginx -s reload

2.7 根据 URL 中的目录地址实现代理转发

背景

  • 通过 Nginx 实现动静分离,即通过 Nginx 反向代理配置规则实现让动态资源和静态资源及其他业务分别由不同的服务器解析,以解决网站性能、安全用户体验等重要问题。

需求分析

配置

  • static_pools 为静态服务器池,有一个服务器,地址为 192.168.2.145,端口为 80。

  • upload_pools 为上传服务器池,有一个服务器,地址为 192.168.2.146,端口为 80。

  • default_pools 为默认的服务器池,即动态服务器,有一个服务器,地址为 192.168.2.150,端口为 80。

  • Nginx 反向代理的实际配置如下:

    扫描二维码关注公众号,回复: 3545952 查看本文章
[root@lb001 conf]# cat nginx.conf
worker_processes  1;
error_log logs/error.log;
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"';
    sendfile        on;
    keepalive_timeout  65;
    upstream static_pools {
        server 192.168.2.145:80 weight=1;
    }
    upstream upload_pools {
        server 192.168.2.146:80 weight=1;
    }
    upstream default_pools {
        server 192.168.2.148:80 weight=1;
    }
    server {
        listen 80;
        server_name www.yangyangyang.org;
        location / {
                proxy_pass http://default_pools;
                include proxy.conf;
        }
        location /static/ {
                proxy_pass http://static_pools;
                include proxy.conf;
        }
        location /upload/ {
                proxy_pass http://upload_pools;
                include proxy.conf;
        }
    }
}

[root@lb001 conf]# /application/nginx/sbin/nginx
[root@lb001 conf]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.14.0//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.14.0//conf/nginx.conf test is successful
[root@lb001 conf]# /application/nginx/sbin/nginx -s reload
  • web001 节点配置及测试
[root@web001 ~]# tail -1 /etc/hosts
192.168.2.145 www.yangyangyang.org bbs.yangyangyang.org

[root@web001 ~]# /application/nginx/sbin/nginx
[root@web001 ~]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.14.0//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.14.0//conf/nginx.conf test is successful
[root@web001 ~]# /application/nginx/sbin/nginx -s reload

[root@web001 ~]# cd /application/nginx/html/www/
[root@web001 www]# mkdir -p static
[root@web001 www]# echo static_pools >static/index.html
[root@web001 www]# cat static/index.html
static_pools
[root@web001 www]# curl www.yangyangyang.org/static/index.html
static_pools
  • web002 节点配置及测试
[root@web002 ~]# /application/nginx/sbin/nginx
[root@web002 ~]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.14.0//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.14.0//conf/nginx.conf test is successful
[root@web002 ~]# /application/nginx/sbin/nginx -s reload

[root@web002 ~]# tail -1 /etc/hosts
192.168.2.146 www.yangyangyang.org bbs.yangyangyang.org

[root@web002 ~]# cd /application/nginx/html/www/
[root@web002 www]# mkdir -p upload
[root@web002 www]# echo upload_pools >upload/index.html
[root@web002 www]# cat upload/index.html
upload_pools
[root@web002 www]# curl www.yangyangyang.org/upload/index.html
upload_pools
  • web003 节点配置及测试
[root@web003 ~]#  /application/nginx/sbin/nginx
[root@web003 ~]# nginx: the configuration file /application/nginx-1.14.0//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.14.0//conf/nginx.conf test is successful
[root@web003 ~]#  /application/nginx/sbin/nginx -s reload

[root@web003 ~]# tail -1 /etc/hosts
192.168.2.150 www.yangyangyang.org bbs.yangyangyang.org

[root@web003 ~]# cd /application/nginx/html/www/
[root@web003 www]# echo default_pools >index.html
[root@web003 www]# cat index.html
default_pools
[root@web003 www]# curl www.yangyangyang.org
default_pools
  • 在浏览器客户端的 hosts 文件里把 www.yangyangyang.org 解析到 Nginx 反向代理服务器的 IP,然后访问上述 URL

static

upload

default

猜你喜欢

转载自blog.csdn.net/weixin_42061048/article/details/82973272
今日推荐