1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Nginx之反向代理与负载均衡实现动静分离实战

Nginx之反向代理与负载均衡实现动静分离实战

时间:2020-06-04 05:14:08

相关推荐

Nginx之反向代理与负载均衡实现动静分离实战

Nginx之反向代理与负载均衡实现动静分离实战

什么是反向代理与负载均衡

Nginx仅仅作为Nginx proxy反向代理使用的,因为这个反向代理功能表现的效果是负载均衡集群的效果。

负载均衡指的是对请求数据包的转发,从负载均衡下面的节点服务器来看,接收到的请求还是来自访问负载均衡器的客户端的真实用户,而反向代理服务器指的是接收到用户的请求后,会代理用户重新发起请求代理下的节点服务器,最后把数据返回给客户端用户,在节点服务器看来,范文的节点服务器的客户端用的就是反向代理服务器了,而非真实的网站访问用户。

Nginx负载均衡核心组件介绍

一、实验目标

实战1:配置基于域名虚拟主机的web节点

实战2:实现代理服务器携带主机头和记录用户IP

实战3:根据URL中的目录地址实现代理转发

实战4:Nginx负载均衡检测节点状态

二、实验环境

实验拓扑

三、实验步骤

1、Nginx的安装------四台主机都是怎么安装

[root@yu61 ~]#service httpd stop

[root@yu61 ~]#service iptables stop

[root@yu61 ~]#yum install pcre pcre-devel openssl openssl-devel -y

[root@yu61 ~]# mkdir -p /home/yu/tools

[root@yu61 ~]# cd /home/yu/tools/

[root@yu61 tools]# wget -q /download/nginx-1.6.3.tar.gz

[root@yu61 tools]# ls

nginx-1.6.3.tar.gz

[root@yu61 tools]# useradd nginx -s /sbin/nologin -M

[root@yu61 tools]# tar xf nginx-1.6.3.tar.gz

[root@yu61 tools]# cd nginx-1.6.3

[root@yu61 nginx-1.6.3]# ./configure --user=nginx --group=nginx --prefix=/application/nginx-1.6.3 --with-http_stub_status_module --with-http_ssl_module

[root@yu61 nginx-1.6.3]#make -j 4 && make install

[root@yu61 nginx-1.6.3]# ln -s /application/nginx-1.6.3/ /application/nginx

Nginx负载均衡实战

实战1:配置基于域名虚拟主机的web节点

1、修改配置文件------在两台web服务器上修改

[root@yu61 nginx-1.6.3]# cd /application/nginx/conf/

[root@yu61 conf]# egrep -v '#|^$' nginx.conf.default > nginx.conf

[root@yu63 conf]# cat nginx.conf

worker_processes 1;

events {

worker_connections 1024;

}

http {

include mime.types;

default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"';

server {

listen 80;

server_name;

location / {

roothtml/bbs;

index index.html index.htm;

}

access_log logs/access.log main;

}

}

server {

listen 80;

server_name;

location / {

roothtml/www;

index index.html index.htm;

}

access_log logs/access.log main;

}

}

}

2、检查语法和启动nginx服务----------四台主机都做

[root@yu61 conf]# mkdir /application/nginx/html/{www,bbs}

[root@yu61 conf]# /application/nginx/sbin/nginx -t

nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok

nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful

[root@yu61 conf]# /application/nginx/sbin/nginx

[root@yu61 conf]# netstat -anutp | grep nginx

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 48817/nginx

3、添加可测试内容-------两台web主机都做,测试用可以只做一台

[root@yu63 conf]# echo '192.168.1.63 www' > ../html/www/index.html

[root@yu63 conf]# echo '192.168.1.63 bbs' > ../html/bbs/index.html

[root@yu63 conf]# cat /application/nginx/html/www/index.html

192.168.1.63 www

[root@yu63 conf]# cat /application/nginx/html/bbs/index.html

192.168.1.63 bbs

[root@yu64 conf]# echo '192.168.1.64 www' > ../html/www/index.html

