Nginx+Tomcat简单集群

Posted FangZheng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx+Tomcat简单集群相关的知识,希望对你有一定的参考价值。

1.软件准备

下载nginx和Tomcat

Nginx:http://nginx.org/en/download.html 这里需要下载稳定版:Stable version
Tomcat:下载就不说了,这里使用apache-tomcat-6.0.14版本

解压到一个目录

技术分享

2.修改Tomcat的端口

Tomcat1:修改Server.xml

D:\nginx_cluster\apache-tomcat-6.0.14_1\conf\server.xml
共修改3处内容:将以下端口都加1

  1. <!--第1处-->
  2. <Serverport="18005"shutdown="SHUTDOWN">
  3. <!--第2处-->
  4. <Connectorport="18080"protocol="HTTP/1.1"
  5. connectionTimeout="20000"
  6. redirectPort="8443"/>
  7. <!--第3处-->
  8. <Connectorport="18009"protocol="AJP/1.3"redirectPort="8443"/>

Tomcat2:修改Server.xml

D:\nginx_cluster\apache-tomcat-6.0.14_2\conf\server.xml
共修改3处内容:将以下端口都加2

  1. <!--第1处-->
  2. <Serverport="28005"shutdown="SHUTDOWN">
  3. <!--第2处-->
  4. <Connectorport="28080"protocol="HTTP/1.1"
  5. connectionTimeout="20000"
  6. redirectPort="8443"/>
  7. <!--第3处-->
  8. <Connectorport="28009"protocol="AJP/1.3"redirectPort="8443"/>

3.测试Tomcat是否正常运行

分别访问两个Tomcat

http://localhost:18080/
http://localhost:28080/
都出现猫的页面说明正常,为了区分不同的Tomcat,这里修改${Tmocat_home}\webapps\ROOT\ index.html文件内容,加入内容以便区分

  1. <h1>This Tomcat1</h1>

之后再次访问两个Tomcat
技术分享
技术分享
至此,两个Tomcat运行正常。

4.配置Nginx

修改Nginx的主配置文件:
D:\nginx_cluster\nginx-1.10.2\conf\ nginx.conf

  1. #user nobody;
  2. worker_processes 1;
  3. #error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;
  6. #pid logs/nginx.pid;
  7. events {
  8. worker_connections 1024;
  9. }
  10. http {
  11. include mime.types;
  12. default_type application/octet-stream;
  13. #log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘
  14. # ‘$status $body_bytes_sent "$http_referer" ‘
  15. # ‘"$http_user_agent" "$http_x_forwarded_for"‘;
  16. #access_log logs/access.log main;
  17. sendfile on;
  18. #tcp_nopush on;
  19. #keepalive_timeout 0;
  20. keepalive_timeout 65;
  21. #gzip on;
  22. #监听localhost的80端口
  23. server {
  24. listen 80;
  25. server_name localhost;
  26. location /{
  27. proxy_connect_timeout 3;
  28. proxy_send_timeout 30;
  29. proxy_read_timeout 30;
  30. proxy_pass http://localhost;
  31. }
  32. }
  33. # another virtual host using mix of IP-, name-, and port-based configuration
  34. #
  35. #server {
  36. # listen 8000;
  37. # listen somename:8080;
  38. # server_name somename alias another.alias;
  39. # location / {
  40. # root html;
  41. # index index.html index.htm;
  42. # }
  43. #}
  44. #集群配置:服务器列表
  45. upstream localhost {
  46. server localhost:18080 weight=2;#服务器配置 weight是权重的意思,权重越大,分配的概率越大。
  47. server localhost:28080 weight=1;
  48. }
  49. # HTTPS server
  50. #
  51. #server {
  52. # listen 443 ssl;
  53. # server_name localhost;
  54. # ssl_certificate cert.pem;
  55. # ssl_certificate_key cert.key;
  56. # ssl_session_cache shared:SSL:1m;
  57. # ssl_session_timeout 5m;
  58. # ssl_ciphers HIGH:!aNULL:!MD5;
  59. # ssl_prefer_server_ciphers on;
  60. # location / {
  61. # root html;
  62. # index index.html index.htm;
  63. # }
  64. #}
  65. }

