使用nginx代理weblogic負載方案


    之前一直用apache來做weblogic的前端,由於nginx對靜態內容的出色性能,不得不轉投nginx。這里就不
再寫weblogic的安裝了。

安裝nginx
nginx需要pcre做支持,一般系統都自帶,當然可以自己下載高版本的源碼包安裝,建議大家使用高版本的pcre,
這樣使用正則的時候會有更好的支持。
首先去http://wiki.nginx.org//NginxChs下載nginx,我用了0.7

# tar zxvf nginx-0.7.59.tar.gz
# cd nginx-0.7.59
# ./configure –with-http_stub_status_module –prefix=/opt/nginx
# make
# make install

–with-http_stub_status_module 增加nginx的監控模塊,用來監控 Nginx 的當前狀態。

下面來配置nginx
配置文件,在/opt/nginx/conf下的nginx.conf,為了省事,直接復制我的配置文件過來:
#user  nobody;
worker_processes  10;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
 use epoll;        //nginx處理連接的方法,epoll – 高效的方法,使用於Linux內核2.6版本及以后的系統。
 worker_connections  51200;
}


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  75;

    #gzip  on;


 upstream 99ding {
         server 124.42.*.***:7002 weight=10;
         server 124.42.*.***:7001 weight=10;
    }        //因為我的weblogic沒有做集群,所以只能用nginx自帶的負載均衡,不過推薦使用weblogic的集群

    server {
        listen       80;
        server_name  www.test.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

         location ~ ^/status/ {
            stub_status on;
            access_log off;
         }       //監控nginx的狀態:http://www.test.com/status/


        location / {
            root   /opt/html/app;
            index  index.html index.htm;
            expires 30d;    //expires是設定cookie的存活時間,我用了30天
        }

         location ~ ^/(WEB-INF)/ {
            deny all;
         }


         location ~ \.(htm|html|gif|jpg|jpeg|png|bmp|ico|rar|css|js|zip|txt|flv|swf|mid|doc|ppt|xls|pdf|txt|mp3|wma)$ {
             root /opt/html/app;
            expires 24h;
        }


        location ~ (\.jsp)|(\.do) {
            proxy_pass  http://test;
            proxy_set_header    X-Real-IP  $remote_addr;

            proxy_set_header    Host       $host;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
            proxy_buffer_size 4k;
            proxy_buffers 4 32k;
            proxy_busy_buffers_size 64k;
            proxy_temp_file_write_size 64k;
            proxy_max_temp_file_size 512m;
        }       //最關鍵的地方,將jsp和do文件轉給weblogic處理,這里使用的是上面創建的test(負載均衡的名字),如果不用

負載均衡,可以把test用 “ip:端口號”來代替,例如http://10.0.0.1:7001

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }


}


接下來啟動服務即可,首先檢查配置文件是否有錯誤:
/opt/nginx/sbin/nginx -t -c /opt/nginx/conf/nginx.conf
啟動服務:
/opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf

 

nginx用做weblogic前端七層負載

  • 准備安裝包(nginx和upstream_hash)
https://github.com/evanmiller/nginx_upstream_hash
 
http://nginx.org/download/

 

  • 編譯安裝
tar -zxvf nginx-0.7.65.tar.gz
unzip cep21-nginx_upstream_hash-99ace64.zip
 
patch -p0 < ../cep21-nginx_upstream_hash-99ace64/nginx.patch
patching file src/http/ngx_http_upstream.h
 
./configure –add-module=../cep21-nginx_upstream_hash-99ace64 –prefix=/ngboss/webproxy1
make && make install

 

  • 配置nginx
#user  webproxy1 webproxy1;
worker_processes  1;
 
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
error_log  logs/error.log  info;
 
pid        logs/nginx.pid;
 
