niginx 负载均衡

时间:2023-10-17 11:15:26

下面是Nginx安装

直接yum install nginx不行,要先处理下源,下面是安装完整流程,十分简单:

1、CentOS 6,先执行:
rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
2,查看yum的nginx信息

[]# yum info nginx

Loaded plugins: fastestmirror

Determining fastest mirrors

* base: mirror.esocc.com

* extras: mirror.esocc.com

* updates: mirror.esocc.com

base                                                     | 3.7 kB     00:00

base/primary_db                                          | 4.4 MB     00:28

extras                                                   | 3.5 kB     00:00

extras/primary_db                                        |  19 kB     00:00

nginx                                                    | 2.9 kB     00:00

nginx/primary_db                                         |  22 kB     00:00

updates                                                  | 3.5 kB     00:00

updates/primary_db                                       | 2.1 MB     00:10

Installed Packages

Name        : nginx

Arch        : x86_64

Version     : 1.4.0

Release     : 1.el6.ngx

Size        : 874 k

Repo        : installed

From repo   : nginx

Summary     : nginx is a high performance web server

URL         : http://nginx.org/

License     : 2-clause BSD-like license

Description : nginx [engine x] is an HTTP and reverse proxy server, as well as

: a mail proxy server

3,安装并启动nignx
[root@server ~]# yum install nginx
[root@server ~]# service nginx start
Starting nginx:                                            [  OK  ]

4,然后进入浏览器,输入http://192.168.0.161/测试,如果看到

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

恭喜你,你成功了!

如果不能连接到nginx,原因很多,但是可以先检查 1,nginx服务是否真的起来了;2,linux服务器防火墙是否打开。

service  iptables  status        查看防火墙状态
service  iptables  start           开启防火墙
service  iptables  stop           关闭防火墙
service  iptables  restart        重启防火墙

下面是Nginx负载均衡配置实例详解

负载均衡

先来简单了解一下什么是负载均衡,单从字面上的意思来理解就可以解释N台服务器平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况。那么负载均衡的前提就是要有多台服务器才能实现,也就是两台以上即可。

测试环境
由于没有服务器,所以本次测试直接host指定域名,然后在VMware里安装了三台CentOS。

测试域名  :a.com

A服务器IP :192.168.5.149 (主)

B服务器IP :192.168.5.27

C服务器IP :192.168.5.126

部署思路
A服务器做为主服务器,域名直接解析到A服务器(192.168.5.149)上,由A服务器负载均衡到B服务器(192.168.5.27)与C服务器(192.168.5.126)上。

域名解析

由于不是真实环境,域名就随便使用一个a.com用作测试,所以a.com的解析只能在hosts文件设置。

打开:C:WindowsSystem32driversetchosts

在末尾添加

192.168.5.149    a.com

保存退出,然后启动命令模式ping下看看是否已设置成功

从截图上看已成功将a.com解析到192.168.5.149IP

A服务器nginx.conf设置
打开nginx.conf,文件位置在nginx安装目录的conf目录下。

在http段加入以下代码

