Nginx+tomcat Distributed Deployment

Source: Internet
Author: User
Tags documentation port number server port tomcat

Configuration files that need to be changed:

1. Hosts file

2, the server.xml of each tomcat

3, nginx configuration file nginx.conf


Configure two tomcat as a single machine (configure multiple machines as long as the corresponding IP address is changed to the IP address of the cluster machine)


The first step is to configure the Hosts file

Win7 under: C:\Windows\System32\drivers\etc\hosts

Append at the very end: Host IP domain name

The second step, the Tomcat configuration file, if on the same machine to pay attention to modify the port number, if the deployment on a different machine on the same port number is not related

<?xml version= ' 1.0 ' encoding= ' utf-8 '?>
<!--
Licensed to the Apache software Foundation (ASF) under one or more
Contributor license agreements. See the NOTICE file distributed with
This is for additional information regarding copyright ownership.
The ASF licenses this file to you under the Apache License, Version 2.0
(the "License"); Except in compliance with
The License. Obtain a copy of the License at


http://www.apache.org/licenses/LICENSE-2.0


Unless required by applicable law or agreed to writing, software
Distributed under the License is distributed on a "as is" BASIS,
Without warranties or CONDITIONS of any KIND, either express or implied.
See the License for the specific language governing permissions and
Limitations under the License.
-
<!--note:a "Server" is not itself A "Container"
Define subcomponents such as "valves" at the This level.
Documentation at/docs/config/server.html
-
<server port= "8006" shutdown= "Shutdown" >
<listener classname= "Org.apache.catalina.startup.VersionLoggerListener"/>
<!--Security Listener. Documentation at/docs/config/listeners.html
<listener classname= "Org.apache.catalina.security.SecurityListener"/>
-
<!--APR Library loader. Documentation At/docs/apr.html--
<listener classname= "Org.apache.catalina.core.AprLifecycleListener" sslengine= "on"/>
<!--Prevent memory leaks due to use of particular Java/javax apis-->
<listener classname= "Org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
<listener classname= "Org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
<listener classname= "Org.apache.catalina.core.ThreadLocalLeakPreventionListener"/>


<!--Global JNDI Resources
Documentation at/docs/jndi-resources-howto.html
-
<GlobalNamingResources>
<!--Editable User database that can also is used by
Userdatabaserealm to authenticate users
-
<resource name= "Userdatabase" auth= "Container"
Type= "Org.apache.catalina.UserDatabase"
description= "User database that can be updated and saved"
factory= "Org.apache.catalina.users.MemoryUserDatabaseFactory"
Pathname= "Conf/tomcat-users.xml"/>
</GlobalNamingResources>


<!--a "Service" is a collection of one or more "connectors" that share
A single "Container" Note:a "Service" was not itself A "Container",
Define subcomponents such as "valves" at the This level.
Documentation at/docs/config/service.html
-
<service name= "Catalina" >


<!--the connectors can use a GKFX executor, you can define one or more named thread pools-->
<!--
<executor name= "Tomcatthreadpool" nameprefix= "catalina-exec-"
Maxthreads= "minsparethreads=" 4 "/>
-
<executor name= "Tomcatthreadpool" nameprefix= "catalina-exec-"
maxthreads= "minsparethreads=" prestartminsparethreads= "true"/>


<!--A "Connector" represents an endpoint by which requests is received
and responses are returned. Documentation at:
Java HTTP Connector:/docs/config/http.html (Blocking & non-blocking)
Java AJP Connector:/docs/config/ajp.html
APR (HTTP/AJP) Connector:/docs/apr.html
Define a non-ssl/tls http/1.1 Connector on port 8080
-
<connector port= "8082" executor= "Tomcatthreadpool" protocol= "Org.apache.coyote.http11.Http11NioProtocol"
connectiontimeout= "20000" enablelookups= "false" disableuploadtimeout= "true"
Redirectport= "8444" usebodyencodingforuri= "true" uriencoding= "UTF-8"
Address= "This tomcat machine IP"/>
<!--A "Connector" using the shared thread pool-->
<!--
<connector executor= "Tomcatthreadpool"
port= "8080" protocol= "http/1.1"
connectiontimeout= "20000"
redirectport= "8443"/>
-
<!--Define a SSL/TLS http/1.1 Connector on port 8443
This connector uses the NIO implementation that requires the JSSE
Style configuration. When using the apr/native implementation, the
OpenSSL style configuration is required as described in the apr/native
Documentation--
<!--
<connector port= "8443" protocol= "Org.apache.coyote.http11.Http11NioProtocol"
Maxthreads= "sslenabled=" true "scheme=" "https" secure= "true"
Clientauth= "false" sslprotocol= "TLS"/>
-