主要配置

技术分享
技术分享
至此,Nginx的简单配置就完成了。下面开始测试

5.测试集群访问

启动Nginx

进入到Nginx目录
启动命令为:start nginx
停止命令为:nginx –s stop
技术分享

访问测试

访问:http://localhost/
Nginx内部配置了监听80端口,默认进行服务器的分发。
技术分享
技术分享
随便刷新测试了10次,共访问了Tomcat1共8次,Tomcat2共2次。可以看到权重越大,访问到的概率越大。

附录:

Nginx.conf配置

  1. #Nginx所用用户和组
  2. #user niumd niumd;
  3. #工作的子进程数量(通常等于CPU数量或者2倍于CPU)
  4. worker_processes 2;
  5. #错误日志存放路径
  6. #error_log logs/error.log;
  7. #error_log logs/error.log notice;
  8. error_log logs/error.log info;
  9. #指定pid存放文件
  10. pid logs/nginx.pid;
  11. events {
  12. #使用网络IO模型linux建议epoll,FreeBSD建议采用kqueue
  13. #use epoll;
  14. #允许最大连接数
  15. worker_connections 2048;
  16. }
  17. http {
  18. include mime.types;
  19. default_type application/octet-stream;
  20. #定义日志格式
  21. #log_format main ‘$remote_addr - $remote_user [$time_local] $request ‘
  22. # ‘"$status" $body_bytes_sent "$http_referer" ‘
  23. # ‘"$http_user_agent" "$http_x_forwarded_for"‘;
  24. #access_log off;
  25. access_log logs/access.log;
  26. client_header_timeout 3m;
  27. client_body_timeout 3m;
  28. send_timeout 3m;
  29. client_header_buffer_size 1k;
  30. large_client_header_buffers 44k;
  31. sendfile on;
  32. tcp_nopush on;
  33. tcp_nodelay on;
  34. #keepalive_timeout 75 20;
  35. include gzip.conf;
  36. #集群配置:服务器列表
  37. upstream localhost {
  38. server localhost:18080 weight=2;#服务器配置 weight是权重的意思,权重越大,分配的概率越大。
  39. server localhost:28080 weight=1;
  40. }
  41. server {
  42. listen 80;
  43. server_name localhost;
  44. location /{
  45. proxy_connect_timeout 3;
  46. proxy_send_timeout 30;
  47. proxy_read_timeout 30;
  48. proxy_pass http://localhost;
  49. }
  50. }
  51. }

6.配置文件