upstream a.com { 
      server  192.168.5.126:80; 
      server  192.168.5.27:80; 

  
server{ 
    listen 80; 
    server_name a.com; 
    location / { 
        proxy_pass         http://a.com; 
        proxy_set_header   Host             $host; 
        proxy_set_header   X-Real-IP        $remote_addr; 
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for; 
    } 
}

保存重启nginx

B、C服务器nginx.conf设置
打开nginx.confi,在http段加入以下代码

server{ 
    listen 80; 
    server_name a.com; 
    index index.html; 
    root /data0/htdocs/www; 
}

保存重启nginx

测试
当访问a.com的时候,为了区分是转向哪台服务器处理我分别在B、C服务器下写一个不同内容的index.html文件,以作区分。

打开浏览器访问a.com结果,刷新会发现所有的请求均分别被主服务器(192.168.5.149)分配到B服务器(192.168.5.27)与C服务器(192.168.5.126)上,实现了负载均衡效果。

B服务器处理页面

C服务器处理页面

假如其中一台服务器宕机会怎样?
当某台服务器宕机了,是否会影响访问呢?

我们先来看看实例,根据以上例子,假设C服务器192.168.5.126这台机子宕机了(由于无法模拟宕机,所以我就把C服务器关机)然后再来访问看看。

访问结果:

我们发现,虽然C服务器(192.168.5.126)宕机了,但不影响网站访问。这样,就不会担心在负载均衡模式下因为某台机子宕机而拖累整个站点了。

如果b.com也要设置负载均衡怎么办?
很简单,跟a.com设置一样。如下:

假设b.com的主服务器IP是192.168.5.149,负载均衡到192.168.5.150和192.168.5.151机器上

现将域名b.com解析到192.168.5.149IP上。

在主服务器(192.168.5.149)的nginx.conf加入以下代码:

upstream b.com { 
      server  192.168.5.150:80; 
      server  192.168.5.151:80; 

  
server{ 
    listen 80; 
    server_name b.com; 
    location / { 
        proxy_pass         http://b.com; 
        proxy_set_header   Host             $host; 
        proxy_set_header   X-Real-IP        $remote_addr; 
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for; 
    } 
}
保存重启nginx

在192.168.5.150与192.168.5.151机器上设置nginx,打开nginx.conf在末尾添加以下代码:

server{ 
    listen 80; 
    server_name b.com; 
    index index.html; 
    root /data0/htdocs/www; 
}

保存重启nginx

完成以后步骤后即可实现b.com的负载均衡配置。

主服务器不能提供服务吗?
以上例子中,我们都是应用到了主服务器负载均衡到其它服务器上,那么主服务器本身能不能也加在服务器列表中,这样就不会白白浪费拿一台服务器纯当做转发功能,而是也参与到提供服务中来。

如以上案例三台服务器:

A服务器IP :192.168.5.149 (主)

B服务器IP :192.168.5.27

C服务器IP :192.168.5.126

我们把域名解析到A服务器,然后由A服务器转发到B服务器与C服务器,那么A服务器只做一个转发功能,现在我们让A服务器也提供站点服务。

我们先来分析一下,如果添加主服务器到upstream中,那么可能会有以下两种情况发生:

1、主服务器转发到了其它IP上,其它IP服务器正常处理;

2、主服务器转发到了自己IP上,然后又进到主服务器分配IP那里,假如一直分配到本机,则会造成一个死循环。

怎么解决这个问题呢?因为80端口已经用来监听负载均衡的处理,那么本服务器上就不能再使用80端口来处理a.com的访问请求,得用一个新的。于是我们把主服务器的nginx.conf加入以下一段代码:

server{ 
    listen 8080; 
    server_name a.com; 
    index index.html; 
    root /data0/htdocs/www; 
}
 
重启nginx,在浏览器输入a.com:8080试试看能不能访问。结果可以正常访问

既然能正常访问,那么我们就可以把主服务器添加到upstream中,但是端口要改一下,如下代码:

upstream a.com { 
      server  192.168.5.126:80; 
      server  192.168.5.27:80; 
      server  127.0.0.1:8080; 
}

由于这里可以添加主服务器IP192.168.5.149或者127.0.0.1均可以,都表示访问自己。

重启Nginx,然后再来访问a.com看看会不会分配到主服务器上。

主服务器也能正常加入服务了。

最后
一、负载均衡不是nginx独有,著名鼎鼎的apache也有,但性能可能不如nginx。

二、多台服务器提供服务,但域名只解析到主服务器,而真正的服务器IP不会被ping下即可获得,增加一定安全性。

三、upstream里的IP不一定是内网,外网IP也可以。不过经典的案例是,局域网中某台IP暴露在外网下,域名直接解析到此IP。然后又这台主服务器转发到内网服务器IP中。

四、某台服务器宕机、不会影响网站正常运行,Nginx不会把请求转发到已宕机的IP上。

原文地址:http://www.php100.com/html/program/nginx/2013/0905/5525.html

注:upstream 也可以监听多个域名

upstream xxy.com {
server 192.168.10.47:80;
server www.baidu.com;
}

server{
listen 80;
server_name xxy.com;
location / {
proxy_pass http://xxy.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header type '1';

}
}

upstream bbb.com {
server 192.168.10.47:80;
server www.baidu.com;

}

server{
listen 80;
server_name bbb.com;
location / {
proxy_pass http://bbb.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header type '2';

}
}