events {
    use         epoll;
    worker_connections  51200;
}
 
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  15;
 
    #gzip  on;
 
    userid          on;
    userid_name     QHAINGBOSSID;
    userid_domain   192.168.102.249;
    userid_path     /;
    userid_expires  2d;
 
    upstream ngboss_cluster {
        hash   $cookie_QHAINGBOSSID;
        server 192.168.102.249:8081;
    }
 
    upstream saleserv_cluster {
        hash $cookie_QHAINGBOSSID;
        server 192.168.102.249:8083;
    }
 
    server {
        listen       9090;
        server_name  localhost;
 
        proxy_redirect off;
 
        location /saleserv {
            if ($request_uri ~* “.*\.(js|css|gif|jpg|jpeg|png|bmp|swf)$”) {
                expires max;
            }
            proxy_pass http://saleserv_cluster;
        }      
 
        location / {
            if ($request_uri ~* “.*\.(js|css|gif|jpg|jpeg|png|bmp|swf)$”) {
                expires max;
            }
            proxy_pass http://ngboss_cluster;
        }
    }
}

 

  • 起停控制
# 啟服務
sbin/nginx
 
# 停服務
sbin/nginx -s stop
 
# 平滑重啟
kill -HUP `cat logs/nginx.pid `

 

==============================================

 

