使用nginx實現負載均衡和動靜分離php
yum -y install gcc gcc-c++ autoconf automake zib zib-devel openssl openssl-devel pcre pcre-devel
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
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
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;
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
yum install httpd php -y
在網站根目錄下建立如下文件
啓動 httpdc++
service httpd start
安裝httpd php,在網站根目錄下建立 index.html,test.php,1.png,啓動httpd服務vim
中止 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
ulimit -n //系統默認一個進程最多同時容許打開1024個文件 1024
設置進程最多同時容許打開的默認文件個數爲2000負載均衡
ulimit -n 2000
這樣就能夠解決報錯問題ide