[root@yu64 conf]# echo '192.168.1.64 bbs' > ../html/bbs/index.html

[root@yu64 conf]# cat /application/nginx/html/www/index.html

192.168.1.64 www

[root@yu64 conf]# cat /application/nginx/html/bbs/index.html

192.168.1.64 bbs

4、用curl测试web端

[root@yu61 conf]# tail -2 /etc/hosts

192.168.1.63 bbs

192.168.1.63 www

[root@yu61 conf]# scp /etc/hosts /etc/hosts 192.168.1.63:/etc/hosts

[root@yu61 conf]# curl

192.168.1.63 www

[root@yu61 conf]# curl

192.168.1.63 bbs

实战2:实现代理服务器携带主机头和记录用户IP

1)Upstream模块的介绍

Nginx的负载均衡功能依赖于ngx_http_proxy_module模块,所支持代理方式包括proxy_pass(代理)、fastcgi_pass(PHP/JAVA)、memcache_pass(缓存)等。

ngx_http_proxy_module模块允许Nginx定义一组或度组节点服务器组,使用时可以通过pass_pass代理方式吧网站的请求发送到实现定义好ID对应upstream组的名字上。

负载均衡模块用于从”upstream”指令定义的后端主机列表中选取一台主机。Nginx先使用负载均衡模块找到一台主机,再使用upstream模块实现与这台主机的交互。

2)Upstream模块语法:

upstream www_server_pools #upstream是关键字必须要有,www_server_pools是upstream集群组的名字,可以自定义。

server 192.168.1.63:80 weight=1; #server是关键字,这是固定的,后面可以接域名或者ip地址,如果布置点端口,默认是80端口,weight是权重,数值越大被分配的请求越多,结尾是分号。

3)Upstream模块相关说明

Upstream模块的内容应放于http{}标签中,其默认调度算法是wrr,权重轮询。

Upstream模块内部server标签参数说明

4)upstream模块3种常用的调度算法方式

Nginx的upstream支持5种分配方式。其中,前三种为Nginx原生支持的分配方式,后两种为第三方支持的分配方式:

(1)、rr轮询

轮询是upstream的默认分配方式,即每个请求按照时间顺序轮流分配到不同的后端服务器,如果某个后端服务器down掉后,能自动剔除。按照1:1轮询。

upstream backend {

server 192.168.1.101:88;

server 192.168.1.102:88;

}

(2)、wrr轮询。

轮询的加强版,即可以指定轮询比率,weight和访问几率成正比,主要应用于后端服务器异质的场景下。

upstream backend {

server 192.168.1.101 weight=1;

server 192.168.1.102 weight=2;

server 192.168.1.103 weight=3;

}

(3)、ip_hash

每个请求按照访问ip(即Nginx的前置服务器或者客户端IP)的hash结果分配,这样每个访客会固定访问一个后端服务器,可以解决session一致问题。

upstream backend {

ip_hash;

server 192.168.1.101:81;

server 192.168.1.102:82;

server 192.168.1.103:83;

}

(4)、fair

fair顾名思义,公平地按照后端服务器的响应时间(rt)来分配请求,响应时间短即rt小的后端服务器优先分配请求。

upstream backend {

server 192.168.1.101;

server 192.168.1.102;

fair;

}

(5)、url_hash

与ip_hash类似,但是按照访问url的hash结果来分配请求,使得每个url定向到同一个后端服务器,主要应用于后端服务器为缓存时的场景下。

upstream backend {

server 192.168.1.101;

server 192.168.1.102;

hash $request_uri;

hash_method crc32;

}

注意:

_method为使用的hash算法,需要注意的是:server语句中不能加weight等参数。后边两种目前作了解即可。

5)http proxy模块参数

1、修改nginx主从服务器------两台负载均衡器主机都要修改

[root@yu61 conf]# cat nginx.conf

worker_processes 1;

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.1.63:80 weight=1;

server 192.168.1.64:80 weight=1;

}

