Nginx實現反向代理,負載均衡,url重寫以及健康檢測

一,先對Nginx打補丁,以實現對後臺服務器的健康狀況進行檢測,nginx源碼,補丁,nginx的服務控制腳本已經上傳到附件。
注意在實驗中前端服務器並沒有使用兩塊網卡,但是在實際應用肯定需要兩塊網卡,一個內網,一個用於接收外網請求。兩臺web服務器都需要安裝http軟件。
#yum  -y  install pcre-devel(nginx的rewrite需要依賴)
#useradd -r nginx -s /sbin/noglogin
# unzip healthcheck_nginx_upstreams.zip 
#tar xf nginx-1.0.14.tar.gz
# cd nginx-1.0.14
#patch  -p1 </root/cep21-healthcheck_nginx_upstreams-16d6ae7/nginx.patch 
#./configure \
  --prefix=/usr \
  --sbin-path=/usr/sbin/nginx \
  --conf-path=/etc/nginx/nginx.conf \
  --error-log-path=/var/log/nginx/error.log \
  --http-log-path=/var/log/nginx/access.log \
  --pid-path=/var/run/nginx/nginx.pid  \
  --lock-path=/var/lock/nginx.lock \
  --user=nginx \
  --group=nginx \
  --with-http_ssl_module \
  --with-http_flv_module \
  --with-http_stub_status_module \
  --with-http_gzip_static_module \
  --http-client-body-temp-path=/var/tmp/nginx/client/ \
  --http-proxy-temp-path=/var/tmp/nginx/proxy/ \
  --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
  --with-pcre \
  --add-module=/root/cep21-healthcheck_nginx_upstreams-16d6ae7/nginx.patch 
#make && make install
將服務控制腳本複製到/etc/rc.d/init.d/目錄下,並給執行權限chmod +x /etc/rc.d/init.d/nginx。
啓動服務並測試nginx能否正常。下面開始實現nginx的各種功能:
二,實現簡單反代,但是沒有緩存。沒有緩存的話只做反代還會降低客戶端的響應速度。
Vim  /etc/nginx/nginx.conf
location / {/表示無論客戶端請求什麼內容都代理到http://17216.150.22
           # root   html;
           # index  index.html index.htm;
           proxy_pass http://172.16.150.22;
  
   }
  
三,爲反代提供緩存,在配置文件中添加下面
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;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;
     proxy_cache_path /tmp/nginx/cache       levels=1:2  keys_zone=STATIC:10m
       定義緩存的目錄  inactive=24h   max_size=1g;
levels=1:2表示記錄的緩存級別的1級目錄一個字符,2級目錄2個字符)
proxy_temp_path /tmp/nginx/tmp keys_zone表示在內存中將緩存建立索引存儲在STATIC這個變量,且最大爲10
緩存的臨時目錄 inactive=24h非活動連接保存24h小時,max_size=1g緩存大保存1g。
    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / { 
           # root   html;
           # index  index.html index.htm;
            proxy_pass http://172.16.150.22;
           proxy_set_header Host  172.16.150.22;
           proxy_cache          STATIC;
           proxy_cache_valid    200 302 1d;爲返回碼爲200,302的請求緩存1天
           proxy_cache_valid    404 1m; 爲返回碼爲404的請求緩存1分鐘,404爲錯誤頁面
       
}
每次修改完配置文件的內容需要重啓nginx服務或重新載入。不然新的配置不會生效。
查看剛纔的levels的效果。
# ll /tmp/nginx/cache/8/a8/bc72520e6a590d2bea21b7264dcafa88 
-rw------- 1 nginx nginx 321 Aug  9 09:15 
  8/a8表示1:2
# cat /tmp/nginx/cache/8/a8/bc72520e6a590d2bea21b7264dcafa88 
四,如果只代理動態頁面,則靜態頁面由nginx自己響應用戶。下面的配置當用戶
訪問http://172.16.150.1/images這個路徑則使用後端服務器來響應
location / {
            root   html;
            index  index.html index.htm;
          }
        location /images/ {
           proxy_pass http://172.16.150.22/images;在後臺服務器的網頁根目錄需要創建這個目錄
           proxy_set_header Host  172.16.150.22;
           proxy_cache          STATIC;
           proxy_cache_valid    200 302 1d;
           proxy_cache_valid    404 1m;
           proxy_cache_use_stale  error timeout invalid_header updating
                                  http_500 http_502 http_503 http_504;
        }
