Configure cluster load balancing with Nginx+Tomcat under Windows

 Nginx is a lightweight web server/reverse proxy server, which defines itself Baidu in more detail. At present, domestic companies such as Sina and NetEase are using it. Let's talk about my server software environment first:

系统:windows_server_2008_standard_enterprise_and_datacenter_with_sp2_x64

Currently running Tomcat: non-installed version Tomcat 6.0.36

Let’s just talk about these two key points. The current problem is that a single tomcat is unable to load due to the sharp increase in traffic, and it often times out. Therefore, it is planned to use nginx to arrange load balancing. Most of the information found on the Internet is about the layout and use of the linux version of nginx, but in windows, if the linux version of nginx is used, it can only be used for testing, and cannot be used in the actual production environment. , the following error will be reported:

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

This is because the number of file access handles is limited to 1024, and it will fail to respond when there is a large amount of access. I have checked a lot of information on the Internet, saying that modifying the parameter worker_connections can solve this limitation, and there are many other saying that modifying the parameter worker_rlimit_nofile, etc., have tried but all ended in failure. Just when I was about to switch to other tools, I saw a reply on a foreign forum. I can't remember the address. It said that there is a special Windows version of nginx, which has modified the file handle data limit. After downloading, the configuration is successful and it runs ok. As long as you download the correct version, the configuration is still so easy. The following is the download configuration process~

nginx for windows official website: http://nginx-win.ecsds.eu/

nginx for windows download address:  http://nginx-win.ecsds.eu/download/

I downloaded the version of nginx 1.7.7.1 WhiteRabbit.zip, which is too new for me to be unstable. After downloading, unzip the installation package, there is a brief update information and installation guide Readme nginx-win version.txt. The key information is as follows:

*** 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 

Find the nginx-win.conf in the conf folder, copy it and rename it nginx.conf, and then configure it in this file. My configuration file is as follows:

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

}


 

The configuration of Tomcat is relatively simple, just copy one more port and change it in three places. Specifically Baidu.

After the configuration is complete, run nginx.exe and tomcat to verify the configuration.

For nginx startup control, I used a script from a buddy on the Internet. The address is as follows:

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

 

 


 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325457056&siteId=291194637