<!--Define an AJP 1.3 Connector on port 8009-

<connector port= "8010" protocol= "ajp/1.3" redirectport= "8444"/>




<!--an Engine represents the entry point (within Catalina) that processes
Every request. The Engine implementation for Tomcat stand alone
Analyzes the HTTP headers included with the request, and passes them
The appropriate host (virtual host).
Documentation At/docs/config/engine.html--


<!--should set Jvmroute to support load-balancing via AJP ie:
<engine name= "Catalina" defaulthost= "localhost" jvmroute= "jvm1" >
-
<engine name= "Catalina" defaulthost= "domain Name" >


<!--for clustering, please take a look at documentation at:
/docs/cluster-howto.html (Simple-to)
/docs/config/cluster.html (reference documentation)--
<!--
<cluster classname= "Org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
-


<!--use of the Lockoutrealm to prevent attempts to guess user passwords
Via a brute-force attack--
<realm classname= "Org.apache.catalina.realm.LockOutRealm" >
<!--This Realm uses the userdatabase configured in the global JNDI
Resources under the key "Userdatabase". Any edits
That is performed against this userdatabase is immediately
Available for use by the Realm. -
<realm classname= "Org.apache.catalina.realm.UserDatabaseRealm"
Resourcename= "Userdatabase"/>
</Realm>


Unpackwars= "true" autodeploy= "true" >


<!--Singlesignon valve, share authentication between Web applications
Documentation at:/docs/config/valve.html--
<!--
<valve classname= "Org.apache.catalina.authenticator.SingleSignOn"/>
-


<!--Access log processes all example.
Documentation at:/docs/config/valve.html
Note:the pattern used is equivalent to using pattern= "common"-
<valve classname= "Org.apache.catalina.valves.AccessLogValve" directory= "Logs"
prefix= "Localhost_access_log" suffix= ". txt"
pattern= "%h%l%u%t &quot;%r&quot; %s%b "/>


</Host>
</Engine>
</Service>
</Server>

Other Tomcat configurations above (same machine note port number)

Step three, configure Nginx



#user Zhanghao;
Worker_processes 2;


#error_log Logs/error.log;
#error_log Logs/error.log Notice;
#error_log Logs/error.log Info;


#pid Logs/nginx.pid;



Events {
Worker_connections 65535;
}




HTTP {
Client_max_body_size 100m;
Include Mime.types;
Default_type Application/octet-stream;


Sendfile on;
Tcp_nopush on;


Keepalive_timeout 60;


# gzip on;

Include vhosts_tomcat/*.conf;


Upstream myserver{
IP of server tomcat1: Port weight=2;
IP of server tomcat2: Port weight=3;
}
server{
Listen 80;
server_name domain name;


Location/{
root HTML;
Index index.html index.htm index.jsp;
Proxy_pass http://MyServer;
#proxy_read_timeout 1800;
#proxy_send_timeout 1800;
#该指令用来设置分配到后端服务器的连接超时时间
#proxy_connect_timeout 300;


# #cache # #
Proxy_connect_timeout 50;
Proxy_read_timeout 120;
Proxy_send_timeout 120;
Proxy_buffer_size 16k;
Proxy_buffers 4 64k;
Gzip_proxied any;
Proxy_busy_buffers_size 128k;
Proxy_temp_file_write_size 128k;


# support WebSocket below is the key
Proxy_http_version 1.1;
Proxy_set_header Upgrade $http _upgrade;
Proxy_set_header Connection "Upgrade";
# This is the problem of configuring Webpysessoin loss
Fastcgi_param Script_name "";
}
}
}

Fourth step, start each tomcat in turn, start Nginx


Enjoy youself.

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.