server {

listen 80;

server_name ;

location / {

proxy_pass http://www_server_pools;

}

}

}

2、测试负载均衡

[root@yu61 conf]# ../sbin/nginx -t

nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok

nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful

[root@yu61 conf]# ../sbin/nginx -s reload

[root@yu61 conf]# cat /etc/hosts

192.168.1.61

[root@yu61 conf]# curl

192.168.1.63 bbs

[root@yu61 conf]# curl

192.168.1.64 bbs

[root@yu61 conf]# curl

192.168.1.63 bbs

[root@yu61 conf]# curl

192.168.1.64 bbs

注释:

默认没有在请求头来告诉节点服务器找那台虚拟机主机,所以,web节点服务器接收到请求后发现没有主机头信息,因此,就把节点服务器的第一个虚拟机主机发给看反向代理了(二节点上的第一个虚拟主机放置的第一个是bbs),解决这个问题的办法,就是当反向代理想后重新发起请求时,要携带主机头信息,以明确的告诉节点服务器要找哪一个虚拟主机。

3、修改配置文件-添加代理服务器携带主机头文件配置

[root@yu61 conf]# cat nginx.conf

worker_processes 1;

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.1.63:80 weight=1;

server 192.168.1.64:80 weight=1;

}

server {

listen 80;

server_name ;

location / {

proxy_pass http://www_server_pools;

proxy_set_header Host $host;

}

}

}

4、使用另外一个客户端测试

[root@yu61 conf]# curl

192.168.1.63 www

[root@yu61 conf]# curl

192.168.1.64 www

[root@yu62 ~]# curl

192.168.1.64 www

[root@yu62 ~]# curl

192.168.1.63 www

[root@yu61 conf]# tail -2 /application/nginx/logs/access.log

192.168.1.61- - [18/May/:19:44:04 +0800] "GET / HTTP/1.1" 200 17 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.3.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2""-"

192.168.1.61- - [18/May/:19:44:05 +0800] "GET / HTTP/1.1" 200 17 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.3.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2""-"

5、再次修改配置文件,添加代理服务器获取用户IP配置

[root@yu61 conf]# cat nginx.conf

worker_processes 1;

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.1.63:80 weight=1;

server 192.168.1.64:80 weight=1;

}

server {

listen 80;

server_name ;

location / {

proxy_pass http://www_server_pools;

proxy_set_header Host $host;

proxy_set_header X-Forwarded-For $remote_addr;

}

}

}

用于查看的客户端需要开启log记录

6、测试

[root@yu64 conf]# tail -2 /application/nginx/logs/access.log

192.168.1.61 - - [18/May/:19:56:19 +0800] "GET / HTTP/1.0" 200 17 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.3.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2""192.168.1.62"

192.168.1.61 - - [18/May/:19:56:19 +0800] "GET / HTTP/1.0" 200 17 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.3.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2""192.168.1.62"

实战3:根据URL中的目录地址实现代理转发

实验拓扑

(1)当用户访问/upload/xxx的时候,代理服务器会吧请求分配到上传服务器池处理数据。

(2)当用户访问/static/xxx的时候,代理服务器会吧请求分配到动态上传服务器池请求数据。

(3)当用户访问//xxx的时候,不包含任何指定的目录地址路径时。代理服务器会把请求分配到默认的动态服务器池请求数据

1、修改配置文件,添加地址池

[root@yu61 conf]# cat nginx.conf

worker_processes 1;

events {

worker_connections 1024;

}

http {

include mime.types;

default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

upstream upload_pools{

server 192.168.1.63:80 weight=1;

}

upstream static_pools{

server 192.168.1.64:80 weight=1;

}

upstream default_pools{

server 192.168.1.62:80 weight=1;

}

server {

listen 80;

server_name ;

location / {

proxy_pass http://default_pools;

proxy_set_header Host $host;

proxy_set_header X-Forwarded-For $remote_addr;

}

location /static/ {

proxy_pass http://static_pools;

proxy_set_header Host $host;

proxy_set_header X-Forwarded-For $remote_addr;

}

location /upload/ {

proxy_pass http://upload_pools;

proxy_set_header Host $host;

proxy_set_header X-Forwarded-For $remote_addr;

}

}

}

