免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
nginx反向代理-負載均衡-URL重寫

拓撲圖:

 

一、nginx代理服務(wù)器安裝配置:

1)安裝Nginx:

1、解決依賴關(guān)系

編譯安裝nginx需要事先需要安裝開發(fā)包組"Development Tools"和 "Development Libraries"。同時,還需要專門安裝pcre-devel包:
# yum -y install pcre-devel
2、安裝
首先添加用戶nginx,實現(xiàn)以之運行nginx服務(wù)進程:
  1. # groupadd -r nginx  
  2. # useradd -r -g nginx –s /bin/false -M nginx  
  3. # tar xf nginx-1.2.3.tar.gz  
  4. # cd nginx-1.2.3  
接著開始編譯和安裝:
  1. # ./configure \ 
  2.   --prefix=/usr \           //指定nginx安裝目錄 
  3.   --sbin-path=/usr/sbin/nginx \      //指定nginx可執(zhí)行二進制文件存放目錄 
  4.   --conf-path=/etc/nginx/nginx.conf \  //指定nginx配置文件所在目錄 
  5.   --error-log-path=/var/log/nginx/error.log \  //指定nginx錯誤日志所在目錄 
  6.   --http-log-path=/var/log/nginx/access.log \  //指定HTTP訪問日志文件目錄 
  7.   --pid-path=/var/run/nginx/nginx.pid  \       //指定nginx的PID文件所在目錄 
  8.   --lock-path=/var/lock/nginx.lock \      //指定nginx的lock文件所在目錄 
  9.   --user=nginx \             //為nginx工作進程指定非特權(quán)用戶 
  10.   --group=nginx \            //為nginx工作進程指定非特權(quán)用戶組 
  11.   --with-http_ssl_module \    //加載ngx_http_ssl_module模塊,使其支持ssl功能 
  12.   --with-http_flv_module \   //加載ngx_http_flv_module模塊 
  13.   --with-http_stub_status_module \   //加載ngx_http_stub_status_module模塊 
  14.   --with-http_gzip_static_module \   //加載ngx_http_gzip_static_module模塊  
  15.   --http-client-body-temp-path=/var/tmp/nginx/client/ \  //指定HTTP客戶端請求主體臨時存放目錄  
  16.   --http-proxy-temp-path=/var/tmp/nginx/proxy/ \   //指定HTTP代理臨時文件存放目錄 
  17.   --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \   //指定HTTP的Fastcgi臨時文件存放目錄 
  18.   --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \   //指定HTTP的Uwsgi臨時文件存放目錄 
  19.   --http-scgi-temp-path=/var/tmp/nginx/scgi \    //指定HTTP的Scgi臨時文件存放目錄 
  20.   --with-pcre    //強制支持pcre庫 
  21. # make && make install 

