Home  >  Article  >  Backend Development  >  Nginx+tomcat configure cluster

Nginx+tomcat configure cluster

WBOY
WBOYOriginal
2016-08-08 09:20:13892browse

This article is reproduced from: http://os.51cto.com/art/201111/304608.htm

This article introduces the final realization of load balancing through the cluster configuration of nginx and tomcat in Windows xp and ubuntu environments .

AD At the beginning, I had a small question: why not use open source apache and Nginx software loads? F5 equipment often costs hundreds of thousands and is expensive? I had a relatively naive question, and I later understood: F5 operates on the transport layer of the IOS network model, and Nginx and apache are based on the http reverse proxy method and are located in the seventh application layer of the ISO model. To put it bluntly, the difference between TCP UDP and http protocols is that Nginx cannot provide load balancing for applications based on TCP protocols.

Understood the differences and application scenarios between the two, and developed a strong interest in Nginx. I read Zhang Yan’s "Practical Nginx" (this young and promising young man in 1985 was envious + jealous), and understood the general principles and Configuration, Ubuntu10.10, tried to configure Nginx+tomcat load balancing under window, forwarded all requests to tomcat, and did not configure static, dynamic separation, image hotlink prevention and other configurations.

Nginx Introduction

Nginx (pronounced the same as engine x) is a lightweight web server/reverse proxy server and email (IMAP/POP3) proxy server, and is released under a BSD-like protocol. Its characteristics are that it occupies less memory and has strong concurrency capabilities. In fact, nginx’s concurrency capabilities do perform better among web servers of the same type. Currently, the users of nginx websites in mainland China include: Sina, NetEase, Tencent, and other well-known micronets Plurk also uses nginx.

The above is an introduction to Nginx, which is basically nonsense. Now let’s get to the point. A combination of pictures and text will show the basic configuration, first the window environment, and secondly the Ubuntu environment (Vbox virtual).

Window xp environment: Nginx+Tomcat6

1. Download address

http://nginx.org/en/download.html. Here we recommend downloading stable versions. This article uses nginx-0.8. 20.

2. Directory structure
                                                                                                                                                                                               Log directory            “ nginx.exe main program
It is extremely simple to install Nginx under the window. Just unzip it to an English directory without spaces (personal habit, worry about problems with Chinese). Double-click nginx to start. Here I installed it to: D:server directory, which is covered below. The tomcat is also installed in this directory.

Start in DOS environment

If you want to stop nginx, run the command in dos environment: nginx -s stop
3. nginx.conf configuration

The Nginx configuration file is in the conf directory by default. The main configuration file is nginx.conf. We install it in D:servernginx-0.8.20, the default main configuration file is D:servernginx-0.8.20nginx.conf. The following is the configuration of nginx as a front-end reverse proxy server.


