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

centos7下配置nginx反向代理負載均衡叢集

一,實驗介紹

利用三臺centos7虛擬機器搭建簡單的nginx反向代理負載叢集,

三臺虛擬機器地址及功能介紹

192.168.1.188    nginx負載均衡器

192.168.1.189    web01伺服器

192.168.1.190    web02伺服器

軟體準備:

   centos7.4 x86_64

   nginx-1.6.3.tar.gz (http://nginx.org/download/nginx-1.6.3.tar.gz)

二,安裝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

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

三,配置檔案

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;
    }
   
}

mkdir /app/nginx/html/bbs

echo "192.168.1.189 bbs" >>/app/nginx/html/bbs/index.html             189

echo "192.168.1.189 bbs.dengchuanghai.org" >> /etc/hosts                     189

echo "192.168.1.190 bbs" >>/app/nginx/html/bbs/index.html    190

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

(分別在兩臺web伺服器上輸入以上內容)

然後分別啟動nginx

/app/nginx/sbin/nginx -t (檢查配置檔案有無錯誤)

/app/nginx/sbin/nginx  啟動

ss -tnlp | grep 80

使用curl bbs.dengchuanghai.org 看是否成功

2,以下操作在nginx負載均衡器上進行

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;
    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

使用curl www.dengchuanghai.org 


發現結果輸出為兩臺伺服器輪流輸出

實驗結束 ,