Home  >  Article  >  Backend Development  >  Nginx+tomcat cluster environment construction

Nginx+tomcat cluster environment construction

WBOY
WBOYOriginal
2016-08-08 09:26:501077browse

Experimental environment: windows xp sp3

Nginx version: 1.5.12;

Download address: http://nginx.org/en/download.html

Tomcat version: 6.0.39

Download address: http://tomcat.apache.org/download-60.cgi

1. Configure nginx

1. Create an nginx folder in the root directory of drive D, and extract the downloaded nginx release package nginx-1.5.12.zip to this directory.

2. D:nginxnginx-1.5.12 directory structure

Nginx-

                                                                                                                                     

                                                                                                 

|_ docs Document directory

                                                                                                                          ​                                                                                                                                                                                                                                     

                                           |

|_ nginx.exe main program

3. Start nginx

Starting nginx under windows is very simple, just double-click to run nginx.exe. Nginx runs on port 80 by default. To check whether nginx is started, we only need to enter http://localhost in the browser and you will see the following page, indicating that our nginx has been started.

1. Stop nginx

If you need to stop nginx, you need to open a command line window, enter the directory where nginx is decompressed, that is, enter the directory where the nginx.exe file is located, and enter the command nginx –s stop to stop nginx.

2. Cluster configuration

1. Configure tomcat

Create a tomcat folder in the root directory of drive D, and unzip two copies of the tomcat6.0.39 release package into this directory, naming them tomcat01 and tomcat02 respectively. In order to easily observe which tomcat we are accessing, we modify the D:tomcattomcat01webappsROOTindex.html of tomcat01

[html] view plaincopy

  1. b> Apache Tomcatb>td> changed to [html] view plaincopy b> Apache Tomcat 1
b

>

td

>

  1. Similarly, we put tomcat02’s D:tomcattomcat02webappsROOTindex.html in [html] view plaincopy
    1. b> Apache Tomcatb>td> changed to [html] view plaincopy b>
    2. Apache Tomcat 2
    b

    >

    td

    >
    1. We have two tomcats on the same computer. In order to prevent the ports of the two tomcats from conflicting, we put tomcat02’s D:tomcattomcat02confserver.xml [html] view plaincopy <Server port="8005"shutdown="SHUTDOWN">
    2. changed to

    [html] view plaincopy

      <
    1. Server port="8105"shutdown="SHUTDOWN">
[html] view plaincopy

<
  1. Connector port="8080"protocol="HTTP/1.1"                connectionTimeout=
"20000"
     

                

redirectPort

=
  1. "8443" />  changed to [html] view plaincopy
    1. <Connector port="8180" protocol="HTTP/1.1"   
    2.                connectionTimeout="20000"   
    3.                redirectPort="8543" />  


    1. <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />  
    改为

    [html] view plaincopy

    1. <Connector port="8109" protocol="AJP/1.3" redirectPort="8543" />  


    [html] view plaincopy

    1.   
    2、  配置nginx

    nginx.conf

    [html] view plaincopy

    1. #Nginx user and group, not specified under window
    2. #user niumd niumd;
    3. #The number of working sub-processes (usually equal to the number of CPUs or 2 times the CPU)
    4. worker_processes 2;
    5. #Error log storage path
    6. #error_log logs/error.log;
    7. #error_log logs/error.log notice;
    8. #error_log logs/error.log info;
    9. #Specify pid to store files
    10. #pid logs/nginx.pid;
    11. events {
    12.    #When using the network IO model, Linux recommends epoll, FreeBSD recommends kqueue, and it is not specified under window.
    13. #use epoll;
    14. #Maximum number of connections allowed
    15. worker_connections 1024;
    16. }
    17. http {
    18. include mime.types;
    19. default_type application/octet-stream;
    20. #Define log format
    21. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    22.                    '$status                                                                                   
    23.                 '"$http_user_agent" "$http_x_forwarded_for"';          
    24. access_log logs/access.log main;
    25. client_header_timeout 3m;
    26. client_body_timeout 3m;
    27. send_timeout 3m;
    28. client_header_buffer_size 1k;
    29. large_client_header_buffers 4 4k;
    30. sendfile on;
    31. tcp_nopush on;
    32. tcp_nodelay on;
    33. #keepalive_timeout 0;
    34. keepalive_timeout 65;
    35. #gzip on;
    36. upstream localhost {
    37.          #Allocate requests to each backend tomcat based on IP calculation. Many people mistakenly believe that it can solve the session problem, but it does not.​
    38.         #If the same machine is in multiple networks, the IP may be different when the route is switched
    39. ip_hash;
    40.                                                                                                                                                                                                 out out out way       off, ,         off off off surprise surpriselop off off exactly why zen to-
    41. C Server localhost: 8180;
    42.                                                                                          
    43. server {
    44. Listen 80;
    45. server_name localhost;
    46.           #charset koi8-r;                                       
    47.          #access_log  logs/host.access.log  main;                               
    48. Location / {
    49.               proxy_connect_timeout     3;                
    50. proxy_send_timeout 30;
    51.                                                                                   ’ ’ s ’ s ’ ’ ’ 1 t           ’’ ’ ’ ’ ’ ’ ’ ’ ’ ’ 1 out out out out through'' ‐ ‐ ‐‐‐‐‐‐‐‐
    52.               proxy_pass  http://localhost;             
    53.           } 
    54. P #ERROR_PAGE 404 /404.html;
    55.         # redirect server error pages to the static page /50x.html
    56.             #
    57. error_page 500 502 503 504 /50x.html;
    58. Location = /50x.html {
    59. root html;
    60.           } 
    61. }
    62. }
    63. 3. View the reverse proxy configuration results Start nginx, tomcat01, tomcat02.
    64. Enter http://localhost in the browser and you will see the management interface of tomcat01, as shown below.

    Then transparently stop tomcat02, refresh the page, and nginx automatically switches to tomcat02 for us, as shown below.



    The above introduces the establishment of Nginx+tomcat cluster environment, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn