本文收錄在Linux運維企業架構實戰系列html
今天想起當初研究nginx反向代理負載均衡時,nginx自身的upstream後端配置用着很是不舒服; 當時使用的淘寶基於nginx二次開發的Tengine,今天總結一下。nginx
官網:http://tengine.taobao.org/download.htmlc++
[root@along app]# wget http://tengine.taobao.org/download/tengine-2.2.3.tar.gz [root@along app]# tar -xvf tengine-2.2.3.tar.gz
[root@along app]# groupadd nginx [root@along app]# useradd -s /sbin/nologin -g nginx -M nginx [root@along app]# yum -y install gc gcc gcc-c++ pcre-devel zlib-devel openssl-devel
[root@along app]# cd tengine-2.2.3/ [root@along tengine]# ./configure --user=nginx --group=nginx --prefix=/app/tengine --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module [root@along tengine]# make && make install [root@along tengine]# chown -R nginx.nginx /app/tengine [root@along tengine]# ll /app/tengine total 8 drwxr-xr-x 2 nginx nginx 4096 Feb 20 14:55 conf drwxr-xr-x 2 nginx nginx 40 Feb 20 14:50 html drwxr-xr-x 2 nginx nginx 4096 Feb 20 14:50 include drwxr-xr-x 2 nginx nginx 6 Feb 20 14:50 logs drwxr-xr-x 2 nginx nginx 6 Feb 20 14:50 modules drwxr-xr-x 2 nginx nginx 35 Feb 20 14:50 sbin
注:web
[root@along nginx]# vim /usr/lib/systemd/system/nginx.service [Unit] Description=nginx - high performance web server Documentation=http://nginx.org/en/docs/ After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/app/tengine/logs/nginx.pid ExecStartPre=/app/tengine/sbin/nginx -t -c /app/tengine/conf/nginx.conf ExecStart=/app/tengine/sbin/nginx -c /app/tengine/conf/nginx.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
[root@along ~]# systemctl start nginx [root@along ~]# ss -nutlp |grep 80 tcp LISTEN 0 128 *:80 *:* users:(("nginx",pid=4933,fd=6),("nginx",pid=4932,fd=6))
網頁訪問驗證算法
由於tengine的其餘功能和nginx配置差很少,就不在演示了;主要演示,我認爲較爲方便的反向代理配置。vim
tengine配置反向代理格式和haproxy很類似;後端
後端兩臺服務器事先本身準備好網頁服務(nginx/httpd等均可以)緩存
[root@along tengine]# cd /app/tengine/conf/ [root@along conf]# vim nginx.conf http { ... ... #配置後端代理集羣,默認是輪詢算法 upstream srv { server 192.168.10.101:80; server 192.168.10.106:80; check interval=3000 rise=2 fall=5 timeout=1000 type=http; check_http_send "HEAD / HTTP/1.0\r\n\r\n"; check_http_expect_alive http_2xx http_3xx; } ... ... #在server端location反向代理 server { location / { proxy_pass http://srv; } } ... ... }
(1)驗證配置是否有誤安全
[root@along tengine]# ./sbin/nginx -t nginx: the configuration file /app/tengine/conf/nginx.conf syntax is ok nginx: configuration file /app/tengine/conf/nginx.conf test is successful
(2)重啓服務器bash
[root@along tengine]# systemctl restart nginx
(3)網頁訪問驗證
由於默認是輪詢算法,因此刷新頁面,就會輪詢調度到後臺2個網頁服務器
輪詢是upstream的默認分配方式,即每一個請求按照時間順序輪流分配到不一樣的後端服務器,若是某個後端服務器down掉後,能自動剔除。
upstream srv { server 192.168.10.101:80; server 192.168.10.106:80; }
加權輪詢,輪詢的增強版,便可以指定輪詢比率,weight和訪問概率成正比,主要應用於後端服務器異質的場景下。
upstream srv { server 192.168.10.101:80 weight=1; server 192.168.10.106:80 weight=2; }
每一個請求按照訪問ip(即Nginx的前置服務器或者客戶端IP)的hash結果分配,這樣每一個訪客會固定訪問一個後端服務器,能夠解決session一致問題。
upstream srv { ip_hash; server 192.168.10.101:80; server 192.168.10.106:80; }
注意:
fair顧名思義,公平地按照後端服務器的響應時間(rt)來分配請求,響應時間短即rt小的後端服務器優先分配請求。若是須要使用這種調度算法,必須下載Nginx的upstr_fair模塊。
upstream srv { fair; server 192.168.10.101:80; server 192.168.10.106:80; }
與ip_hash相似,可是按照訪問url的hash結果來分配請求,使得每一個url定向到同一個後端服務器,主要應用於後端服務器爲緩存時的場景下。
upstream srv { server 192.168.10.101:80; server 192.168.10.106:80; hash $request_uri; hash_method crc32; }
(1)參數說明
(2)舉例說明以下:
upstream backend { server backend1.example.com weight=5; server 127.0.0.1:8080 max_fails=3 fail_timeout=30s; server unix:/tmp/backend3; }