Windows下用Nginx+Tomcat配置集羣負載均衡

 Nginx是一款輕量級的web服務器/反向代理服務器,更詳細的釋義本身百度了。目前國內像新浪、網易等都在使用它。先說下個人服務器軟件環境:php

系統:windows_server_2008_standard_enterprise_and_datacenter_with_sp2_x64html

當前運行的Tomcat:非安裝版本Tomcat 6.0.36linux

就說這兩個關鍵的吧,目前遇到的問題是訪問量劇增單個tomcat已無力負載了,常常出現超時的狀況。因而就計劃用nginx佈置負載均衡,網絡上查到的資料可能是介紹linux版本的nginx的佈置及使用,但在windows中若是使用linux版本的nginx只能作個測試用,實際生產環境是沒法使用的,會報以下的錯誤:nginx

maximum number of descriptors supported by select() is 1024 while waiting for request

這是由於文件訪問句柄數被限制爲1024了,當訪問量大時就會沒法響應。去網上有查過不少資料說是修改參數worker_connections能夠解決此限制,還有其它不少說修改worker_rlimit_nofile 參數等,都嘗試了但都以失敗了結。就在準備換其它工具時在國外的一個論壇看到了一條回覆,地址不記得了,說的是有專門的windows版本的nginx,已修改了文件句柄數據的限制。後來下載後果然配置成功運行ok了。只要下載到正確的版本配置仍是so easy的。如下爲下載配置過程哈~~web

nginx for windows官網:http://nginx-win.ecsds.eu/windows

nginx for windows下載載地址: http://nginx-win.ecsds.eu/download/tomcat

我下載的是nginx 1.7.7.1 WhiteRabbit.zip這個版本,太新的版本我怕不穩定。下載完畢後解壓安裝包,裏面有個簡要的更新信息和安裝指南Readme nginx-win version.txt。關鍵信息以下:服務器

*** Default installation instructions;  
* New: unzip this version with folder structure  
* Old: overwrite with this version  
* Check nginx.conf, nginx-org.conf and nginx-win.conf  
* Windows optimization registry file: check your current values BEFORE setting the new ones

找到conf文件夾中的nginx-win.conf,把它複製一份改名爲nginx.conf,而後在此文件中作配置,個人配置文件以下:網絡

#user  nobody;
# multiple workers works !
# 工做進程數:這個數值要根據服務器CPU核心數來配置,如6核12線程的cpu能夠配置爲6或12。
worker_processes  6;

#錯誤日誌存放路徑
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
#設置單個進程同時打開的最大鏈接數,這個值設置大些能接受較多的鏈接,固然這須要cpu和內存支持哦~~
    worker_connections  32768;
    # max value 32768, nginx recycling connections+registry optimization = 
    #   this.value * 20 = max concurrent connections currently tested with one worker
    #   C1000K should be possible depending there is enough ram/cpu power
    # multi_accept on;
}


http {
    #include      /nginx/conf/naxsi_core.rules;
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr:$remote_port - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

#     # loadbalancing PHP
#     upstream myLoadBalancer {
#         server 127.0.0.1:9001 weight=1 fail_timeout=5;
#         server 127.0.0.1:9002 weight=1 fail_timeout=5;
#         server 127.0.0.1:9003 weight=1 fail_timeout=5;
#         server 127.0.0.1:9004 weight=1 fail_timeout=5;
#         server 127.0.0.1:9005 weight=1 fail_timeout=5;
#         server 127.0.0.1:9006 weight=1 fail_timeout=5;
#         server 127.0.0.1:9007 weight=1 fail_timeout=5;
#         server 127.0.0.1:9008 weight=1 fail_timeout=5;
#         server 127.0.0.1:9009 weight=1 fail_timeout=5;
#         server 127.0.0.1:9010 weight=1 fail_timeout=5;
#         least_conn;
#     }
# 在此處設置tomcat服務器信息,一樣tomcat也能夠不在同一主機中。這裏設置了兩個tomcat服務,比重是1:1了。 localhost更換爲服務器的IP
         upstream localhost{
	   #ip_hash;
           server  127.0.0.1:9010 weight=1;
           server  127.0.0.1:9020 weight=1;
         }

    sendfile        off;
    #tcp_nopush     on;

    server_names_hash_bucket_size 128;

## Start: Timeouts ##
    client_body_timeout   10;
    client_header_timeout 10;
    keepalive_timeout     80;
    send_timeout          10;
    keepalive_requests    10;
## End: Timeouts ##

    #gzip  on;

    server {
#這個很關鍵~~它是nginx監聽的端口哦~~
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

# For Naxsi remove the single # line for learn mode, or the ## lines for full WAF mode

        location / {
            #include    /nginx/conf/mysite.rules; # see also http block naxsi include line
            ##SecRulesEnabled;
        	  ##DeniedUrl "/RequestDenied";
	          ##CheckRule "$SQL >= 8" BLOCK;
	          ##CheckRule "$RFI >= 8" BLOCK;
	          ##CheckRule "$TRAVERSAL >= 4" BLOCK;
	          ##CheckRule "$XSS >= 8" BLOCK;
	          
	    proxy_pass         http://localhost;
            root   html;
            index  index.html index.htm;
        }

# For Naxsi remove the ## lines for full WAF mode, redirect location block used by naxsi
        ##location /RequestDenied {
        ##    return 412;
        ##}

## Lua examples !
#         location /robots.txt {
#           rewrite_by_lua '
#             if ngx.var.http_host ~= "localhost" then
#               return ngx.exec("/robots_disallow.txt");
#             end
#           ';
#         }

        #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;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000; # single backend process
        #    fastcgi_pass   myLoadBalancer; # or multiple, see example above
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl spdy;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_prefer_server_ciphers On;
    #    ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
    #    ssl_ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!eNULL:!MD5:!DSS;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}


 

Tomcat的配置就比較簡單了就是再複製一個改三處端口就能夠了。具體本身百度了。session

配置完畢運行nginx.exe和tomcat就能夠驗證配置狀況了。

nginx啓動控制我採用了網上一哥們的腳本,地址以下:

http://feitianbenyue.iteye.com/blog/1989868

相關文章
相關標籤/搜索