1. 程式人生 > >Centos 7配置nginx反向代理負載均衡叢集

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軟體包命令集合

複製程式碼
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.6裡面沒有安裝wget命令,所以要自己安裝:

yum -y install wget

三,配置檔案
1,(以下操作在web01和web02上進行)

vim /app/nginx/conf/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 bbs.dengchuanghai.org;
  location / {
  root html/bbs;
  index index.html index.htm;
  }
  access_log logs/access_bbs.log main;
}
複製程式碼

對照上面的把註釋去掉。

然後儲存退出,在輸入一下內容:

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

更改為如下內容

複製程式碼
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;
     }
    }
}
複製程式碼

儲存並退出:

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

檢查語法 

/app/nginx/sbin/nginx -t

啟動服務

/app/nginx/sbin/nginx

在外部瀏覽器輸入你的代理伺服器IP;