Nginx.conf code

  1. #The user and group used by Nginx are 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. P #USE EPOLL;
  13. #Allow the maximum number of connections
  14. worker_connections
  15. 2048
  16. ;
  17.           #Define log format  
  18.     #log_format   main  
  19. '$remote_addr - $remote_user [$time_local] $request '
  20.   " #
  21. '" $ Status "$ BODY_BYTES_SENT" $ http_referr "'
  22. " #
  23. '" $ http_user_agent "" $ http_x_Forwardeded_For "'
  24. ;
  25. #access_log off;
  26. access_log logs/access.log;
  27. client_header_timeout 3m;
  28. client_body_timeout 3m;
  29. send_timeout 3m;
  30. client_header_buffer_size 1k;
  31. large_client_header_buffers
  32. 4
  33. 4k;
  34. sendfile on;
  35. tcp_nopush on;
  36. tcp_nodelay on;
  37. #keepalive_timeout 75
  38. 20
  39. ;
  40. include gzip.conf;
  41. upstream localhost {
  42.   #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.
  43.           #In the case of multiple networks, the IP may be different when routing is switched
  44.         #ip_hash;
  45. }
  46. server {
  47. Listen proxy_connect_timeout 3
  48. ;
  49. proxy_send_timeout
  50. 30
  51. ;
  52. proxy_read_timeout 3 0
  53. ;                                                     proxy_pass http://localhost; }                                                                    
  54. proxy_set_header                       Host $host;
  55. proxy_set_header                                             
  56. ;
  57. proxy_send_timeout
  58. 300;
  59. proxy_read_timeout
  60. 300;
  61. proxy_buffer_size 4k;
  62. proxy_buffers 4
  63. 32k;
  64. proxy_busy_buffers_size 64k;
  65. proxy_temp_file_write_size 64k;
  66. gzip compression related configuration is as follows:

  67. Gzip.conf code:

gzi p on;

gzip_min_length 1000

;
  1. gzip_types text /plain text/css application/x-javascript;
  2. 4. Tomcat configuration
  3. Everyone is familiar with tomcat. You only need to modify the server.xml configuration file. Here we use apache-tomcat-6.0.14 For example, decompress and name them in the server directory: apache-tomcat-6.0.14_1, apache-tomcat-6.0.14_2.
  4. The first port modification:
  5. Xml code
  6. <
  7. Server
  8. port
  9. =
  10. "18006"
  11. shutdown=
  12. "SHUTDOWN"
  13. >

The second port modification:

Xml code

  1. <
  2. Connector
  3. port
=

"18081"protocol

=

"HTTP/1.1"

​​​​​​​​

connectionTimeout ="20000"

CRedirectport
    =
  1. "8443" /& gt;
  2. Third port modification: java code
& lt; connector port =

"8009" protocol = "ajp/1.3 1.3 "

redirectPort=
    "8443"
  1. />
  2. Engine element adds jvmRoute attribute: Xml code

    <Enginename="Catalina"defaultHost="localhost"jvmRoute="tomcat1">

    Don’t duplicate the two tomcat ports. To ensure that it can be started, the other tomcat configuration is omitted and Seagate is omitted, and the listening port is 18080. We will upload all the configuration information in the attachment.

    5. Verify configuration and test load balancing

    First test whether the nginx configuration is correct, test command: nginx -t (default verification: confnginx.conf), you can also specify the configuration file path.

    Secondly, verify tomcat and start two tomcats. If there is no port conflict, it is considered successful (I won’t talk nonsense about the java that tomcat relies on and so on);

    Finally, verify the configuration of load balancing settings, http:// localhost/ or http://localhost/index.jsp. I modified the index.jsp page and added log output information for easier observation. Note: On the head of the kitten in the upper left corner: access tomcat2, access tomcat1. It means that different tomcats are accessed.

    This completes the nginx+tomcat load balancing configuration under window. Regarding tomcat Session, memcached is usually used, or nginx_upstream_jvm_route is used. It is an Nginx extension module used to implement the Cookie-based Session Sticky function. If there are too many tomcats, session synchronization is not recommended. Synchronizing sessions between servers consumes resources, and high concurrency environments can easily cause session storms. Please adopt the session solution reasonably according to your own application situation.

    Ubuntu10.10 environment: Nginx+Tomcat6

    Let’s briefly talk about how to install and configure under ubuntu10.10

    1. Download Nginx

    Address: http://nginx.org/ en/download.html, linux version: nginx-0.8.20.tar.. Decompression command:

    tar -zxvf nginx-0.8.20.tar.gz


    2. Compile and install Nginx

    Nginx depends on some other PCRE, openssl (depends on libssl-dev), and my notebook Ubuntu environment has been installed For PCRE, you only need to install the dependent openssl. Let’s briefly talk about how to install PCRE and openssl, etc.

    PCRE download address: ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/

    Shell code

    1. tar zxvf pcre-8.01.tar.gz
    2. cd pcre-8.01
    3. sudo ./configure
    4. sodu make
    5. sodu make install

    openssl via apt-get install Install

    1. sudo apt-get install openssl
    2. sudo apt-get install libssl-dev
    3. //If other packages are missing, please use this method to install. Ubuntu has a dependency prompt

    The dependent software packages are installed, below To compile Nginx:

    Shell code

    1. #Copy the window shared directory software to the current working directory
    2. cp /mnt/fileshare/nginx-0.8.20.tar.gz ./
    3. #Unzip the software package
    4. tar zxvf nginx-0.8.20.tar.gz
    5. cd nginx-0.8.20
    6. //Compile source code, use nobody by default, specify existing users and groups on this machine, enable nginx-status function, and monitor nginx status. Start debug
    7. sudo ./configure --user=niumd --group=niumd --with-debug --with-http_stub_status_module
    8. sudo make
    9. sudo make install

    The compilation and installation ends correctly. Check the default configuration according to the above method under the window, then start nginx under the default configuration, visit http://127.0.0.1

    Nginx configuration After success, we will make few modifications to nginx.conf under window, as follows:

    Ubuntu nginx.conf code

    1. #User and group used by Nginx
    2. user niumd niumd;
    3. #The number of worker sub-processes (usually equal to the number of CPUs or 2 times the CPU)
    4. worker_processes 2;
    5. #Error日志存放路径 
    6. #error_log  logs/error.log; 
    7. #error_log  logs/error.log  notice; 
    8. error_log  logs/error.log  info; 
    9.  
    10. #指定pid存放文件 
    11. pid        logs/nginx. pid;
    12. events {
    13. #Use network IO model Linux recommends epoll, FreeBSD recommends kqueue
    14. Use epoll;
    15. #Maximum number of connections allowed
    16. worker_ connections
    17. 2048
    18. ; }
    19. html user [$time_local] $request '
    20. " #
    21. '" $ Status "$ BODY_BYTES_SENT" $ http_referr "'
    22. " #
    23. '" $ http_user_agent "" $ http_x_Forwardeded_For "'
    24. ;
    25. #access_log off;
    26. access_log logs/access.log;
    27. client_header_timeout 3m;
    28. client_body_timeout 3m;
    29. send_timeout 3m;
    30. client_header_buffer_size 1k;
    31. large_client_header_buffers
    32. 4
    33. 4k;
    34. sendfile on;
    35. tcp_nopush on;
    36. tcp_nodelay on;
    37. #keepalive_timeout
    38. 75
    39. 20;
    40. include gzip.conf;
    41. upstream localhost {
    42. #ip_hash
    43. #ip_hash;
    44. server localhost:
    45. 18081;                                                                                                 using using using using using       out out out out out out of   ’ s to ’ ’s ’ ‐   ‐ ‐ ‐ ‐ server { location proxy_connect_timeout 3
    46. ;
    47. proxy_send_timeout
    48. 30
    49. ; proxy_read_timeout
    50. 30
    51. ;
    52. proxy_pass http://localhost;
    53.                                                                                   users and groups;
    54. 3. Configure tomcat
    55. Please refer to the configuration under window, which is exactly the same.
    56. 4. Start and stop nginx
    57. Starting nginx under ubuntu is slightly different from that in window. The general starting and stopping method is as follows.
    58. Java code
      1. #nginx目录执行 
      2. sbin/nginx 
      3. 或通过-c 指定配置文件 
      4. sbin/nginx -c usr/local/nginx8.20/conf/nginx/conf

      Shell代码

      1. niumd@niumd-laptop:/usr/local/nginx$ pwd 
      2. /usr/local/nginx 
      3. niumd@niumd-laptop:/usr/local/nginx$ sudo sbin/nginx -t 
      4. the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok 
      5. configuration file /usr/local/nginx/conf/nginx.conf test is successful 
      6. niumd@niumd-laptop:/usr/local/nginx$ sudo sbin/nginx -v 
      7. nginx version: nginx/0.8.20 
      8. niumd@niumd-laptop:/usr/local/nginx$ sudo sbin/nginx -V 
      9. nginx version: nginx/0.8.20 
      10. built by gcc 4.4.3 (Ubuntu 4.4.3-4ubuntu5)  
      11. configure arguments: --user=niumd --group=niumd --with-debug --with-http_sub_module 
      12. niumd@niumd-laptop:/usr/local/nginx$ sudo sbin/nginx  
      13. niumd@niumd-laptop:/usr/local/nginx$ ps -ef|grep nginx 
      14. root      5158     1  022:32 ?        00:00:00 nginx: master process sbin/nginx 
      15. niumd     5159  5158  022:32 ?        00:00:00 nginx: worker process 
      16. niumd     5161  1577  022:32 pts/0    00:00:00 grep --color=auto nginx 
      17. niumd@niumd-laptop:/usr/local/nginx$  

      我们通过ps  -ef|grep nginx,看到如下结果:

      注意:在启动时linux提示一句警告【warn】……,是因为我们设置的 #允许最大连接数 worker_connections  2048,超过linux默认1024的限制。

      停止:kill -信号类型 pid

      nginx/logs目录下有个nginx。pid的文件,此文件记录了每次运行的pid,也可以通过ps命令查询。

      信号类型如下:

      信号类型 描述
      RERM.INT 快速关闭
      HUP 平滑重启,加载配置
      USR1 重新加载日志
      USER2 平滑升级执行程序
      WINCH 从容关闭工作进程
      QUIT 从容关闭

      以上就介绍了Nginx+tomcat配置集群,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

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