Tomcat1 的Server.xml配置文件

  1. <!-- Note: A "Server" is not itself a "Container", so you may not
  2. define subcomponents such as "Valves" at this level.
  3. Documentation at /docs/config/server.html
  4. -->
  5. <Serverport="18005"shutdown="SHUTDOWN">
  6. <!--APR library loader. Documentation at /docs/apr.html -->
  7. <ListenerclassName="org.apache.catalina.core.AprLifecycleListener"SSLEngine="on"/>
  8. <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
  9. <ListenerclassName="org.apache.catalina.core.JasperListener"/>
  10. <!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html -->
  11. <ListenerclassName="org.apache.catalina.mbeans.ServerLifecycleListener"/>
  12. <ListenerclassName="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
  13. <!-- Global JNDI resources
  14. Documentation at /docs/jndi-resources-howto.html
  15. -->
  16. <GlobalNamingResources>
  17. <!-- Editable user database that can also be used by
  18. UserDatabaseRealm to authenticate users
  19. -->
  20. <Resourcename="UserDatabase"auth="Container"
  21. type="org.apache.catalina.UserDatabase"
  22. description="User database that can be updated and saved"
  23. factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
  24. pathname="conf/tomcat-users.xml"/>
  25. </GlobalNamingResources>
  26. <!-- A "Service" is a collection of one or more "Connectors" that share
  27. a single "Container" Note: A "Service" is not itself a "Container",
  28. so you may not define subcomponents such as "Valves" at this level.
  29. Documentation at /docs/config/service.html
  30. -->
  31. <Servicename="Catalina">
  32. <!--The connectors can use a shared executor, you can define one or more named thread pools-->
  33. <!--
  34. <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
  35. maxThreads="150" minSpareThreads="4"/>
  36. -->
  37. <!-- A "Connector" represents an endpoint by which requests are received
  38. and responses are returned. Documentation at :
  39. Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
  40. Java AJP Connector: /docs/config/ajp.html
  41. APR (HTTP/AJP) Connector: /docs/apr.html
  42. Define a non-SSL HTTP/1.1 Connector on port 8080
  43. -->
  44. <Connectorport="18080"protocol="HTTP/1.1"
  45. connectionTimeout="20000"
  46. redirectPort="8443"/>
  47. <!-- A "Connector" using the shared thread pool-->
  48. <!--
  49. <Connector executor="tomcatThreadPool"
  50. port="8080" protocol="HTTP/1.1"
  51. connectionTimeout="20000"
  52. redirectPort="8443" />
  53. -->
  54. <!-- Define a SSL HTTP/1.1 Connector on port 8443
  55. This connector uses the JSSE configuration, when using APR, the
  56. connector should be using the OpenSSL style configuration
  57. described in the APR documentation -->
  58. <!--
  59. <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
  60. maxThreads="150" scheme="https" secure="true"
  61. clientAuth="false" sslProtocol="TLS" />
  62. -->
  63. <!-- Define an AJP 1.3 Connector on port 8009 -->
  64. <Connectorport="18009"protocol="AJP/1.3"redirectPort="8443"/>
  65. <!-- An Engine represents the entry point (within Catalina) that processes
  66. every request. The Engine implementation for Tomcat stand alone
  67. analyzes the HTTP headers included with the request, and passes them
  68. on to the appropriate Host (virtual host).
  69. Documentation at /docs/config/engine.html -->
  70. <!-- You should set jvmRoute to support load-balancing via AJP ie :
  71. <Engine name="Standalone" defaultHost="localhost" jvmRoute="jvm1">
  72. -->
  73. <Enginename="Catalina"defaultHost="localhost">
  74. <!--For clustering, please take a look at documentation at:
  75. /docs/cluster-howto.html (simple how to)
  76. /docs/config/cluster.html (reference documentation) -->
  77. <!--
  78. <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
  79. -->
  80. <!-- The request dumper valve dumps useful debugging information about
  81. the request and response data received and sent by Tomcat.
  82. Documentation at: /docs/config/valve.html -->
  83. <!--
  84. <Valve className="org.apache.catalina.valves.RequestDumperValve"/>
  85. -->
  86. <!-- This Realm uses the UserDatabase configured in the global JNDI
  87. resources under the key "UserDatabase". Any edits
  88. that are performed against this UserDatabase are immediately
  89. available for use by the Realm. -->
  90. <RealmclassName="org.apache.catalina.realm.UserDatabaseRealm"
  91. resourceName="UserDatabase"/>
  92. <!-- Define the default virtual host
  93. Note: XML Schema validation will not work with Xerces 2.2.
  94. -->
  95. <Hostname="localhost"appBase="webapps"
  96. unpackWARs="true"autoDeploy="true"
  97. xmlValidation="false"xmlNamespaceAware="false">
  98. <!-- SingleSignOn valve, share authentication between web applications
  99. Documentation at: /docs/config/valve.html -->
  100. <!--
  101. <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
  102. -->
  103. <!-- Access log processes all example.
  104. Documentation at: /docs/config/valve.html -->
  105. <!--
  106. <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
  107. prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>
  108. -->
  109. </Host>
  110. </Engine>
  111. </Service>
  112. </Server>

Tomcat2 的Server.xml配置文件

  1. <!-- Note: A "Server" is not itself a "Container", so you may not
  2. define subcomponents such as "Valves" at this level.
  3. Documentation at /docs/config/server.html
  4. -->
  5. <Serverport="28005"shutdown="SHUTDOWN">
  6. <!--APR library loader. Documentation at /docs/apr.html -->
  7. <ListenerclassName="org.apache.catalina.core.AprLifecycleListener"SSLEngine="on"/>
  8. <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
  9. <ListenerclassName="org.apache.catalina.core.JasperListener"/>
  10. <!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html -->
  11. <ListenerclassName="org.apache.catalina.mbeans.ServerLifecycleListener"/>
  12. <ListenerclassName="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
  13. <!-- Global JNDI resources
  14. Documentation at /docs/jndi-resources-howto.html
  15. -->
  16. <GlobalNamingResources>
  17. <!-- Editable user database that can also be used by
  18. UserDatabaseRealm to authenticate users
  19. -->
  20. <Resourcename="UserDatabase"auth="Container"
  21. type="org.apache.catalina.UserDatabase"
  22. description="User database that can be updated and saved"
  23. factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
  24. pathname="conf/tomcat-users.xml"/>
  25. </GlobalNamingResources>
  26. <!-- A "Service" is a collection of one or more "Connectors" that share
  27. a single "Container" Note: A "Service" is not itself a "Container",
  28. so you may not define subcomponents such as "Valves" at this level.
  29. Documentation at /docs/config/service.html
  30. -->
  31. <Servicename="Catalina">
  32. <!--The connectors can use a shared executor, you can define one or more named thread pools-->
  33. <!--
  34. <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
  35. maxThreads="150" minSpareThreads="4"/>
  36. -->
  37. <!-- A "Connector" represents an endpoint by which requests are received
  38. and responses are returned. Documentation at :
  39. Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
  40. Java AJP Connector: /docs/config/ajp.html
  41. APR (HTTP/AJP) Connector: /docs/apr.html
  42. Define a non-SSL HTTP/1.1 Connector on port 8080
  43. -->
  44. <Connectorport="28080"protocol="HTTP/1.1"
  45. connectionTimeout="20000"
  46. redirectPort="8443"/>
  47. <!-- A "Connector" using the shared thread pool-->
  48. <!--
  49. <Connector executor="tomcatThreadPool"
  50. port="8080" protocol="HTTP/1.1"
  51. connectionTimeout="20000"
  52. redirectPort="8443" />
  53. -->
  54. <!-- Define a SSL HTTP/1.1 Connector on port 8443
  55. This connector uses the JSSE configuration, when using APR, the
  56. connector should be using the OpenSSL style configuration
  57. described in the APR documentation -->
  58. <!--
  59. <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
  60. maxThreads="150" scheme="https" secure="true"
  61. clientAuth="false" sslProtocol="TLS" />
  62. -->
  63. <!-- Define an AJP 1.3 Connector on port 8009 -->
  64. <Connectorport="28009"protocol="AJP/1.3"redirectPort="8443"/>
  65. <!-- An Engine represents the entry point (within Catalina) that processes
  66. every request. The Engine implementation for Tomcat stand alone
  67. analyzes the HTTP headers included with the request, and passes them
  68. on to the appropriate Host (virtual host).
  69. Documentation at /docs/config/engine.html -->
  70. <!-- You should set jvmRoute to support load-balancing via AJP ie :
  71. <Engine name="Standalone" defaultHost="localhost" jvmRoute="jvm1">
  72. -->
  73. <Enginename="Catalina"defaultHost="localhost">
  74. <!--For clustering, please take a look at documentation at:
  75. /docs/cluster-howto.html (simple how to)
  76. /docs/config/cluster.html (reference documentation) -->
  77. <!--
  78. <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
  79. -->
  80. <!-- The request dumper valve dumps useful debugging information about
  81. the request and response data received and sent by Tomcat.
  82. Documentation at: /docs/config/valve.html -->
  83. <!--
  84. <Valve className="org.apache.catalina.valves.RequestDumperValve"/>
  85. -->
  86. <!-- This Realm uses the UserDatabase configured in the global JNDI
  87. resources under the key "UserDatabase". Any edits
  88. that are performed against this UserDatabase are immediately
  89. available for use by the Realm. -->
  90. <RealmclassName="org.apache.catalina.realm.UserDatabaseRealm"
  91. resourceName="UserDatabase"/>
  92. <!-- Define the default virtual host
  93. Note: XML Schema validation will not work with Xerces 2.2.
  94. -->
  95. <Hostname="localhost"appBase="webapps"
  96. unpackWARs="true"autoDeploy="true"
  97. xmlValidation="false"xmlNamespaceAware="false">
  98. <!-- SingleSignOn valve, share authentication between web applications
  99. Documentation at: /docs/config/valve.html -->
  100. <!--
  101. <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
  102. -->
  103. <!-- Access log processes all example.
  104. Documentation at: /docs/config/valve.html -->
  105. <!--
  106. <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
  107. prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>
  108. -->
  109. </Host>
  110. </Engine>
  111. </Service>
  112. </Server>