1.Nginx代理與負載均衡配置與優化
 
 
 Nginx從0.7.48版本開始,支持了類似Squid的緩存功能。Nginx的Web緩存服務主要由proxy_cache相關指令集和fastcgi_cache相關指令集構成,前者用於反向代理時,對后端內容源服務器進行緩存,后者主要用於對FastCGI的動態程序進行緩存。兩者的功能基本上一樣。
 
 Nginx 0.8.32版本,proxy_cache和fastcgi_cache已經比較完善,加上第三方的ngx_cache_purge模塊(用於清除指定URL的緩存),已經可以完全取代Squid。
 
 在功能上,Nginx已經具備Squid所擁有的Web緩存加速功能、清除指定URL緩存的功能。而在性能上,Nginx對多核CPU的利用,勝過Squid不少。另外,在反向代理、負載均衡、健康檢查、后端服務器故障轉移、Rewrite重寫、易用性上,Nginx也比Squid強大得多。這使得一台Nginx可以同時作為“負載均衡服務器”與“Web緩存服務器”來使用。
 下面的文檔說明了nginx如何做代理服務器,將請求轉發到其他服務器,本身不做緩存。使用版本為nginx-0.8.15,配置如下:
 
 
 
 http
{
……..
     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;            // 會保存用戶的頭信息,供nginx進行規則處理
 

proxy_buffers                        4  32k;    // 告訴nginx保存單個用的幾個buffer最大用多大空間

 
proxy_busy_buffers_size        64k;       
proxy_max_temp_file_size      64k;
// proxy緩存臨時文件的大小
 
 
upstream clubsrv {
        server 192.168.0.110:80 weight=5;
        server 192.168.0.121:80 weight=5;
    }
    upstream mysrv {
        server 192.168.0.32:80 weight=2;
        server 127.0.0.1:8000 weight=8;
    }
    server {
        listen       80;
        server_name  club.xywy.com;
        charset gbk;
        root  /www;
        access_log logs/aaa.log combined;
//下面是第一個域名,使用clubsrv的代理
        location / {
            proxy_next_upstream http_502 http_504 error timeout invalid_header;
// 如果后端服務器返回502、504或執行超時等錯誤,自動將請求轉發到upstream另一台服務器
            proxy_pass  http://clubsrv;
// 與上面upstream自己命名的名字填寫一致
            proxy_redirect     off;
            proxy_set_header   Host            club.xywy.com;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
// nginx在前端做代理,后端的日志會顯示127.0.0.1,上面配置可以顯示用戶真實IP(還需裝第三方軟件,見下面的詳細說明)
            index  index.htm index.html index.php;
        }
//下面是第二個域名,使用mysrv的代理,訪問www.sum.com/message目錄下的
    server {
        listen       80;
        server_name  www.sum.com;
        location /message {
           proxy_pass  http://mysrv;
           proxy_set_header   Host            $host;
// 訪問這個域名的,只有mysrv 本機可以訪問
          }
//訪問除了/message之外的www.sum.com/ 地址,
        location / {
           proxy_pass  http://mysrv;
           proxy_set_header   Host            $host;
                     proxy_set_header   X-Real-IP       $remote_addr;
 
下面的配置,與上面錯誤返回的效果相同,這里可以不寫。
 
 error_page   500 502 503 504  /50x.html;  
 location = /50x.html
{
   root   html;
}

 
2、Nginx負載均衡指令 
Nginx屬於軟件的七層負載均衡(lvs是軟件的四層負載均衡的代表),七層負載均衡軟件還有L7SW(Layer7 switching)、HAProxy等。支持負載均衡的模塊是Http Upstream。下面介紹此模塊及他下面的幾個指令 
HTTP Upstream 模塊
  1 ip_hash 指令 
當對后端的多台動態應用服務器做負載均衡時,ip_hash指令將某個客戶端IP的請求通過哈希算法定位到同一台后端服務器上。這樣,當來自某ip用戶在Sever A上登錄后,再訪問該站點的其他URL時,能保證訪問仍在Server A上。如果不加ip_hash,加入用戶在Server A上登錄,再訪問該站點其他URL,就有可能跳轉到后端的Sever B、C…..,而session記錄在A上,B、C上沒有,就會提示用戶未登錄。
注意:但這種訪問不能保證后端服務器的負載均衡,可能后端有些server接受到的請求多,有些server接受的少,設置的權重值不起作用。
建議如果后端的動態應用程序服務器能做到session共享,而不用nginx上配置ip_hash的方式。 
 
upstream mysrv {
        ip_hash;
        server 192.168.0.110:80 weight=2;
        server 127.0.0.1:8000 down;
        server 192.168.0.212:80 weight=8;
    }
2 server 指令
該指令用語指定后端服務器的名稱和參數。服務器的名稱可以是一個域名,一個ip,端口號或UNIX Socket。
參數介紹:
weight=number : 設置服務器權重,權重值越高,被分配到客戶端請求數越多。默認為1;
max_fails=numbser : 在fail_timeout指定的時間內對后端服務器請求失敗的次數,如果檢測到后端服務器無法連接及發生錯誤(404除外),則標記為失敗。如果沒有設置,默認為1。設置為0則關閉這項檢查。
fail_timeout=time : 在經歷參數max_fails設置的失敗次數后,暫停的時間。
down : 表示服務器為永久離線狀態。
Backup : 僅僅在非backup服務器全部down或繁忙的時候才啟用。
配置如下:

 

 
upstream mysrv {
        ip_hash;
        server  www.xywy.com  weight=2;
        server  127.0.0.1:8000   down;
        server  192.168.0.212:80  max_fails=3  fail_timeout=30s;
        server  unix:/tmp/bakend3;
    }

 =================================================

 

打算使用nginx做負載均衡,把請求代理轉到一個apache上,apache后面是WebLogic上的應用。apache上有別的插件,必須要過apache。
    down到nginx-0.6.35,安裝后,配置很簡單。配置后問題如下:走nginx直接到WebLogic上的應用沒有問題;但是nginx到apache再到WebLogic上的應用就有問題。
    日志中是302(nginx中”HTTP/1.1 302 2783″,apache上是”HTTP/1.0 302 2771″)。對比后,開始以為是HTTP/1.1和HTTP/1.0的問題。后來想到 Nginx 和瀏覽器使用 HTTP/1.1 進行對話,而在后台服務中使用 HTTP/1.0,是對的,然后注意到 2個web服務上得到的 返回給客戶端的不包括響應頭的字節數 是不一樣長的,這個缺的 12 字節缺在哪里還需要追究一下。
    同時想起來訪問測試的時候訪問 /web 和訪問 /web/ 是不一樣的,apache上是可以處理掉的,注意到apache處理類似的資源請求時 日志中會先打 302,然后后面自動加 / ,之后日志中就200了。nginx沒有處理,不知道是沒有設置的緣故還是他本身就不能處理類似的問題。不過后者幾率應該不大,還得研究一下nginx配置和apache的相關處理機制。
    再有想到用重寫機制來加上對”/”的處理,但是考慮到nginx代理到apache上也會報302,估計這樣也解決不了問題,還得研究一下proxy的配置。
    OK,看看以上3條路 那條 是 正解。

此條目是由callen發表在平台運維分類目錄的。將固定鏈接加入收藏夾。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM