Nginx反向代理+負載均衡(原創)
注意:
1、本文依據實際生産環境整理,安裝部分可直接複制使用,核心部分需要依據公司要求配置
2、如讀者在閱讀時遇到任何問題,歡迎留言回報
3、聯系方式:
QQ:595627025
Mail:[email protected]
1. 環境
1.1 系統環境
[root@nginx-cache-1-1 nginx]# cat /etc/redhat-release
CentOS release 5.8 (Final)
[root@nginx-cache-1-1 ~]# uname -r
2.6.18-308.el5
[root@nginx-cache-1-1 ~]# uname -m
x86_64
[root@nginx-cache-1-1 ~]# uname -n
nginx-cache-1-1
1.2 軟體需求
軟體:
nginx-1.2.1.tar.gz
pcre-8.11.tar.gz
位址:
http://nginx.org/en/download.html
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/
2. 安裝Nginx
2.1 安裝pcre
1)安裝指令:
cd /home/start/tools
tar zxf pcre-8.11.tar.gz
cd pcre-8.11
./configure
make && make install
cd ../
2)安裝過程:
[root@nginx-cache-1-1 tools]# tar zxf pcre-8.11.tar.gz
[root@nginx-cache-1-1 tools]# cd pcre-8.11
[root@nginx-cache-1-1 pcre-8.11]# ./configure
[root@nginx-cache-1-1 pcre-8.11]# make && make install
2.2 安裝Nginx
1)安裝指令:(可以直接複制)
useradd -M -s /sbin/nologin www
tar zxf nginx-1.2.1.tar.gz
cd nginx-1.2.1
./configure \
--user=www \
--group=www \
--prefix=/application/nginx-1.2.1 \
--with-pcre \
--with-http_stub_status_module \
--with-http_ssl_module
ln -s /application/nginx-1.2.1 /application/nginx
echo '/usr/local/lib' >>/etc/ld.so.conf
tail -1 /etc/ld.so.conf
ldconfig
echo 'export PATH=$PATH:/application/nginx/sbin' >>/etc/profile
source /etc/profile
echo ‘start for nginx by start 2012-01-26’ >>/etc/rc.local
echo ‘/application/nginx/sbin/nginx’ >>/etc/rc.local
tail -2 /etc/rc.local
2)安裝過程:
[root@nginx-cache-1-1 tools] useradd -M -s /sbin/nologin www <==添加Nginx系統運作帳戶
[root@nginx-cache-1-1 tools] tar zxf nginx-1.2.1.tar.gz <==解壓Nginx
[root@nginx-cache-1-1 tools] cd nginx-1.2.1
[root@nginx-cache-1-1 nginx-1.2.1] ./configure \ <==編譯安裝
[root@nginx-cache-1-1 nginx-1.2.1] make && make install
[root@nginx-cache-1-1 nginx-1.2.1] ln -s /application/nginx-1.2.1 /application/nginx <==建立軟連結友善更新
[root@nginx-cache-1-1 nginx-1.2.1] echo '/usr/local/lib' >>/etc/ld.so.conf <==配置lib庫
[root@nginx-cache-1-1 nginx-1.2.1] tail -1 /etc/ld.so.conf <==檢查是否添加
/usr/local/lib
[root@nginx-cache-1-1 nginx-1.2.1] ldconfig
[root@nginx-cache-1-1 nginx-1.2.1] cd ..
[root@nginx-cache-1-1 nginx-1.2.1] echo ‘export PATH=$PATH:/application/nginx/sbin’ >>/etc/profile <==将Nginx指令加入系統全局變量
[root@nginx-cache-1-1 nginx-1.2.1] source /etc/profile <==使變量生效
[root@nginx-cache-1-1 nginx-1.2.1] echo ‘start for nginx by start 2012-01-26’ >>/etc/rc.local
[root@nginx-cache-1-1 nginx-1.2.1] echo ‘/application/nginx/sbin/nginx’ >>/etc/rc.local <==将nginx加入開機自啟動
2.3 啟動檢查
[root@nginx-cache-1-1 tools]# nginx <==Nginx指令已經加入到系統全局變量
[root@nginx-cache-1-1 tools]# netstat -lnt|grep 80
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
3. Nginx負載均衡
提示:Nginx主配置檔案使用的是預設提供的,加入了Proxy的參數。
3.1 Proxy參數
client_max_body_size 300m;
client_body_buffer_size 128k;
proxy_connect_timeout 600;
proxy_read_timeout 600;
proxy_send_timeout 600;
proxy_buffer_size 16k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
參數解釋:
#允許用戶端請求的最大的單個檔案位元組數
client_max_body_size 300m;
#緩沖區代理緩沖使用者端請求的最大位元組數 可以了解為先儲存到本地再傳給使用者
client_body_buffer_size 128k;
#跟後端伺服器連接配接的逾時時間_發起握手等候響應逾時時間
proxy_connect_timeout 600;
#連接配接成功後_等候後端伺服器響應時間_其實已經進入後端的排隊之中等候處理
proxy_read_timeout 600;
#後端伺服器資料回傳時間_就是在規定時間之内後端伺服器必須傳完所有的資料
proxy_send_timeout 600;
#代理請求緩存區_這個緩存區間會儲存使用者的頭資訊以供Nginx進行規則處理_一般隻要能儲存下頭資訊即可
proxy_buffer_size 16k;
#同上 告訴Nginx儲存單個用的幾個Buffer 最大用多大空間
proxy_buffers 4 32k;
#如果系統很忙的時候可以申請更大的proxy_buffers 官方推薦*2
3.2 upstream子產品
3.2.1 文法
官方位址:http://nginx.org/en/docs/http/ngx_http_upstream_module.html
官方提示:upstream子產品預設被proxy_pass, fastcgi_pass, and memcached_pass 這三個參數調用
The ngx_http_upstream_module module allows to define groups of servers that can be referenced from the proxy_pass, fastcgi_pass, and memcached_pass directives.
官方示例:
upstream backend {
server backend1.example.com weight=5; <==單獨域名。如果不加端口,預設是80端口。weight代表權重,值越大被配置設定的幾率越高;
server backend2.example.com:8080; <==域名加端口。轉發到後端的指定端口上;
server unix:/tmp/backend3; <==指定socket檔案(具體用法不祥)
提示:Server如果接域名,需要内網有DNS伺服器,或者在hosts檔案做解析。Server後面還可以直接接IP或IP加端口
server 192.168.1.2
server 192.168.1.3:8080
server backup1.example.com:8080 backup; <==備份伺服器,等上面指定的伺服器都不可通路的時候會啟用,backup的用法和Haproxy中用法一樣;
server backup2.example.com:8080 backup;
}
3.2.1 配置參數
官方原文:
weight=number
sets a weight of the server, by default 1.
設定該伺服器的權重,預設值是1.這個數值越大,伺服器會被轉發更多的請求;
max_fails=number
sets a number of unsuccessful attempts to communicate with the server during a time set by the fail_timeout parameter after which it will be considered down for a period of time also set by the fail_timeout parameter. By default, the number of unsuccessful attempts is set to 1. A value of zero disables accounting of attempts. What is considered to be an unsuccessful attempt is configured by the proxy_next_upstream, fastcgi_next_upstream, and memcached_next_upstream directives. The http_404 state is not considered an unsuccessful attempt.
Nginx嘗試連結後端主機失敗次數,這個數值是配合 proxy_next_upstream, fastcgi_next_upstream, and memcached_next_upstream這三個參數來使用的,當Nginx接收後端伺服器傳回這三個參數定義的狀态碼的時候,會将這個請求轉發給正常工作的後端伺服器,例如404,502,503.Max_fails 預設值是1;
fail_timeout=time
sets
a time during which the specified number of unsuccessful attempts to communicate with the server should happen for the server to be considered down;
and a period of time the server will be considered down.
By default, timeout is set to 10 seconds.
在max_fails定義的失敗次數後,距離下次檢查的間隔時間,預設是10s;
backup
marks the server as a backup server. It will be passed requests when the primary servers are down.
這标志着這個伺服器作為備份伺服器,當主伺服器全部當機的時候,才會向他轉發請求;
down
marks the server as permanently down; used along with the ip_hash directive.
這标志着伺服器永遠不可用,這個參數隻配合ip_hash使用
示例:
server backend1.example.com weight=5; <==如果就是單個Server,沒必要設定權重
server 127.0.0.1:8080 max_fails=5 fail_timeout=10s; <==當檢測次失敗數等于5的時候,間隔10s再檢查,這個參數和proxy/fasrcgi/memcached_next_upstream,相關;
server unix:/tmp/backend3;
server backup1.example.com:8080 backup; <==熱備機器設定
max_fails=5 fail_timeout=10s
重新加載nginx配置或WEB主機檢測正常後,如果後端出現proxy_next_upstream中定義的錯誤(502),Nginx會根據max_fails的值去後端伺服器檢測,如果max_fails是5 ,他就檢測5次,如果5次都是502那麼,他就會根據fail_timeout的值,等待10s再去檢查,過10s後檢查一次,如果還是502,那麼繼續等待10s,再去檢查,還是隻檢查一次,如果持續502,在不重新加載nginx配置或web站點沒有恢複的情況下,每隔10s都隻檢測一次。
3.2.2 排程算法
1)輪詢(預設)
每個請求按時間順序注意配置設定到不同的機器,相當于LVS中rr算法,如果後端伺服器當機(預設情況下隻檢測80端口,如果後端報502,404,403,503,還是會直接傳回給使用者),則會跳過該伺服器,将請求配置設定給下一個伺服器。
2)weight(權重)
在指定的輪詢的基礎上加上權重(預設是rr+weight),權重輪詢和通路成正比,權重越大,轉發的請求也就越多。可以根據伺服器的配置和性能指定權重值大小,可以有效解決新舊伺服器配置設定問題。
後端伺服器192.168.1.2配置:E5520*2 CPU,8G記憶體
後端伺服器192.168.1.3配置:Xeon(TM)2.80GHz * 2,4G記憶體
我希望在有30個請求到達前端時,其中20個請求交給192.168.1.3處理,剩餘10個請求交給192.168.1.2處理,就可做如下配置;
upstream engine {
server 192.168.1.2 weight=1;
server 192.168.1.3 weight=2;
}
3)ip_hash
每個請求按通路的Ip的hash結果配置設定,當新的請求到達時,先将其用戶端ip通過雜湊演算法哈希出一個值,在随後請求用戶端Ip的哈希值隻要相同,就會被配置設定至同一個伺服器,該排程算法可以解決session問題,但有時會導緻配置設定不均即,無法保證負載均衡。
提示:必須是最前端的伺服器,後端也必須直接接應用伺服器
ip_hash;
server 192.168.1.2:80;
server 192.168.1.3:8080;
4)fair(第三方)
按照後端伺服器的響應時間來配置設定請求,響應時間短的優先配置設定。
server 192.168.1.2;
server 192.168.1.3;
fair;
5)usr_hash(第三方)
按通路url的hash結果來配置設定請求,使每個url定向到同一個後端伺服器,後端伺服器為緩存時比較有效。在upstream中加入hash語句,server語句中不能寫入weight等其他的參數,hash_method是使用的hash算法。
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
3.3 Proxy_pass 指令
3.3.1官方文檔:
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
3.3.2官方定義:
This module makes it possible to transfer requests to another server.
此子產品可以将請求轉發到另一台伺服器
3.4 Location 指令
Nginx中的location指令是NginxHttpCoreModule中重要指令。location比較簡單且常用。
location 指令,是用來對rul進行比對的,URI即文法中的/uri/,可以是字元串或正規表達式。但是如果是正則表達,必須指定字首。
3.4.1 基本文法
1)文法:
location [=|~|~*|^~|@] /uri/ {···}
2)解釋:
[ = ] 精确比對,如果找到,立即停止搜尋,并立即處理請求(優先級最高)
[ ~ ] 區分大小寫
[ ^~ ] 之比對字元串,不比對正規表達式
[ ~*] 不區分大小寫
[ @ ] 指定一個命名的location,一般隻用于内部重定向請求
3)比對過程
首先對字元串進行比對查詢,最确切的比對将被使用。然後,正規表達式的比對查詢開始,比對第一個結果後會停止搜尋,如果沒有找到正規表達式,将使用字元串的搜尋結果,如果字元串和正則都比對,那麼正則優先級較高。
提示:本文沒有針對location的比對順序,進行測試,總結起來就是“精确比對優先”。具體怎麼個精确比對還需要讀者自己體會。
4. 反向代理和負載均衡配置(後端虛拟主機請自行配置)
4.1 L4 負載均衡
4.1.1 Upstraem配置
1)權重輪詢
server 192.168.18.211:80 weight=2;
server 192.168.18.212:80 weight=3;
server 192.168.18.213:80 weight=4;
2)ip_hash
server 192.168.18.211:80;
server 192.168.18.212:80;
server 192.168.18.213:80 down;
4.1.2 Server配置
server {
listen 80;
server_name nginx.san.com;
location / {
proxy_pass http://engine;
4.2 L7 負載均衡
4.2.1 根據URI轉發
4.2.1.1 Upstream配置
upstream nginx {
upstream php {
upstream java {
server 192.168.18.213:80;
4.2.1.2 虛拟主機配置
location /nginx/ {
proxy_pass http://nginx/;
location /php/ {
proxy_pass http://php/;
location /java/ {
proxy_pass http://java/;
提示:關于結尾這個“/”問題我這沒有測試,大家回去有興趣的可以測試下!
4.2.2 根據擴充名轉發(需要正規表達式支援)
4.2.2.1 Upstream配置
server 192.168.18.211:81;
server 192.168.18.211:82;
4.2.2.2 虛拟主機配置
location ~* /(.*\.jpg)$ {
proxy_pass http://nginx/$1;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
location ~* /(.*\.gif)$ {
proxy_pass http://php/$1;
proxy_set_header Host $host;
location ~* /(.*\.png)$ {
proxy_pass http://java/$1;
4.3 Nginx SSL 加密代理
配置檔案(沒有測試多台證書伺服器)
listen 443 ssl; <==指定https端口,這個“ssl”必須有不然報錯
server_name sso.eefocus.com;
#access_log logs/sss-access.log;
#error_log logs/ssl-error.log;
##SSL cert files##
ssl_certificate /usr/local/nginx/ssl/ee_com.crt;
#ssl_certificate_key /usr/local/nginx/ssl/ees_com.key;
ssl_certificate_key /usr/local/nginx/ssl/ee_com-nopass.key;
#keepalive_timeout 60;
location / {
proxy_pass https://192.168.18.61;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
#proxy_set_header X-Forwarded-Proto https;
4.4 WEB日志用戶端IP記錄
4.4.1 X-Forwarded-For 字段
解釋(維基百科):
X-Forwarded-For(XFF)是用來識别通過HTTP代理或負載均衡方式連接配接到Web伺服器的用戶端最原始的IP位址的HTTP請求頭字段。
位址:http://zh.wikipedia.org/wiki/X-Forwarded-For
4.4.2 Apeche(WEB)
4.4.2.1 Nginx配置
location ~* /(.*\.png)$ {
proxy_set_header Host $host;
4.4.2.2 Apache日志格式配置(這裡和HAProxy一樣)
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
提示:
1 注意大括号後面還有一個“i”;
2 (自己測試)大括号内“X-Forwarded-For”的值可以随意定義隻要和Nginx中X-Forwarded-For 一緻,不區分大小寫
4.4.3 Nginx(WEB)
4.4.3.1 Nginx(Proxy)
location ~* /(.*\.jpg)$ {
4.4.3.2 Nginx(WEB)日志格式配置
log_format main '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" ';
4.5 Nginx健康檢查
4.5.1 upstream 配置(詳細内容前面已經提到)
server 10.0.11.82:81 weight=1;
server 10.0.11.83:80 weight=3 max_fails=5 fail_timeout=10s;
4.5.2 server 配置
server_name php.san.com;
proxy_pass http://php;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;