Nginx配置文件

  1. #user nobody;
  2. worker_processes 1;
  3. #error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;
  6. #pid logs/nginx.pid;
  7. events {
  8. worker_connections 1024;
  9. }
  10. http {
  11. include mime.types;
  12. default_type application/octet-stream;
  13. #log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘
  14. # ‘$status $body_bytes_sent "$http_referer" ‘
  15. # ‘"$http_user_agent" "$http_x_forwarded_for"‘;
  16. #access_log logs/access.log main;
  17. sendfile on;
  18. #tcp_nopush on;
  19. #keepalive_timeout 0;
  20. keepalive_timeout 65;
  21. #gzip on;
  22. #监听localhost的80端口
  23. server {
  24. listen 80;
  25. server_name localhost;
  26. location / {
  27. proxy_connect_timeout 3;
  28. proxy_send_timeout 30;
  29. proxy_read_timeout 30;
  30. proxy_pass http://localhost;
  31. }
  32. }
  33. # another virtual host using mix of IP-, name-, and port-based configuration
  34. #
  35. #server {
  36. # listen 8000;
  37. # listen somename:8080;
  38. # server_name somename alias another.alias;
  39. # location / {
  40. # root html;
  41. # index index.html index.htm;
  42. # }
  43. #}
  44. #集群配置:服务器列表
  45. upstream localhost {
  46. server localhost:18080 weight=2;#服务器配置 weight是权重的意思,权重越大,分配的概率越大。
  47. server localhost:28080 weight=1;
  48. }
  49. # HTTPS server
  50. #
  51. #server {
  52. # listen 443 ssl;
  53. # server_name localhost;
  54. # ssl_certificate cert.pem;
  55. # ssl_certificate_key cert.key;
  56. # ssl_session_cache shared:SSL:1m;
  57. # ssl_session_timeout 5m;
  58. # ssl_ciphers HIGH:!aNULL:!MD5;
  59. # ssl_prefer_server_ciphers on;
  60. # location / {
  61. # root html;
  62. # index index.html index.htm;
  63. # }
  64. #}
  65. }












































以上是关于Nginx+Tomcat简单集群的主要内容,如果未能解决你的问题,请参考以下文章

nginx 与 tomcat 集群 一二事 - 简单介绍

Nginx + Tomcat 配置负载均衡集群简单实例

Nginx+Tomcat搭建集群环境

Nginx + Tomcat 配置负载均衡集群

Nginx+Tomcat 集群部署

Nginx + Tomcat 配置负载均衡集群