Windows+nginx+tomcat Build load balancing and cluster environment simultaneously realize session sharing (I.)

Source: Internet
Author: User
Tags goto session id server port
absrtact : With the visit of the website more and more, so consider to add server to the website, now more popular practice is to do the cluster environment for the site, the following I do the process of recording, to facilitate future viewing, but also hope to help to have the need of friends.

One: First, the environment:

1.JDK 1.6.0_45

2.tomcat 6.0.44

3.nginx 1.8.0


Second: The installation of Jdk,tomcat,nginx:

1.JDK installation Please refer to: Click to open the link

Installation steps for 2.nginx

(1). First download the Nginx version of Windows, I download the Nginx 1.8.0 version, download the address: Click to open the link

(2). After the download is complete, extract directly to a path without Chinese, I am here to extract to: D:\server\nginx-1.8.0

(3). Through the DOS command into the nginx-1.8.0 directory to start the Nginx can be

(4) Operation Nginx Command under Windows

1. Start Nginx.exe start Nginx 2 nginx-s stop nginx-s quit

Stop stops Nginx immediately and does not save related information

Quit indicates a normal exit Nginx and holds information 3. Reboot nginx-s reload reboot (reboot required because of configuration change)

3.tomcat installation (directly to Tomcat's official website to download the uncompressed version of the Can)


Three: To download the download of Tomcat copied 3, altogether is a, of which three do Qdksdemo cluster environment, and the other three do Qdkydemo cluster environment, the specific screenshot as follows:


After the replication is complete, modify each tomcat's port and session share configuration separately, as follows:

1. The first port number to be modified

<server port= "8005" shutdown= "Shutdown" >
2. The second port number to be modified

<connector port= "8081" protocol= "http/1.1" connectiontimeout= "20000" 
               redirectport= "8443"/>
3. The third port number to be modified

<connector port= "8009" protocol= "ajp/1.3" redirectport= "8443"/>
4. Fourth to increase the session sharing configuration, this can see the official file of Tomcat, which has a configuration directly copied to the Server.xml can be, address: http://tomcat.apache.org/tomcat-6.0-doc/ Cluster-howto.html

<cluster classname= "Org.apache.catalina.ha.tcp.SimpleTcpCluster" channelsendoptions= "6" >
                   <manager classname= "Org.apache.catalina.ha.session.BackupManager" expiresessionsonshutdown= "false"
          Notifylistenersonreplication= "true" mapsendoptions= "6"/> <!-- <manager classname= "Org.apache.catalina.ha.session.DeltaManager" expiresessionsonshutdown= false "notifylistenersonreplication=" true "/>--> <channel classname=" Org.apache . Catalina.tribes.group.GroupChannel "> <membership classname=" org.apache.catalina.tribes.membership.Mcast Service "address=" 228.0.0.4 "port=" 45564 "Frequenc y= "droptime=" 3000 "/> <receiver classname=" ORG.APACHE.CATALINA.TRIBES.TRANSP
   Ort.nio.NioReceiver "                   address= "Auto" port= "5000" selectortimeout= "100" maxthreads= "6"/> <sender classname= "Org.apache.catalina.tribes.transport.ReplicationTransmi
            Tter "> <transport classname=" Org.apache.catalina.tribes.transport.nio.PooledParallelSender "/> </Sender> <interceptor classname= "Org.apache.catalina.tribes.group.interceptors.TcpFailureDete ctor "/> <interceptor classname=" org.apache.catalina.tribes.group.interceptors.MessageDispatch15Intercept
          or "/> <interceptor classname=" Org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor "/> </Channel> <valve classname= "Org.apache.catalina.ha.tcp.ReplicationValve" f Ilter= ". *\.gif|. *\.js|. *\.jpeg|. *\.jpg|. *\.png|. *\.htm|. *\.html|. *\.css|. *\.txt "/> <deployer classname=" org.apache.catalina.ha.dePloy.
                    Farmwardeployer "tempdir="/tmp/war-temp/"deploydir="/tmp/war-deploy/" Watchdir= "/tmp/war-listen/" watchenabled= "false"/> <clusterlistener Classnam E= "Org.apache.catalina.ha.session.ClusterSessionListener"/> </Cluster>

Such a tomcat configuration file is modified, and the other 5 tomcat configurations are the same as this one, except for the same port.


Four: Nginx configuration:

1. First, create a new folder in the Nginx directory to store the cluster profiles between the different projects, as shown in the following figure:


2. Create a new configuration file in the Manyvhost folder for the cluster configuration of two projects, as shown in the following figure:


The configuration of 3.qdks.conf and qdky.conf is as follows:

(1). qdks.conf

Upstream Qdks {server localhost:8081 weight=1;
	Server localhost:8082 weight=1;
Server localhost:8083 weight=1;
	} server {Listen 8086;

	server_name 192.168.1.103;
	Error_page 502 503 504/50x.html;
	Location =/50x.html {root html;

	} Root/data/projects/ycp/bill; #-Rewrite:if (Path ~ "^/assets/(. *)") goto "/public/assets/$1" # location ~ ^/static/assets/(. *) $ # {#alias
	/data/projects/payment/web/public/assets/$1;
	# Access_log off;
	# #expires 3d;
			#} location/{index index.html index.htm index.jsp;
	 } location ~. * {# Proxy_pass_header Server;
	Proxy_set_header Host $http _host;
	 # proxy_redirect off;
	 Proxy_set_header X-real-ip $remote _addr;
	 Proxy_set_header X-scheme $scheme;
	 Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
	 Add_header Pragma "No-cache";
	Proxy_pass Http://qdks;

	} rewrite ^/admin/?$/admin/login redirect; # for rewrite rewrite ^/(channel|admin|mobile|api|web)/(. *) $/public/index.php/$2 last;
	#redirect to Mobile WAP #rewrite ^$/m redirect;

#rewrite ^/$/mobile/user redirect; }

(2). qdky.conf

Upstream Qdky {server localhost:7081 weight=1;
   Server localhost:7082 weight=1;
Server localhost:7083 weight=1;
	} server {Listen 7086;
	server_name 192.168.1.103;
	Error_page 502 503 504/50x.html;
	Location =/50x.html {root html;

	} Root/data/projects/ycp/bill; #-Rewrite:if (Path ~ "^/assets/(. *)") goto "/public/assets/$1" # location ~ ^/static/assets/(. *) $ # {#alias
	/data/projects/payment/web/public/assets/$1;
	# Access_log off;
	# #expires 3d;
			#} location/{index index.html index.htm index.jsp;
	 } location ~. * {# Proxy_pass_header Server;
	Proxy_set_header Host $http _host;
	 # proxy_redirect off;
	 Proxy_set_header X-real-ip $remote _addr;
	 Proxy_set_header X-scheme $scheme;
	 Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
	 Add_header Pragma "No-cache";
	Proxy_pass Http://qdky;
	
	} rewrite ^/admin/?$/admin/login redirect; # for rewrite rewrite ^/(channel|admin|mobile|api|web)/(. *) $/public/index.php/$2 last;
	#redirect to Mobile WAP #rewrite ^$/m redirect;



#rewrite ^/$/mobile/user redirect; }

These two profiles are the configuration files in my current environment, where the port number of the Qdks project is 8086, and the following three Tomcat ports are: 8081,8082,8083;qdky 7086, and the following three tomcat port number is: 7081, 7082,7083


4. Finally, the cluster configuration of the above two projects is introduced into the nginx.conf core configuration file in the Nginx conf directory, as follows:

#user nobody;

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 {Worker_connections 5000;}
    HTTP {include mime.types;
	
    Default_type Application/octet-stream;

    #隐藏nginx的版本号 Server_tokens off; #log_format Main ' $remote _addr-$remote _user [$time _local] "$request" ' # ' $status $body _bytes_sen

    T "$http _referer" "$http _user_agent" "$http _x_forwarded_for";
    #access_log Logs/access.log Main;

   #access日志存放路径和格式 Access_log Logs/access.log;
   #提升文件传输性能 sendfile on;

   #tcp_nopush on;
   #keepalive_timeout 0;
   
   #keepalive_timeout 65;
   #设置客户端能够请求的单个文件大小 Client_max_body_size 300m;

    Keepalive_timeout 75;
    #proxy参数 #跟后端服务器连接的超时时间, initiate handshake wait for response timeout Proxy_connect_timeout 5;
    #连接成功后, wait for back-end server response time Proxy_read_timeout 600; #后端服务器数据回传时间 Proxy_send_timeout 600;
    #代理请求缓存区 Proxy_buffer_size 16k;
    #同上, tell Nginx to save a single use of several buffer, maximum space proxy_buffers 4 64k;
    #如果系统很忙时可以申请更大的proxy_buffers, officially recommended *2 proxy_busy_buffers_size 128k;

    Proxy_temp_file_write_size 128k;
    #gzip on;
    #开启压缩功能 gzip on;
    Gzip_min_length 1k;
    Gzip_buffers 4 16k; 
    Gzip_http_version 1.1;
	
    Gzip_comp_level 2;
    #压缩级别从低到高1-9 gzip_types text/plain application/x-javascript text/css application/xml;

    Gzip_vary on;
    #qdksDemo和qdkyDemo项目的虚拟目录 (represented by absolute path) include d:/server/nginx-1.8.0/manyvhost/qdks.conf;

    Include d:/server/nginx-1.8.0/manyvhost/qdky.conf; 
    # Another virtual host using mix of ip-, name-, and port-based configuration # #server {# listen 80;
    # Listen somename:8080;

    # server_name somename alias Another.alias;
    # location/{# root HTML;
    # index index.html index.htm;   #} # # HTTPS server # #server {# Listen 443;

    # server_name localhost;
    # SSL on;
    # ssl_certificate Cert.pem;

    # Ssl_certificate_key Cert.key;

    # ssl_session_timeout 5m;
    # ssl_protocols SSLv2 SSLv3 TLSv1; # ssl_ciphers high:!anull:!
    MD5;

    # ssl_prefer_server_ciphers on;
    # location/{# root HTML;
    # index index.html index.htm;
 #    }
    #}

}

V: Deploy the corresponding project separately to test whether the cluster environment we configured above is normal

1. Download the corresponding project source code, download and extract to your local workspace below, my path is: D:\workspace_qdexam\qdksDemo and D:\workspace_qdexam\qdkyDemo

Qdksdemo's download Address: http://download.csdn.net/detail/sxdtzhaoxinguo/9187453

Qdkydemo's download Address: http://download.csdn.net/detail/sxdtzhaoxinguo/9187455


2. Deploy to the corresponding tomcat below, here I am using the direct mapping path method

(1). The Qdksdemo deployment method is to create a new configuration file Qdksdemo.xml under the corresponding three Tomcat Conf\catalina\localhost directory, which reads as follows:

<context    path= "/qdksdemo"   docbase= "D:\workspace_qdexam\qdksDemo\WebContent"     debug= "0"    Privileged= "true" >      </Context>

(2). The Qdkydemo deployment method is to create a new profile qdkydemo.xml under the corresponding three Tomat Conf\catalina\localhost directory, which reads as follows:

<context path= "/qdkydemo"   docbase= "D:\workspace_qdexam\qdkyDemo\WebContent" debug= "0" privileged= "true" ></Context>


3. Start each of these 6 tomcat, as shown below:


4. Start Nginx, as shown below:


5. Finally, through the browser to access the two projects, the following figure:



Click Refresh Separately, you will find that the actual access to the above port will change, but the following session ID is unchanged, which means that based on Nginx and Tocmat cluster built successfully.


Finally I put my tomcat and nginx all packed and uploaded for your reference.

Tomcat configuration Compression Package download address: http://download.csdn.net/detail/sxdtzhaoxinguo/9187521

Nginx Configuration Compression Package download address: http://download.csdn.net/detail/sxdtzhaoxinguo/9187527




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.