Centos 7配置nginx反向代理负载均衡集群

时间:2023-03-08 21:11:00
Centos 7配置nginx反向代理负载均衡集群

一,实验介绍

利用三台centos7虚拟机搭建简单的nginx反向代理负载集群,

三台虚拟机地址及功能介绍

192.168.2.76    nginx负载均衡器

192.168.2.82    web01服务器

192.168.2.78    web02服务器

二,安装nginx软件(以下操作三台虚拟机都要进行)
1,安装依赖软件包命令集合

yum -y install openssl openssl-devel pcre pcre-devel gcc

2,安装nginx软件包命令集合

Centos 7配置nginx反向代理负载均衡集群
mkdir /app

cd /app

wget -q http://nginx.org/download/nginx-1.6.3.tar.gz

useradd -s /sbin/nologin -M nginx

id nginx
tar xf nginx-1.6.3.tar.gz cd nginx-1.6.3 ./configure --user=nginx --group=nginx --prefix=/app/nginx --with-http_stub_status_module --with-http_ssl_module make && make install
Centos 7配置nginx反向代理负载均衡集群

有些Centos 7.6里面没有安装wget命令,所以要自己安装:

yum -y install wget

三,配置文件
1,(以下操作在web01和web02上进行)

vim /app/nginx/conf/nginx.conf

将配置文件修改为以下内容

Centos 7配置nginx反向代理负载均衡集群
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 bbs.dengchuanghai.org;
  location / {
  root html/bbs;
  index index.html index.htm;
  }
  access_log logs/access_bbs.log main;
}
Centos 7配置nginx反向代理负载均衡集群

对照上面的把注释去掉。

然后保存退出,在输入一下内容:

web1服务器:

mkdir /app/nginx/html/bbs

echo "192.168.2.82 bbs" >>/app/nginx/html/bbs/index.html             

echo "192.168.2.82 bbs.dengchuanghai.org" >> /etc/hosts                     

web2服务器:

echo "192.168.2.78 bbs" >>/app/nginx/html/bbs/index.html    

echo "192.168.2.78 bbs.dengchuanghai.org" >> /etc/hosts  

然后分别启动nginx

/app/nginx/sbin/nginx -t (检查配置文件有无错误)

/app/nginx/sbin/nginx  启动

ss -tnlp | grep 80

2,以下操作在nginx负载均衡器上进行

vim /app/nginx/conf/nginx.conf

有些Centos 7.6 没有安装vim命令,需要自己安装:

yum -y install vim-enhanced

更改为如下内容

Centos 7配置nginx反向代理负载均衡集群
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.190:80 weight=1;
server 192.168.1.189:80 weight=1;
}
server {
listen 80;
server_name www.dengchuanghai.org;
location / {
proxy_pass http://www_server_pools;
}
}
}
Centos 7配置nginx反向代理负载均衡集群

保存并退出:

echo "192.168.1.188 www.dengchuanghai,org" >> /etc/hosts

检查语法

/app/nginx/sbin/nginx -t

启动服务

/app/nginx/sbin/nginx

在外部浏览器输入你的代理服务器IP;