部署nginx服务

本版本采用1.8版本,如图:

nginx有哪几种负载均衡_ci

一、nginx安装

1、安装依赖


yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel

yum -y install gcc*

nginx有哪几种负载均衡_IP_02


2.下载好nginx-1.8.1.tar.gz存放到 服务器Centos 7 上的 /usr/local/ 下
3. 解压
tar zxvf nginx-1.8.1.tar.gz



nginx有哪几种负载均衡_nginx_03


4.进行configure配置(ps:当前我在 /usr/local/ 的位置,执行以下命令)


cd /usr/local/nginx-1.8.1 && ./configure --prefix=/usr/local/nginx


5.编译安装(ps:4步骤执行后,马上进行5步骤)


make && make install

注安装完毕后,将在/usr/local/下看到nginx文件夹,nginx文件夹中有四个文件夹:

conf 存放配置文件

html 存放静态页面

logs 存放日志文件

sbin 执行文件

6.启动 nginx


/usr/local/nginx/sbin/nginx //启动  
  /usr/local/nginx/sbin/nginx -s stop //关闭  
  /usr/local/nginx/sbin/nginx -s reload //重启


 7、 附带:查看启动状态  


ps -ef | grep nginx  
netstat -anptul |grep 80  //查看80端口


8、启动成功如图:

nginx有哪几种负载均衡_nginx_04

9、浏览器访问即可,出现下图经典欢迎页面表示 nginx 安装成功

nginx有哪几种负载均衡_IP_05

一、IPhash-哈希模式

  • # IP-hash-哈希模式 - 固定响应
  • # nginx服务器根据数据源IP地址 - 哈希计算 (IP地址不变 -得到哈希值固定)
  • # nginx服务器根据哈希值进⾏⾃动匹配 - 后端服务器
  • # 哈希算法 - 均衡 ,流量均衡分配到后端server
  • # 在访问量较少时,有所有流量集中访问到同⼀个后端server的可能性。
  • # 在访问量较⼤时,流量分配越均衡



[root@ha1 nginx]# vim nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
# [alert] 18037#0: 1024 worker_connections are not enough
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

##### add to config start #####

stream {
log_format main '$remote_addr $upstream_addr - [$time_local] $status
$upstream_bytes_sent';
access_log /var/log/nginx/web_cluster.log main;

# upstream-load_balance-Cluster 负载均衡
upstream web_cluster {
hash $remote_addr consistent;
server 192.168.8.101:80 ; # server1 IP:port
server 192.168.8.102:80 ; # server2 IP:port
server 192.168.8.103:80 ; # server3 IP:port
}

server {
listen 80; # nginx proxy port -
proxy_pass web_cluster; # 反向代理
}

}
##### add to config end #####


检查Nginx配置⽂件格式


[root@ha1 nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful


重新加载配置⽂件
[root@ha1 nginx]# nginx -s reload

二、权重加权轮询模式

# weight 权重

# 根据分配不同权重,负载均衡根据权重值来进⾏访问分配

修改配置文件


[root@ha1 nginx]# vim nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
# [alert] 18037#0: 1024 worker_connections are not enough
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
worker_connections 1024;
}

##### add to config start #####

stream {
log_format main '$remote_addr $upstream_addr - [$time_local] $status
$upstream_bytes_sent';
access_log /var/log/nginx/web_cluster.log main;

# upstream-load_balance-Cluster 负载均衡
upstream web_cluster {
server 192.168.40.101:80 weight=3; # server1 IP:port
server 192.168.40.102:80 weight=2; # server2 IP:port
server 192.168.40.103:80 weight=1; # server3 IP:port
}

server {
listen 80; # nginx proxy port -
proxy_pass web_cluster; # 反向代理
}

}
##### add to config end #####
检查Nginx配置⽂件格式


[root@ha1 nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
重新加载配置⽂件
[root@ha1 nginx]# nginx -s reload

三、least_connection-最⼩连接

链接数最少,选择哪个


[root@ha1 nginx]# vim nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
# [alert] 18037#0: 1024 worker_connections are not enough
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
worker_connections 1024;
}

##### add to config start #####

stream {
log_format main '$remote_addr $upstream_addr - [$time_local] $status
$upstream_bytes_sent';
access_log /var/log/nginx/web_cluster.log main;

# upstream-load_balance-Cluster 负载均衡
upstream web_cluster {
least_conn;
server 192.168.8.101:80 ; # server1 IP:port
server 192.168.8.102:80 ; # server2 IP:port
server 192.168.8.103:80 ; # server3 IP:port
}

server {
listen 80; # nginx proxy port -
proxy_pass web_cluster; # 反向代理
}

}
##### add to config end #####


检查Nginx配置⽂件格式


[root@ha1 nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful


重新加载配置⽂件
[root@ha1 nginx]# nginx -s reload