linux基础服务器架设nginx 反向代理&负载均衡

---恢复内容开始---

一、反向代理的概念

在计算机网络中,反向代理是代理服务器的一种。服务器根据客户端的请求,从其关系的一组或多组后端服务器(如Web服务器)上获取资源,然后再将这些资源返回给客户端,
客户端只会得知反向代理的IP地址,而不知道在代理服务器后面的服务器的存在。与前向代理不同,前向代理作为客户端的代理,将从互联网上获取的资源返回给一个或多个的客
户端,服务端(如Web服务器)只知道代理的IP地址而不知道客户端的IP地址;而反向代理是作为服务器端(如Web服务器)的代理使用,而不是客户端。客户端借由前向代理可
以间接访问很多不同互联网服务器的资源,而反向代理是供很多客户端都通过它间接访问不同后端服务器上的资源,而不需要知道这些后端服务器的存在,而以为所有资源都来自
于这个反向代理服务器
二、环境准备
Nginx:10.67.51.39
httpd1:10.67.50.51
httpd2:10.67.50.52
2.1、yum 安装httpd服务,并写入测试档信息!

2.2、修改http的默认端口,改为8080,并启动服务!

2.3、重启服务,并确认服务端口!

2.4、分别在Web页面写下显示内容,并访问测试!分别截图如下!

2.41、10.67.50.52  http

 

 

2.42、10.67.50.51 http

 

 三、nginx实验架构,并测试检测

3.1、下图为我测试时Nginx反向代理实验,这里弄两个Httpd aphche 是为了后面做负载均衡的实验!

3.2、安装nginx,因为nginx 也是80端口,故在安装nginx之前,请确认服务器的80端口是否被占用,如被占用,nginx安装完以后,服务无法启动!

下面我们安装nginx,以下为我安装的nginx版本信息!

配置nginx,修改nginx 的配置文件,下面标注颜色的部分,为我的配置项!

[root@zabbix ~]# vim /etc/nginx/nginx.conf

# Load dynamic modules. See /usr/share/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { 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; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information. include /etc/nginx/conf.d/*.conf; gzip on; gzip_min_length 1000; gzip_types text/plain text/css application/x-javascript; upstream tomcatserver1 { server 10.67.50.51:8080; } #指向被代理服务器,upstream关键字 upstream tomcatserver2 { server 10.67.50.52:8080; } server { listen 80; #设置监听端口80 server_name 8080.chenwang.com; #设置域名 #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://tomcatserver1; #将单向代理指向upstream 关键字后面的说明 index index.html index.htm; } } server { listen 80; server_name 8080.chenwang2.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://tomcatserver2;
index index.html index.htm;
}
}
}

3.3、测试反向代理

因为我们设置的域名为随机指定的,为加入DNS,所以我们需要在测试的终端user放,修改本地hosts文件,将域名与IP地址进行对应!

我这里用windows系统终端测试,修改配置文件!加入标记的两行!

 

3.32、在浏览器中进行测试。输入8080.chenwang2.com/,显示10.67.50.52:8080的内容

在浏览器中输入8080.chenwang.com,显示10.67.50.51:8080的内容

到这里我们就完成反向代理服务器的搭建!!!!!!!!!!

 

 

第二部分  我们进行nginx负载均衡几种模式的测试

Nginx的负载均衡氛围以下几种,下面我们分别介绍一下!

 1.1 轮询分派

按照默认轮询的方式进行负载, 假设后端server down掉,能自己剔除。

缺点:可靠性地,负载不均衡,机器性能可能不一致

upstream tomcatserver1 {
server 10.67.50.51:8080;
server 10.67.50.52:8080;             #这里使用轮询算法,访问 :8080.chenwang.com  ,轮流出现后台Web
}  
upstream tomcatserver2 {
server 10.67.50.52:8080;
}
server {
listen 80;
server_name 8080.chenwang.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://tomcatserver1;
index index.html index.htm;
}
}
server {
listen 80;
server_name 8080.chenwang2.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://tomcatserver2;
index index.html index.htm;
}
}

1.2 、权重分派

考虑1比2的机器配置低,或者1不如2的性能时候 这样将2的权重设置大一些,更多的请求会被分配到2上。

为1分担更多的请求。

 upstream tomcatserver1 {
        server 10.67.50.51:8080 weight=1;       #这里采用权重分配,权重为1:2,分别显示内容!
        server 10.67.50.52:8080 weight=2;
    }   
    upstream tomcatserver2 {
        server 10.67.50.52:8080;
    }   
   server {
        listen       80;
        server_name  8080.chenwang.com;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            proxy_pass   http://tomcatserver1;
            index  index.html index.htm;
        }   
    }   
    server {
        listen       80;
        server_name  8080.chenwang2.com;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            proxy_pass   http://tomcatserver2;
            index  index.html index.htm;
        }   
    }   

1.3、IP hash

这里的IP说的是客户端的出口IP,这样经过 des_server_ip = hash(ip) 相应的ip在没有down掉的情况下,肯定会hash到固定的ip上。

Nginx中的ip_hash技术能够将某个ip 的请求定向到同一台后端web机器中,这样一来这个ip 下的客户端和某个后端 web机器就能建立起稳固的session.

Ip_hash机制缺陷:

(1).nginx不是最前端的服务器

ip_hash要求nginx一定是最前端的服务器,否则nginx得不到正确ip,就不能根据ip作hash. Eg: 使用的是squid为最前端.那么nginx取ip时只能得到squid的服务器ip地址,用这个地址来作分流肯定是错乱的

(2).nginx的后端还有其它负载均衡

假如nginx后端还有其它负载均衡,将请求又通过另外的方式分流了,那么某个客户端的请求肯定不能定位到同一台session应用服务器上,这么算起来,nginx后端只能直接指向应用服务器,或者再搭一人squid,然后指向应用服务器. 最好 的办法是用location作一次分流,将需要session的部分请求通过ip_hash分流,剩下的走其它后端去.

 upstream tomcatserver1 {
        ip hash;
        server 10.67.50.51:8080;
        server 10.67.50.52:8080;       #ip hash 
    }
    upstream tomcatserver2 {
        server 10.67.50.52:8080;
    }
   server {
        listen       80;
        server_name  8080.chenwang.com;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            proxy_pass   http://tomcatserver1;
            index  index.html index.htm;
        }
    }
    server {
        listen       80;
        server_name  8080.chenwang2.com;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            proxy_pass   http://tomcatserver2;
            index  index.html index.htm;
        }
    }

1.4、url hash

按照URI进行哈希,固定的URI Hash到固定的server上。

upstream tomcatserver1 {
        server 10.67.50.51:8080;
        server 10.67.50.52:8080;       #url hash 
        hash $request_uri;
        hash_method crc32;
    }

1.5、fair 性能相应时间分派

性能,相应时间分派: 按后端服务器的响应时间来分配请求。

响应时间短的优先分配。

upstream tomcatserver1 {
        ip hash;
        server 10.67.50.51:8080;
        server 10.67.50.52:8080;      
        fair;
    }

 

 

以上即Nginx负载均衡的几种方式,还有设置备源机的方式,我就不一一举例了!

 

  

 

 

 

 

 

 

 

posted @ 2019-01-15 11:25  Mr_henry_chen  阅读(173)  评论(0编辑  收藏  举报