在後臺服務器上創建目錄與提供頁面
# cd /var/www/html/images
# vim index.html
五,如果後端有多臺服務器,nginx還可以實現負載均衡
    upstream myhttpd { 對後臺服務器設置一個組,這組中添加添加服務器
        server 172.16.150.22 weight=5; weight=5表示設置服務器的權重
        server 172.16.150.30 weight=5;
        }
    server {
        listen       80;
        server_name  localhost;
#charset koi8-r;
#access_log  logs/host.access.log  main;
#   location / {
        #    root   html;
        #    index  index.html index.htm;
        #  }
        location / {
           proxy_pass http://myhttpd;
        #   proxy_cache         STATIC;如果不註釋下,看不到效果
        #   proxy_cache_valid   200 302 1d;
        #   proxy_cache_valid    404 1m;
           #proxy_cache_use_stale  error timeout invalid_header updating
        #                         http_500 http_502 http_503 http_504;
}
六,實現URL重寫
# upstream myhttpd {
        #server 172.16.150.22 weight=5;
        #server 172.16.150.30 weight=5;
        #}
    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root   html;
            index  index.html index.htm;
            if (!-f $request_filename) {
                rewrite /.* /err.html permanent; (permanent表示永久的重定向到這個頁面)
*表示任意頁面,如果訪問不存在的任意網頁都重定向到http://172.16.150.21/err.html
err.html這個頁面需要存在。
          }
        #location / {
        #   proxy_pass http://myhttpd;
        #   proxy_cache         STATIC;
        #   proxy_cache_valid   200 302 1d;
        #   proxy_cache_valid    404 1m;
        #  proxy_cache_use_stale  error timeout invalid_header updating
        #   http_500 http_502 http_503 http_504;
}
爲nginx提供錯誤頁面。
vim /usr/html/err.html
爲某個目錄定義別名,用戶訪問的路徑其實並不存在,而是將其轉發到另外一個頁面。
     location / {
            root   html;
            index  index.html ;
            rewrite ^/forum/(.*) /bbs/$1 last ;
        #location / {
        #   proxy_pass http://myhttpd;
        #   proxy_cache         STATIC;
        #   proxy_cache_valid   200 302 1d;
        #   proxy_cache_valid    404 1m;
           #proxy_cache_use_stale  error timeout invalid_header updating
        #    http_500 http_502 http_503 http_504;
        }
 如果用戶訪問http://172.16.150.21/forum/則被http://172.16.150.21/bbs
# mkdir /usr/html/bbs 創建bbs路徑,並提供測試面
# vim /usr/html/bbs/index.html
七:實現域名跳轉
server {
        listen       80;
        server_name  test.magedu.com; 
如果訪問test.magedu.com則跳轉到tomcat.magedu.com
        rewrite ^/.* http://tomcat.magedu.com/$1 last;
 #charset koi8-r;
#access_log  logs/host.access.log  main;
 location / {
            root   html;
            index  index.html ;
}
八,檢查後端服務器健康狀態
upstream myhttpd {
        server 172.16.150.22 weight=5;
        server 172.16.150.30 weight=5;
        healthcheck_enabled; 開啓健康檢查功能
        healthcheck_delay 1000;   
對同一臺後端服務器兩次檢測之間的時間間隔,單位毫秒,默認爲1000。                healthcheck_timeout 1000;
進行一次健康檢測的超時時間,單位爲毫秒,默認值2000。
        healthcheck_failcount 3;
對一臺後端服務器檢測成功或失敗多少次之後方纔確定其爲成功或失敗,並實現啓用或禁用此服務器
        healthcheck_send "GET/.health HTTP/1.0";
對一臺後端服務器檢測成功或失敗多少次之後方纔確定其爲成功或失敗,並實現啓用或禁用此服務器,只支持http 1.0的協議
        }
    server {
        listen       80;
        #server_name  test.magedu.com;
        #rewrite ^/.* http://tomcat.magedu.com/$1 last;
#charset koi8-r;
        #access_log  logs/host.access.log  main;
# location / {
        #     root   html;
        #     index  index.html ;
        location / {
           proxy_pass http://myhttpd;
}
location /stat {
        healthcheck_status;
        }
訪問172.16.150.21/stat可以看到後臺服務器的運行是否正常 
本文出自 “bingo” 博客,請務必保留此出處http://bingodeng.blog.51cto.com/1038075/960545
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章