2)為nginx提供SysV init腳本:

  1. 新建文件/etc/rc.d/init.d/nginx,內(nèi)容如下:  
  2. #!/bin/sh  
  3. #  
  4. # nginx - this script starts and stops the nginx daemon  
  5. #  
  6. # chkconfig:   - 85 15   
  7. # description:  Nginx is an HTTP(S) server, HTTP(S) reverse \  
  8. #               proxy and IMAP/POP3 proxy server  
  9. # processname: nginx  
  10. # config:      /etc/nginx/nginx.conf  
  11. # config:      /etc/sysconfig/nginx  
  12. # pidfile:     /var/run/nginx.pid  
  13.    
  14. # Source function library.  
  15. . /etc/rc.d/init.d/functions  
  16.    
  17. # Source networking configuration.  
  18. . /etc/sysconfig/network  
  19.    
  20. # Check that networking is up.  
  21. [ "$NETWORKING" = "no" ] && exit 0  
  22.    
  23. nginx="/usr/sbin/nginx"  
  24. prog=$(basename $nginx)  
  25.    
  26. NGINX_CONF_FILE="/etc/nginx/nginx.conf"  
  27.    
  28. [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx  
  29.    
  30. lockfile=/var/lock/subsys/nginx  
  31.    
  32. make_dirs() {  
  33.    # make required directories  
  34.    user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`  
  35.    options=`$nginx -V 2>&1 | grep 'configure arguments:'`  
  36.    for opt in $options; do  
  37.        if [ `echo $opt | grep '.*-temp-path'` ]; then  
  38.            value=`echo $opt | cut -d "=" -f 2`  
  39.            if [ ! -d "$value" ]; then  
  40.                # echo "creating" $value  
  41.                mkdir -p $value && chown -R $user $value  
  42.            fi  
  43.        fi  
  44.    done  
  45. }  
  46.    
  47. start() {  
  48.     [ -x $nginx ] || exit 5  
  49.     [ -f $NGINX_CONF_FILE ] || exit 6  
  50.     make_dirs  
  51.     echo -n $"Starting $prog: "  
  52.     daemon $nginx -c $NGINX_CONF_FILE  
  53.     retval=$?  
  54.     echo  
  55.     [ $retval -eq 0 ] && touch $lockfile  
  56.     return $retval  
  57. }  
  58.    
  59. stop() {  
  60.     echo -n $"Stopping $prog: "  
  61.     killproc $prog -QUIT  
  62.     retval=$?  
  63.     echo  
  64.     [ $retval -eq 0 ] && rm -f $lockfile  
  65.     return $retval  
  66. }  
  67.    
  68. restart() {  
  69.     configtest || return $?  
  70.     stop  
  71.     sleep 1  
  72.     start  
  73. }  
  74.    
  75. reload() {  
  76.     configtest || return $?  
  77.     echo -n $"Reloading $prog: "  
  78.     killproc $nginx -HUP  
  79.     RETVAL=$?  
  80.     echo  
  81. }  
  82.    
  83. force_reload() {  
  84.     restart  
  85. }  
  86.    
  87. configtest() {  
  88.   $nginx -t -c $NGINX_CONF_FILE  
  89. }  
  90.    
  91. rh_status() {  
  92.     status $prog  
  93. }  
  94.    
  95. rh_status_q() {  
  96.     rh_status >/dev/null 2>&1  
  97. }  
  98.    
  99. case "$1" in  
  100.     start)  
  101.         rh_status_q && exit 0  
  102.         $1  
  103.         ;;  
  104.     stop)  
  105.         rh_status_q || exit 0  
  106.         $1  
  107.         ;;  
  108.     restart|configtest)  
  109.         $1  
  110.         ;;  
  111.     reload)  
  112.         rh_status_q || exit 7  
  113.         $1  
  114.         ;;  
  115.     force-reload)  
  116.         force_reload  
  117.         ;;  
  118.     status)  
  119.         rh_status  
  120.         ;;  
  121.     condrestart|try-restart)  
  122.         rh_status_q || exit 0  
  123.             ;;  
  124.     *)  
  125.         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"  
  126.         exit 2  
  127. esac  
而后為此腳本賦予執(zhí)行權(quán)限:
  1. # chmod +x /etc/rc.d/init.d/nginx  
添加至服務(wù)管理列表,并讓其開機自動啟動:
  1. # chkconfig --add nginx  
  2. # chkconfig nginx on  
而后就可以啟動服務(wù)并測試了:
  1. # service nginx start  

二、apache服務(wù)器配置

  1. 1)安裝apache: 
  2. # yum -y install httpd 
  3. 2)安裝php 
  4. # yum -y install php 
  5. 添加php測試頁面 
  6. # vim /var/www/html/index.php 
  7. 內(nèi)容如下: 
  8. <?php 
  9. phpinfo() 
  10. ?> 
  11. 重啟httpd服務(wù)并訪問試試 
  12. # service httpd restart 

三、配置172.16.11.11的nginx代理172.16.11.21的apache的http的服務(wù)

Nginx實現(xiàn)代理需要proxy模塊,默認nginx已經(jīng)具備這一功能,無需進行編譯該模塊,我們要做的就是配置nginx的主配置文件即可;
編輯nginx的主配置文件,找到nginx的默認server的location選項
注釋以下兩行:
  1. #root   html;  
  2. #index  index.html index.htm;  

 添加如下行,制定代理的web服務(wù)器地址

  1. proxy_pass http://172.16.11.21; 
檢查nginx配置文件并重啟nginx服務(wù)
  1. # service nginx configtest 
  2. # service nginx restart 
驗證nginx代理后端web服務(wù)的效果

通過以上配置nginx成功實現(xiàn)了反向代理后端web服務(wù)的作用,但是這種方法并不能在nginx代理服務(wù)器上實現(xiàn)緩存的功能,所以接下來我們就來說說怎樣在nginx上實現(xiàn)緩存;
在對nginx實現(xiàn)緩存之前,我們先對nginx的代理進行壓力測試,以便顯現(xiàn)出緩存的效果;
在其他另外的主機上執(zhí)行如下命令通過nginx代理進行壓力測試:
  1. # ab -n 10000 -c 100 http://172.16.11.11/index.php 
  2. Document Path:          /index.php 
  3. Document Length:        38239 bytes 
  4. Concurrency Level:      100 
  5. Time taken for tests:   154.738780 seconds 
  6. Complete requests:      10000 
  7. Failed requests:        0 
  8. Write errors:           0 
  9. Total transferred:      384000000 bytes 
  10. HTML transferred:       382390000 bytes 
  11. Requests per second:    64.63 [#/sec] (mean)    //每秒鐘響應(yīng)64個請求// 
  12. Time per request:       1547.388 [ms] (mean)    //每個請求用時1547毫秒// 
  13. Time per request:       15.474 [ms] (mean, across all concurrent requests) //并發(fā)請求100個用時15秒// 
  14. Transfer rate:          2423.44 [Kbytes/sec] received   //平均傳輸速率2423kb/s// 
  15. 在其他另外的主機上執(zhí)行如下命令通過直接訪問web頁面進行壓力測試: 
  16. # ab -n 10000 -c 100 http://172.16.11.21/index.php 
  17. Document Path:          /index.php 
  18. Document Length:        38025 bytes 
  19. Concurrency Level:      100 
  20. Time taken for tests:   69.886173 seconds 
  21. Complete requests:      10000 
  22. Failed requests:        0 
  23. Write errors:           0 
  24. Total transferred:      382159368 bytes 
  25. HTML transferred:       380437992 bytes 
  26. Requests per second:    143.09 [#/sec] (mean)    //每秒鐘響應(yīng)143個請求// 
  27. Time per request:       698.862 [ms] (mean)      //每個請求用時698毫秒// 
  28. Time per request:       6.989 [ms] (mean, across all concurrent requests) //并發(fā)請求100個用時6秒// 
  29. Transfer rate:          5340.14 [Kbytes/sec] received   //平均傳輸速率5340kb/s// 

通過以上壓力測試對比發(fā)現(xiàn),盡管采用nginx實現(xiàn)反向代理但nginx沒有緩存功能,頁面響應(yīng)速度不升反而會大大降低;

為nginx反代服務(wù)器提供緩存;

  1. 編輯nginx的主配置文件,找到http選項,添加如下內(nèi)容: 
  2. proxy_cache_path /var/www/cache levels=1:2 keys_zone=mycache:20m; //定義緩存路徑,緩存級別和內(nèi)存鍵區(qū)域名稱及空間大小// 
  3. max_size=2048m inactive=60m;  //最大緩存空間大小和非活動時長(超過該時長緩存過期)// 
  4. proxy_temp_path /var/www/cache/tmp;  //代理臨時目錄,需要事先創(chuàng)建// 
  5. 找到Location選項,添加如下內(nèi)容: 
  6. proxy_pass http://172.16.11.21;  //制定反代服務(wù)器// 
  7. proxy_cache mycache;  //定義緩存名稱,要與上述內(nèi)存鍵區(qū)域名稱保持一致// 
  8. proxy_cache_valid 200 302 60m;  //定義請求頁面響應(yīng)碼為200和302的頁面緩存時長為60分鐘// 
  9. proxy_cache_valid 404 1m;   //定義請求頁面響應(yīng)碼為404的頁面緩存時長為1分鐘// 
  10. 創(chuàng)建緩存目錄 
  11. # mkdir /var/www/cache/tmp -pv 
  12. 檢查nginx配置文件語法并重啟nginx服務(wù) 
  13. # service nginx configtest 
  14. # service nginx restart 
  15. 首先在其他另外的主機上執(zhí)行幾次如下命令通過nginx代理進行壓力測試使緩存中充滿訪問數(shù)據(jù); 
  16. # ab -n 1000 -c 20 http://172.16.11.11/index.php 
  17. 然后利用上述同樣的測試方法通過nginx代理進行壓力測試 
  18. # ab -n 10000 -c 100 http://172.16.11.11/index.php 
  19. Document Path:          /index.php 
  20. Document Length:        38239 bytes 
  21. Concurrency Level:      100 
  22. Time taken for tests:   79.248259 seconds 
  23. Complete requests:      10000 
  24. Failed requests:        0 
  25. Write errors:           0 
  26. Total transferred:      384478146 bytes 
  27. HTML transferred:       382865409 bytes 
  28. Requests per second:    126.19 [#/sec] (mean)   //每秒鐘響應(yīng)126個請求// 
  29. Time per request:       792.483 [ms] (mean)     //每個請求用時792毫秒// 
  30. Time per request:       7.925 [ms] (mean, across all concurrent requests) //并發(fā)請求100個用時7秒// 
  31. Transfer rate:          4737.85 [Kbytes/sec] received  //平均傳輸速率4737kb/s// 

通過上述對nginx反向代理進行壓力測試前后對比發(fā)現(xiàn),自從引入了nginx的緩存功能大大提升了頁面響應(yīng)速度,同時也很好的提升的用戶的感知度;
以上就是nginx反向代理引入緩存功能的介紹;

四、配置nginx代理服務(wù)器使用戶訪問的頁面部分代理到后臺服務(wù)器響應(yīng)

配置nginx主配置文件,在server選項下定義兩個location
  1. location / {                     //定義訪問路徑為”/”時直接在nginx服務(wù)器響應(yīng)// 
  2.             root   html; 
  3.             index  index.html index.htm; 
  4. location /images/ {      //定義訪問路徑為”/images/”時代理到Apache服務(wù)器響應(yīng)// 
  5.              proxy_pass http://172.16.11.21/images/; 
  6.              proxy_cache mycache; 
  7.              proxy_cache_valid 200 302 60m; 
  8.              proxy_cache_valid 404 1m; 
配置apache服務(wù)器在主目錄下面新建images目錄并新建頁面
  1. # mkdir /var/www/html/images 
  2. # vim /var/www/html/images/index.html 
  3. 添加如下內(nèi)容: 
  4. <h1>apache</h1> 
  5. 重啟nginx和后端apache服務(wù)器 
  6. # service nginx restart 
  7. # service httpd restart 
此時訪問一下試試

通過以上訪問結(jié)果成功的實現(xiàn)了部分代理后端服務(wù)響應(yīng)用戶請求。

五、配置nginx代理服務(wù)器實現(xiàn)負載均衡,需要用到nginx的upstream模塊

1)配置nginx主配置文件,在http選項下添加如下內(nèi)容
  1. upstream cluster {                   //定義后端負載均衡服務(wù)器名稱// 
  2.         server 172.16.11.21 weight=1;  //指定后端服務(wù)器和權(quán)重// 
  3.         server 172.16.11.22 weight=2; 
  4. 在server選項下添加如下內(nèi)容 
  5. location / { 
  6.              proxy_pass http://cluster;   //反代指向定義的負載均衡服務(wù)器// 
2)這里新增一臺apache服務(wù)器,安裝方法見步驟一,這里添加測試頁面index.html,內(nèi)容為<h1>RS2</h1>
3)這里將nginx的緩存功能去掉,為了演示效果;
驗證以下nginx的負載均衡的效果吧

結(jié)果證明nginx反向代理服務(wù)器方便快捷的實現(xiàn)的負載均衡的功能;

解析:

upstream
   語法:upstream name { ... } //聲明一組可以被proxy_pass和fastcgi_pass引用的服務(wù)器;這
         些服務(wù)器可以使用不同的端口,并且也可以使用Unix Socket;也可以為服務(wù)器指定不同
         的權(quán)重//
server
   語法:server name [parameters] //其中的name可以是FQDN,主機地址,端口或unix套接字;
   如果FQDN解析的結(jié)果為多個地址,則每個地址都會被用到;[parameters]有以下選項:
weight = NUMBER - 設(shè)定權(quán)重,默認為1.
max_fails = NUMBER - 在fail_timeout指令設(shè)定的時間內(nèi)發(fā)往此server的不成功的請求次數(shù),達到
   此數(shù)目后,此服務(wù)器將變?yōu)椴豢刹僮鳡顟B(tài);默認值為1;設(shè)定為0值則禁用此功能;
fail_timeout = TIME - 默認為10秒;
down - marks server as permanently offline, to be used with the directive ip_hash.
backup - (0.6.7 or later) only uses this server if the non-backup servers are all down
   or busy (cannot be used with the directive ip_hash)//

六、配置nginx實現(xiàn)URL重寫

首先講解以下URL重寫的相關(guān)知識:

URL重寫用到的指令是rewrite

rewrite指令的用法:
rewrite regex replacement flag 即將regex替換為replacemen+flag標志位的格式;
比如:rewrite /images/(.*\.jpg) http://172.16.0.1/images/$1 即web瀏覽器中出現(xiàn)/images/(.*\.jpg)的訪問路徑格式將被替換為http://172.16.0.1/images/$1路徑訪問;
上述/images/(.*\.jpg)路徑還可以寫成^/images/(.*\.jpg)或者^/images/(.*\.jpg)$等,這里意思很明顯,相信你懂的;
注意:/images/(.*\.jpg)路徑指的是瀏覽器中除了IP地址以外的路徑;
flag標志位講解:
在apache模塊中標志位很多,而nginx的標志位僅有4種:
last - completes processing of current rewrite directives and restarts the process (including rewriting) with a search for a match on the URI from all available locations.(表示匹配到的訪問路徑進行持續(xù)性檢查,但是容易造成死循環(huán));
break - completes processing of current rewrite directives and non-rewrite processing continues within the current location block only.( 表示匹配到的訪問路徑只進行一次性檢查,有效的解決了死循環(huán)的問題);
redirect - returns temporary redirect with code 302; it is used if the substituting line begins with http://(表示臨時重定向,返回代碼302);
permanent - returns permanent redirect with code 301(表示永久重定向,返回代碼301);
例如:設(shè)置一個簡單的URL重寫:
某網(wǎng)站原有的論壇訪問路徑為/forum/,但后來根據(jù)要求需要更改為/bbs,于是,就可以通過下面的方法實現(xiàn):rewrite ^/forum/?$ /bbs/ permanent;
其中/bbs/是實實在在存在的路徑;
在進行URL重寫的時候經(jīng)常會用到if判斷語句,當滿足一定條件時進行URL重寫;
1、if指令:
語法: if (condition) { ... }
應(yīng)用環(huán)境: server, location
條件表達式:
1、變量名; false values are: empty string ("", or any string starting with "0";)
2、對于變量進行的比較表達式,可使用=或!=進行測試;
3、正則表達式的模式匹配:
~ 區(qū)分字母大小寫的模式匹配
~* 不區(qū)分字母大小寫的模式匹配
!~ 和 !~* 分別對上面的兩種測試取反
4、測試文件是否存在-f或!-f
5、測試目錄是否存在-d或!-d
6、測試目錄、文件或鏈接文件的存在性-e或!-e
7、檢查一個文件的執(zhí)行權(quán)限-x或!-x
在正則表達式中,可以使用圓括號標記匹配到的字符串,并可以分別使用$1,$2,...,$9進行引用;
例如:
1)判斷用戶的瀏覽器類型(常用到手機和PC機瀏覽器):
  if ($http_user_agent ~* MSIE) {
     rewrite ^(.*)$ /msie/$1 break;
  }
  if ($http_user_agent ~* opera) {
     rewrite ^(.*)$ /opera/$1 break;
  }
2)如果用戶請求的頁面不存在,實現(xiàn)自定義跳轉(zhuǎn):
  if (!-f $request_filename) {
     rewrite ^(/.*)$ /rewrite.html permanent;
  }
例如:編輯nginx的主配置文件,在server中的location中添加如下內(nèi)容:
  location / {
    root html;
    index index.html index.htm;
    if (!-f $request_filename) {
    rewrite /.* /error.html permanent;
    }
  }
在/usr/html中創(chuàng)建error.html頁面:內(nèi)容為ERROR
重啟nginx服務(wù)并訪問:

3)實現(xiàn)域名跳轉(zhuǎn)
  1. server { 
  2. listen 80; 
  3. server_name jump.magedu.com; 
  4. index index.html index.php; 
  5. root /www/htdocs; 
  6. rewrite ^/ http://www.magedu.com/; 
  7. }       //如果用戶訪問的是jump.magedu.com則跳轉(zhuǎn)到http://www.magedu.com/ 
  8. 例如:在nginx主配置文件server中添加如下內(nèi)容: 
  9. server { 
  10. listen 80; 
  11. server_name test.magedu.com; 
  12. index index.html index.php; 
  13. rewrite ^/ http://tomcat.magedu.com/; 
  14. 在本地主機hosts文件中添加如下內(nèi)容: 
  15. 172.16.11.11 test.magedu.com 
  16. 172.16.11.21 tomcat.magedu.com 
重啟nginx服務(wù)并訪問:

4)實現(xiàn)域名鏡像
  1. server { 
  2. listen 80; 
  3. server_name mirror.magedu.com; 
  4. index index.html index.php; 
  5. root /www/htdocs; 
  6. rewrite ^/(.*)$ http://www.magedu.com/$1 last; 
5)簡單的防盜鏈配置:
  1. location ~* \.(gif|jpg|png|swf|flv)$ { 
  2.   valid_referers none blocked www.magedu.com; 
  3.   if ($invalid_referer) { 
  4.     rewrite ^/ http://www.magedu.com/403.html; 
  5.     # return 404 
  6.   } 
  7. }   
解析:
   第一行:gif|jpg|png|swf|flv,表示對gif、jpg、png、swf、flv后綴的文件實行防盜鏈
   第二行:www.magedu.com,表示對www.magedu.com這個來路進行判斷if{}里面內(nèi)容的意思,如果
           來路不是指定來路就跳轉(zhuǎn)到錯誤頁面,當然直接返回404也是可以的。

更多延伸內(nèi)容如下:

  1. if (! -e $request_filename) { 
  2.       rewrite ^/user/([0-9]+)/?$ /view.php?go=user_$1 last; 
  3.       rewrite ^/component/id/([0-9]+)/?$ /page.php?pageid=$1 last; 
  4.       rewrite ^/component/([^/]+)/?$ /page.php?pagealias=$1 last; 
  5.       rewrite ^/category\_([0-9]+)\.htm$ http://$host/category/$1/ permanent; 
  6.       rewrite ^/showday\_([0-9]+)\_([0-9]+)\_([0-9]+)\.htm$ http://$host/date/$1/$2/$3/ permanent; 
  7.       showday_1_2_3.htm $host/date/1/2/3/ 

常用的變量:

  1. $arg_PARAMETER        This variable contains the value of the GET request variable PARAMETER if present in the query string. 
  2. $args                 This variable contains the query string in the URL, for example foo=123&bar=blahblah if the URL is http://example1. com/? foo=123&bar=blahblah 
  3. $binary_remote_addr   The address of the client in binary form. 
  4. $body_bytes_sent      The bytes of the body sent. 
  5. $content_length       This variable is equal to line Content-Length in the header of request. 
  6. $content_type         This variable is equal to line Content-Type in the header of request. 
  7.  
  8. $document_root        This variable is equal to the value of directive root for the current request. 
  9. $document_uri         The same as $uri. 
  10. $host                 This variable contains the value of the 'Host' value in the request header, or the name of the server processing if the 'Host' value is not available. 
  11. $http_HEADER          The value of the HTTP header HEADER when converted to lowercase and with "dashes" converted to "underscores", for example, $http_user_agent, $http_referer. 
  12. $is_args              Evaluates to "?" if $args is set, returns "" otherwise. 
  13. $request_uri          This variable is equal to the *original* request URI as received from the client including the args. It cannot be modified. Look at $uri for the post-rewrite/altered URI. Does not include host name. Example: "/foo/bar.php?arg=baz". 
  14. $scheme               The HTTP scheme (that is http, https). Evaluated only on demand, for example: rewrite ^(.+)$ $scheme://example.com$1 redirect; 
  15. $server_addr          This variable contains the server address. It is advisable to indicate addresses correctly in the listen directive and use the bind parameter so that a system call is not made every time this variable is accessed. 
  16. $server_name          The name of the server. 
  17. $server_port          This variable is equal to the port of the server, to which the request arrived. 
  18. $server_protocol      This variable is equal to the protocol of request, usually this is HTTP/1.0 or HTTP/1.1. 
  19. $uri                  This variable is equal to current URI in the request (without arguments, those are in $args.) It can differ from $request_uri which is what is sent by the browser. Examples of how it can be modified are internal redirects, or with the use of index. Does not include host name. Example: "/foo/bar.html" 

七、安裝配置nginx第三方模塊,實現(xiàn)upstream中對后端http server的健康狀態(tài)檢測:

模塊下載地址:https://github.com/cep21/healthcheck_nginx_upstreams;模塊名稱:ngx_http_healthcheck_module
安裝配置方法:
1、首先解壓healcheck模塊到某路徑下,這里假設(shè)為/health/healthcheck_nginx_upstreams
  1. # mkdir /health/ 
  2. # unzip healthcheck_nginx_upstreams.zip -d /health/ 
  3. # mv /health/cep21-healthcheck_nginx_upstreams-16d6ae7 /health/healthcheck_nginx_upstreams 
2、對nginx打補丁
  1. 首先解壓nginx,并進入nginx源碼目錄: 
  2. # tar xf nginx-1.0.14.tar.gz //注:需要nginx版本匹配,不支持nginx-1.2.2及以上版本
  3. # cd nginx-1.0.14 
  4. # patch -p1 < /health/healthcheck_nginx_upstreams/nginx.patch 
  5. 而后編譯nginx,在執(zhí)行configure時添加類似下面的選項: 
  6. --add-module=/health/healthcheck_nginx_upstreams 
  7. 所以,這里就使用如下命令: 
  8. # ./configure \ 
  9.   --prefix=/usr \ 
  10.   --sbin-path=/usr/sbin/nginx \ 
  11.   --conf-path=/etc/nginx/nginx.conf \ 
  12.   --error-log-path=/var/log/nginx/error.log \ 
  13.   --http-log-path=/var/log/nginx/access.log \ 
  14.   --pid-path=/var/run/nginx/nginx.pid  \ 
  15.   --lock-path=/var/lock/nginx.lock \ 
  16.   --user=nginx \ 
  17.   --group=nginx \ 
  18.   --with-http_ssl_module \ 
  19.   --with-http_flv_module \ 
  20.   --with-http_stub_status_module \ 
  21.   --with-http_gzip_static_module \ 
  22.   --http-client-body-temp-path=/var/tmp/nginx/client/ \ 
  23.   --http-proxy-temp-path=/var/tmp/nginx/proxy/ \ 
  24.   --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \ 
  25.   --with-pcre \ 
  26.   --add-module=/health/healthcheck_nginx_upstreams 
  27. # make && make install 
  28. 編輯nginx的主配置文件: 
  29. http選項中添加: 
  30. upstream cluster { 
  31.         server 172.16.11.21 weight=1; 
  32.         server 172.16.11.22 weight=2; 
  33.      healthcheck_enabled; 
  34.         healthcheck_delay 1000; 
  35.         healthcheck_timeout 1000; 
  36.         healthcheck_failcount 2; 
  37.         healthcheck_expected 'OK'; 
  38.         healthcheck_send "GET /.health HTTP/1.0"; 
  39. Server中添加location選項內(nèi)容: 
  40. location / {  
  41.             proxy_pass http://cluster; 
  42. location /stat { 
  43.       healthcheck_status; 
  44.   } 
重啟nginx和后端apache服務(wù)并訪問:

關(guān)閉172.16.11.21apache服務(wù)

以上結(jié)果表明nginx反向代理成功實現(xiàn)了對后端代理服務(wù)器的健康狀況檢查;

ngx_http_healthcheck_module模塊的使用方法:

1、此模塊支持的指令有:

healthcheck_enabled 啟用此模塊
healthcheck_delay對同一臺后端服務(wù)器兩次檢測之間的時間間隔,單位毫秒,默認為1000;
healthcheck_timeout進行一次健康檢測的超時時間,單位為毫秒,默認值2000;
healthcheck_failcount對一臺后端服務(wù)器檢測成功或失敗多少次之后方才確定其為成功或失敗,并實現(xiàn)啟用或禁用此服務(wù)器;
healthcheck_send為了檢測后端服務(wù)器的健康狀態(tài)所發(fā)送的檢測請求;如:healthcheck_send "GET /health HTTP/1.0" 'Host: www.magedu.com';//注意僅支持HTTP1.0版本協(xié)議//
healthcheck_expected期望從后端服務(wù)器收到的響應(yīng)內(nèi)容;如果未設(shè)置,則表示從后端服務(wù)器收到200狀態(tài)碼即為正確;
healthcheck_buffer健康狀態(tài)檢查所使用的buffer空間大?。?br>healthcheck_status通過類似stub_status的方式輸出檢測信息,使用方法如下:
location /stat {
healthcheck_status;
}

原文地址:http://wjw7702.blog.51cto.com/5210820/1095989

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Nginx 反向代理、負載均衡、頁面緩存、URL重寫及讀寫分離詳解
nginx應(yīng)用總結(jié)(1)--基礎(chǔ)認識和應(yīng)用配置
nginx 常用模塊整理-2 -- 51cto.大飛俠大蝦
【Nginx22】Nginx學習:FastCGI模塊(四)錯誤處理及其它
CentOS下安裝Nginx并實現(xiàn)web功能
Nginx
更多類似文章 >>
生活服務(wù)
分享 收藏 導長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服