反向代理與負載均衡的概念:html
Nginx僅僅是做爲nginx proxy反向代理使用,nginx實際上是反向代理,只不過是有負載均衡的功能!android
安裝Nginx負載均衡nginx
官網Nginx模塊講解web
模塊詳解算法
upstream模塊apache
upstream www { #upstream是關鍵字,必需要寫,後面的www是一個羣組名字,本身起名
server 192.168.70.127:80 weight=1; # server固定關鍵字,後面能夠接域名或者IP,若是不指定端口默認80.vim
#weight權重,數值越大被分配的請求越多,結尾有分號
server 192.168.70.126:80 weight=1 bakup;#bakcup至關於熱備後端
server 192.168.79.128:80 weight=1 max_fails=2 fail_timeout=20s;#max_fails嘗試鏈接後端主機失敗的次數,這個值配合 proxy_next_upstream、 fastcgi_next_upstream和memcached_next_upstream這三個參數來使用。根據需求配置,不要太大建議1-3次緩存
fail_timeout=20s 失敗秒數,在達到max_fails嘗試鏈接失敗次數後,休息20秒,在次鏈接默認10秒,建議2-3秒服務器
}
upstream模塊調度算法
調度算法一份分爲兩類:
第一類 靜態調度算法
分配的時候,不須要考慮後端節點服務器的狀況(rr, wrr, ip_hash調度算法)
第二類 動態調度算法
根據自身的狀況調度,根據後端節點狀態是否響應很快進行調度(least_conn,fair等)
靜態調度算法
rr輪詢(默認調度算法)
按照客戶端請求順序把客戶端的請求逐一分配到不一樣後端節點服務器
wrr權重輪序
在rr輪詢算法的基礎上加上權重,權重值越大,別轉發的請求越多,能夠根據服務器狀態進行指定權重值大小
ip_hash
每一個請求按照客戶端IP的hash結果分配,新的請求到達時,先將客戶端IP經過哈希算法哈希出一個值,在隨後的的客戶端請求中,客戶IP的哈希值只要相同,就會被分配至同一臺服務器,該調度算法能夠解決動態網頁的session共享問題,可是會致使請求分配不均
(在upstream裏配置,若是是ip_hash,不能有weight和bakcup)
upstream www{
ip_hash:
server xxxx
}
動態調度算法
fair
fair算法會根據後端節點服務器的響應時間來分配請求,時間短的優先分配,必須下載nginx的相關模塊upstream_fair
upstream www{
fair;
server xxx;
server xxx;
}
least_conn
least_conn算法根據後端節點的鏈接數來決定分配狀況,哪一個機器鏈接數少就分發
url_hash算法(web cache)
和ip_hash相似,通常用於web緩存,根據訪問URL的hash結果來分配請求的,每一個URL定向到同一個後端服務器,後端服務器爲緩存服務器時效果明顯。
upstream www{
server xxxx
hash $request_uri;
hash_method crc32;
}
一致性hash算法
一致性hash算法通常用於代理後端業務爲緩存服務(squid,memcached)的場景,經過將用戶請求的URI或指定字符串進行計算,而後調度到後端服務器上,此後任何用戶查找同一個RUI或者指定字符串都會被調度到這一臺服務器上,此後後端的每一個節點緩存的內容都是不一樣的。
http{ upstream www{ consistent_hash $request_uri; server xxxx id=1001 weigh=3; } }
http_proxy_module模塊
proxy_pass轉發代理
location /name/ {
proxy_pass http://127.0.0.1/remote/;
}
相關參數
反向代理重要參數
prox_pass http://server_pools; 經過proxy_pass功能把用戶的請求轉向到反向代理定義的upstream服務器
proxy_set_header Host $host; 在代理向後端服務器發送的http請求頭中加入host字段信息,用於後端服務器配置有多個虛擬主機,能夠識別那個虛擬主機
proxy_set_header X-Forwarded-For $remot_addr; 用於接收用戶真實IP,而不是代理服務器ip
在配置文件裏都會加上include proxy.conf;
在proxy.conf裏增長參數,會顯得乾淨
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_connect_timeout 60;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
=========================NGINX--根據URL中的目錄地址實現代理轉發====================
當用戶請求www.daxian.com/upload/xx地址的時候,實現由upload上傳服務器池處理請求
當用戶請求www.daxian.com/static/xx地址的時候,實現由靜態服務器池處理請求
除此以外,對於其餘訪問請求,所有交給默認動態服務器請求
環境 131負載均衡 126nginx 127apache(服務器有限,127當兩臺服務器)
配置過程
在131nginx配置文件中添加
upstream static_pools { server 192.168.70.127:80 weight=1;#apache } upstream upload_pools{ server 192.168.70.127:8080 weight=1; } upstream default_pools{ server 192.168.70.126:80 weight=1;#nginx }
server {
listen 80;
server_name www.daxian.com;
location / {
proxy_pass http://default_pools;
include proxy.conf;
}
location /static/ {
proxy_pass http://static_pools;
include proxy.conf;
}
location /upload/ {
proxy_pass http://upload_pools;
include proxy.conf;
}
}
配置apache
cd /application/apache/conf/extra/ vim httpd-vhosts.conf <VirtualHost *:80> ServerAdmin 2647@qq.com DocumentRoot "/application/apache2.2.34/htdocs/www" ServerName www.daxian.com ServerAlias daxian.com ErrorLog "logs/www-error_log" CustomLog "logs/www-access_log" common </VirtualHost> <VirtualHost *:8080> ServerAdmin 2647@qq.com DocumentRoot "/application/apache2.2.34/htdocs/www8080" ServerName www.daxian.com ErrorLog "logs/www8080-error_log" CustomLog "logs/www8080-access_log" common </VirtualHost> 增長監聽端口8080 vim /application/apache/conf/httpd.conf Listen 80 Listen 8080
建立訪問目錄 mkdir -p /application/apache/htdocs/www8080 echo www8080>/application/apache/htdocs/www8080/index.html cd /application/apache/htdocs/www/ mkdir static/ echo static>index.html ../../bin/apachectl restart
web01修改hosts
192.168.70.127 web02 www.daxian.com
進行測試
curl http://www.daxian.com
apachewww
curl http://www.daxian.com/static/
static
cd www8080
mkdir /upload/
cd /upload/
echo "upload">index.html
測試
curl http://www.daxian.com:8080/upload/
upload
驗證結果
web01 修改hosts文件,將地址指向負載均衡器
192.168.70.131 lb01 www.daxian.com
ping www.daxian.com
PING lb-01 (192.168.70.131) 56(84) bytes of data.
64 bytes from lb-01 (192.168.70.131): icmp_seq=1 ttl=64 time=0.571 ms
64 bytes from lb-01 (192.168.70.131): icmp_seq=2 ttl=64 time=0.686 ms
64 bytes from lb-01 (192.168.70.131): icmp_seq=3 ttl=64 time=0.685 ms
測試
curl http://www.daxian.com
nginx wwww
curl http://www.daxian.com/static/
static
curl http://www.daxian.com/upload/
upload
============================nginx根據移動端轉發============================
在7層負載均衡下不須要人爲拆分域名,對外只須要用一個域名便可,經過獲取用戶請求中設備信息($http_user_agent獲取)
vim nginx.conf
worker_processes 1;
events {
worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream android_pools { server 192.168.70.127:80 weight=1; #apache } upstream iphone_pools { server 192.168.70.127:8080 weight=1; } upstream pc_pools { server 192.168.70.126:80 weight=1; #nginx } server { listen 80; server_name www.daxian.com; location / { if ($http_user_agent ~* "android") { proxy_pass http://android_pools; } if ($http_user_agent ~* "iphone") { proxy_pass http://iphone_pools; } proxy_pass http://pc_pools; include proxy.conf; } } }
../sbin/nginx -s reload
局域網裏就能夠輸入ip地址訪問
安卓
http://192.168.70.131/static/
蘋果
http://192.168.70.131/upload/
pc
http://192.168.70.131