2、重启Nginx

[root@yu61 conf]# ../sbin/nginx -t

nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok

nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful

[root@yu61 conf]# ../sbin/nginx -s reload

3、测试静态分离

[root@yu64 www]# cat /etc/hosts

192.168.1.64

192.168.1.64

[root@yu64 nginx]# cd html/www/

[root@yu64 www]# mkdir static

[root@yu64 www]# echo static_pools > static/index.html

[root@yu64 www]# curl /static/index.html

static_pools

[root@yu63 www]# cat /etc/hosts

192.168.1.63

192.168.1.63

[root@yu63 nginx]# cd html/www/

[root@yu63 www]# mkdir upload

[root@yu63 www]# echo upload_pools > upload/index.html

[root@yu63 www]# curl /upload/index.html

upload_pools

[root@yu65 www]# cat /etc/hosts

192.168.1.65

192.168.1.65

[root@yu65 nginx]# cd html/www/

[root@yu65 www]# echo default_pools > index.html

[root@yu65 www]# curl

default_pools

实战4:Nginx负载均衡检测节点状态

1、安装软件

[root@yu61 tools]# pwd

/home/yu/tools

[root@yu61 tools]# wget /yaoweibin/nginx_upstream_check_module/zip/master

[root@yu61 tools]# unzip master

[root@yu61 tools]# cd nginx-1.6.3

[root@yu61 nginx-1.6.3]# patch -p1 < ../nginx_upstream_check_module-master/check_1.5.12+.patch

[root@yu61 nginx-1.6.3]# ./configure --user=nginx --group=nginx --prefix=/application/nginx-1.6.3 --with-http_stub_status_module --with-http_ssl_module --add-module=../nginx_upstream_check_module-master

--add-module=../nginx_upstream_check_module-master

[root@yu61 nginx-1.6.3]# make

[root@yu61 nginx-1.6.3]# mv /application/nginx/sbin/nginx{,.ori}

[root@yu61 nginx-1.6.3]# cp ./objs/nginx /application/nginx/sbin/

2、检测配置文件

[root@yu61 nginx-1.6.3]# /application/nginx/sbin/nginx -t

nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok

nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful

[root@yu61 nginx-1.6.3]# /application/nginx/sbin/nginx -V

nginx version: nginx/1.6.3

built by gcc 4.4.7 0313 (Red Hat 4.4.7-4) (GCC)

TLS SNI support enabled

configure arguments: --user=nginx --group=nginx --prefix=/application/nginx-1.6.3 --with-http_stub_status_module --with-http_ssl_module --add-module=../nginx_upstream_check_module-master

check interval=3000 rise=2 fall=5 timeout=1000 type=http;

3、修改配置文件

[root@yu61 conf]# cat nginx.conf

worker_processes 1;

events {

worker_connections 1024;

}

http {

include mime.types;

default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

upstream static_pools{

server 192.168.1.64:80 weight=1;

server 192.168.1.63:80 weight=1;

check interval=3000 rise=2 fall=5 timeout=1000 type=http;

}

upstream default_pools{

server 192.168.1.62:80 weight=1;

}

server {

listen 80;

server_name ;

location / {

root html;

index index.html index.htm

proxy_pass http://default_pools;

proxy_set_header Host $host;

proxy_set_header X-Forwarded-For $remote_addr;

}

location /status {

check_status;

access_log off;

}

}

}

4、重启服务测试

[root@yu61 conf]# ../sbin/nginx -t

nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok

nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful

[root@yu61 conf]# ../sbin/nginx -s stop

[root@yu61 conf]# ../sbin/nginx

本文转自 于学康 51CTO博客,原文链接:/blxueyuan/1930873,如需转载请自行联系原作者

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。