實戰:php
1、源碼編譯安裝nginxhtml
[root@tiandong63 ~]#yum groupinstall "Development Tools" "Development Libraries" -y 安裝開發包
[root@tiandong63 ~]#yum install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre* -y 安裝依賴包nginx
[root@tiandong63 ~]#tar -zxvf nginx-1.9.4.tar.gz -C /usr/local/src/
[root@tiandong63 ~]#cd /usr/local/src/
[root@tiandong63 src]#cd nginx-1.9.4/
[root@tiandong63 nginx-1.9.4]#./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_modulec++
參數:
--with-http_dav_module 啓用ngx_http_dav_module支持(增長PUT,DELETE,MKCOL:建立集合,COPY和MOVE方法)默認狀況下爲關閉,需編譯開啓
--with-http_stub_status_module 啓用ngx_http_stub_status_module支持(獲取nginx自上次啓動以來的工做狀態)
--with-http_addition_module 啓用ngx_http_addition_module支持(做爲一個輸出過濾器,支持不徹底緩衝,分部分響應請求)
--with-http_sub_module 啓用ngx_http_sub_module支持(容許用一些其餘文本替換nginx響應中的一些文本)
--with-http_flv_module 啓用ngx_http_flv_module支持(提供尋求內存使用基於時間的偏移量文件)
--with-http_mp4_module 啓用對mp4文件支持(提供尋求內存使用基於時間的偏移量文件
[root@tiandong63 nginx-1.9.4]#make && make install
[root@tiandong63 nginx-1.9.4]#useradd -M -u 8001 -s /sbin/nologin nginx
[root@tiandong63 ~]# /usr/local/nginx/sbin/nginx 啓動nginx
[root@tiandong63 ~]# netstat -antup|grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 42350/nginx
測試nginxapache
2、nginx服務器平常操做:vim
配置nginx爲分發器實現動靜分離後端
vim /usr/local/nginx/conf/nginx.conf緩存
2 user nginx nginx;
43 location / {
44 root html;
45 index index.html index.htm;
46 if ($request_uri ~* \.html$){
47 proxy_pass http://htmlservers;
48 }
49 if ($request_uri ~* \.php$){
50 proxy_pass http://phpservers;
51 }
52 proxy_pass http://picservers;
53 }
123 upstream htmlservers { #定義負載均衡服務器組名稱
124 server 192.168.199.4:80;
125 server 192.168.199.5:80;
126 }
127 upstream phpservers{
128 server 192.168.199.4:80;
129 server 192.168.199.5:80;
130 }
131 upstream picservers {
132 server 192.168.199.4:80;
133 server 192.168.199.5:80;
134 }
135 }bash
把一下文件註釋了,否組php文件直接在nginx服務器上解析了,不在解析給後端的服務器。服務器
[root@tiandong63 ~]# ln -s /usr/local/nginx/sbin/* /usr/bin/
[root@tiandong63 ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@tiandong63 ~]# nginx -s reload
3、配置後端服務器(tiandong64和tiandong65上面配置同樣):
[root@tiandong64 ~]# yum install php httpd -y
[root@tiandong65 ~]# yum -y install php httpd -y
[root@tiandong64 ~]# cd /var/www/html/
[root@tiandong64 html]# more index.html
192.168.199.4
[root@tiandong64 html]# more test.php
192.168.199.4
<?php
phpinfo();
?>
[root@tiandong64 html]#
[root@tiandong64 html]# ll
total 12
-rw-r--r-- 1 root root 14 Mar 20 00:24 index.html
-rw-r--r-- 1 root root 790 Dec 24 2018 pic.PNG
-rw-r--r-- 1 root root 35 Mar 20 00:25 test.php
重啓Apache服務:
[root@tiandong64 ~]# /etc/init.d/httpd restart
[root@tiandong65 ~]# /etc/init.d/httpd restart
4、測試:
測試html頁面:
[root@tiandong66 ~]# curl 192.168.199.3
192.168.199.5
[root@tiandong66 ~]# curl 192.168.199.3
192.168.199.4
[root@tiandong66 ~]# curl 192.168.199.3
192.168.199.5
[root@tiandong66 ~]# curl 192.168.199.3
192.168.199.4
測試php頁面:
測試服務器性能:
[root@tiandong66 ~]# ab -n 1000 -c 1000 http://192.168.199.3/index.html 運行正常
[root@tiandong66 ~]# ab -n 2000 -c 2000 http://192.168.199.3/index.html 運行報錯
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 192.168.199.3 (be patient)
socket: Too many open files (24) #測試時,一次打開的socket文件太多。系統默認的一次打開的最多問1024個
[root@tiandong66 ~]# ulimit -n 10240
[root@tiandong63 ~]# ulimit -n 10240
5、nginx負載的五中策略設置方法:
一、輪詢(默認)
upstream htmlservers { #定義負載均衡服務器組名稱
server 192.168.199.4:80;
server 192.168.199.5:80;
}
二、指定權重(指定輪詢概率,weight和訪問比例成正比,用戶後端服務器性能不均的狀況)
upstream htmlservers { #定義負載均衡服務器組名稱
server 192.168.199.4:80 weight=1;
server 192.168.199.5:80 weight=2;
}
三、IP綁定 ip_hash
每一個請求按訪問ip的hash結果分配,這樣每一個訪客固定訪問一個後端服務器,能夠解決session的問題。
upstream backserver {
ip_hash;
server 192.168.199.4:80;
server 192.168.199.5:80;
}
四、fair(第三方)
按後端服務器的響應時間來分配請求,響應時間短的優先分配。
upstream backserver {
server server1;
server server2;
fair;
}
五、url_hash(第三方)
按訪問url的hash結果來分配請求,使每一個url定向到同一個後端服務器,後端服務器爲緩存時比較有效。
upstream backserver {
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}
6、故障演練
當其中一臺Apache宕機
[root@tiandong64 ~]# /etc/init.d/httpd stop
Stopping httpd: [ OK ]
[root@tiandong66 ~]# curl 192.168.199.3
192.168.199.4
[root@tiandong66 ~]# curl 192.168.199.3
192.168.199.5
[root@tiandong66 ~]# curl 192.168.199.3
192.168.199.4
[root@tiandong66 ~]# curl 192.168.199.3
192.168.199.5
[root@tiandong66 ~]# curl 192.168.199.3
192.168.199.5
[root@tiandong66 ~]# curl 192.168.199.3
192.168.199.5
[root@tiandong66 ~]# curl 192.168.199.3
192.168.199.5
一直訪問的是另一臺機器
故障恢復:
[root@tiandong66 ~]# curl 192.168.199.3192.168.199.5[root@tiandong66 ~]# curl 192.168.199.3192.168.199.4[root@tiandong66 ~]# curl 192.168.199.3192.168.199.5[root@tiandong66 ~]# curl 192.168.199.3192.168.199.4[root@tiandong66 ~]# curl 192.168.199.3192.168.199.5OK!!!!