使用nginx實現負載均衡和動靜分離

使用nginx實現負載均衡和動靜分離php

  • 在192.168.221.10這臺機器上源碼編譯安裝nginx
yum -y install gcc gcc-c++ autoconf automake zib zib-devel openssl openssl-devel pcre pcre-devel

使用nginx實現負載均衡和動靜分離

cd /usr/local/src/
wget http://nginx.org/download/nginx-1.8.0.tar.gz
tar zxvf nginx-1.8.0.tar.gz
cd nginx-1.8.0/
./configure --prefix=/usr/local/nginx-1.8.0 --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module

使用nginx實現負載均衡和動靜分離

make -j 4 && make install
useradd -u 8000 -s /sbin/nologin nginx
/usr/local/nginx-1.8.0/sbin/nginx
echo "/usr/local/nginx-1.8.0/sbin/nginx &" >> /etc/rc.local

nginx的操做html

cd /usr/local/nginx-1.8.0/sbin/
./nginx -t
./nginx -s reload|stop
  • 配置nginx成爲分發器,實現動靜分離
cd /usr/local/nginx-1.8.0/conf/
cp nginx.conf nginx.conf.back
vim nginx.conf
user  nginx nginx;
// 在 location {...}內添加如下內容
if ($request_uri ~* \.html$){
                proxy_pass http://htmlservers;
            }
            if ($request_uri ~* \.php$){
                proxy_pass http://phpservers;
            }
            proxy_pass http://picservers;
  • 定義負載均衡設備的ip
    在配置文件ngin.conf的最後一行}前,添加以下內容
upstream htmlservers {
        server 192.168.221.20:80;
        server 192.168.221.30:80;
        }
    upstream phpservers {
        server 192.168.221.20:80;
        server 192.168.221.30:80;
        }
    upstream picservers {
        server 192.168.221.20:80;
        server 192.168.221.30:80;
        }

檢測語法,從新加載nginx.confnginx

../sbin/nginx -t
../sbin/nginx -s reload
  • 配置後端服務器(192.168.221.20)
yum install httpd php -y

在網站根目錄下建立如下文件
使用nginx實現負載均衡和動靜分離
啓動 httpdc++

service httpd start
  • 配置後端服務器(192.168.221.30)

安裝httpd php,在網站根目錄下建立 index.html,test.php,1.png,啓動httpd服務vim

  • 測試轉發靜態頁

使用nginx實現負載均衡和動靜分離

使用nginx實現負載均衡和動靜分離

  • 測試轉發動態頁

使用nginx實現負載均衡和動靜分離

使用nginx實現負載均衡和動靜分離

  • 測試轉發圖片

使用nginx實現負載均衡和動靜分離

使用nginx實現負載均衡和動靜分離

  • 測試自動剔除壞的節點

中止 192.168.221.20機器上的httpd服務,再次訪問後端

這樣就會一直訪問到192.168.221.30上的站點服務器

  • 性能測試
yum install httpd-tools -y   //安裝ab命令
ab -n 1000 -c 1000 http://192.168.221.10/index.html

使用nginx實現負載均衡和動靜分離

ulimit -n   //系統默認一個進程最多同時容許打開1024個文件
1024

設置進程最多同時容許打開的默認文件個數爲2000負載均衡

ulimit -n 2000

這樣就能夠解決報錯問題ide

相關文章
相關標籤/搜索