準備3臺服務器,一臺作nginx負載均衡,兩臺爲Apache的web服務器html
1.首先源碼安裝nginxnginx
nginx的安裝方式web
配置nginx負載均衡和nginx負載均衡調度算法爲加權輪詢,加權輪詢比例爲1:3,下列代碼有註釋。算法
vim /usr/local/nginx/conf/nginx.conf
http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; upstream webservers { #添加nginx反向代理 server 192.168.189.130 weight=1; #nginx須要代理的服務器,能夠根據狀況加端口 server 192.168.189.131 weight=3; #服務器2 } sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://webservers; #反向代理設置爲web羣組 }
爲兩臺服務器建立測試頁面vim
採用Apache服務,路徑:服務器
/var/www/html vim /var/www/html/index.html
web1 web2
測試訪問nginx的ip,並測試負載均衡和加權輪詢app
能夠看到1:3的web1/2出現次數負載均衡
小步驟:tcp
收集整理nginx負載均衡的日誌到/tmp下面測試
cat /usr/local/nginx/logs/*.log > /tmp/nginx
過濾出訪問最多的前兩條client IP地址
[root@localhost logs]# cat /usr/local/nginx/logs/access.log | awk '{print $1}' |sort|uniq |sed -n '1,2p'
查看到兩個訪問最多的ip
nginx負載均衡的日誌:
tail -f /usr/local/nginx/logs/access.log