Build a high-performance JAVA platform using Nginx + Resin

Source: Internet
Author: User

Currently, there are many popular JavaEE containers: Tomcat, Resin, JBoss, and Glassfish. We usually use the first three containers. Who is more stable in terms of java container performance, what about higher concurrency? The current version of resin is 4.0.33, which is known to be more concurrent than apache. It not only supports jsp language, but also php.

We have used tomcat many times before. We know that if you want to configure multiple services on one machine, you only need to copy different configuration files and configure different ports. In comparison, resin is troublesome to configure multiple services on one machine. Let's take a look at it today.

I. System Environment

 
 
  1. System Environment: CentOS6.0x86 _ 64

  2. Nginx version: Nginx-1.2.6

  3. Resin version: Resin-4.0.33

  4. JDK: JDK-1.6.0_18

Ii. Install Nginx

 
 
  1. # First download the software to be installed: [JDK can be downloaded and installed on the java website]

  2. Wget http://nginx.org/download/nginx-1.2.6.tar.gz

  3. Wget http://www.caucho.com/download/resin-4.0.33.tar.gz

  4. Tar zxvf nginx-1.2.6.tar.gz

  5. Cd nginx-1.2.6/

  6. ./Configure -- user = www -- group = www -- add-module = ../ngx_cache_purge-1.4

  7. \ -- Prefix =/usr/local/nginx \ -- with-http_stub_status_module -- with-http_ssl_module

  8. Make & make install

Iii. Resin Installation

 
 
  1. tar –xzvf resin-4.0.33.tar.gz

  2. cd resin-4.0.33 &&./configure --prefix=/usr/local/resin

  3. \--with-resin-log=/data/logs/resin/ --with-java-home=/usr/java/jdk1.6.0_18/

  4. make &&make install

Iv. Configure Resin

 
 
  1. # Cd/usr/local/resin/conf/

  2. # Add the following code to configure ports 8080 and 8081 for two instances on the local machine

  3. <Clusterid = "app1">

  4. <! -- Define the servers in the cluster -->

  5. <Server-multiid-prefix = "app1" address-list = "$ {appdomainservers}" port = "6800"/>

  6. <! -- The default host, matching any host name -->

  7. <Hostid = "" root-directory = ".">

  8. <Web-appid = "/" root-directory = "/var/www/html/app1"/>

  9. </Host>

  10. </Cluster>

  11. <Clusterid = "app2">

  12. <! -- Define the servers in the cluster -->

  13. <Server-multiid-prefix = "app2" address-list = "$ {app2_servers}" port = "6801"/>

  14. <! -- The default host, matching any host name -->

  15. <Hostid = "" root-directory = ".">

  16. <Web-appid = "/" root-directory = "/var/www/html/app2"/>

  17. </Host>

  18. </Cluster>

Modify the current directory vi resin. properties to modify the following content:

 
 
  1. # app-tier Triad servers: app-0 app-1 app-2

  2. app1_servers : 127.0.0.1:6800

  3. app2_servers : 127.0.0.1:6801

  4. # Use overrides for individual server control, for example: app-0.http : 8081

  5. app1.http : 8080

  6. app2.http : 8081

Configure different ports and then start the resin service:

 
 
  1. /Usr/local/resin/bin/resinctl start

  2. Check the resin port netstat-tnl. We will see ports 8080 and 8081.

5. Configure Nginx

With the extensive use of Nginx High-Performance Web servers, the latest stable version of Nginx is 1.2.6. Dr. Zhang uses Nginx extensively in practical applications and shares knowledge about Nginx High-Performance Web servers, nginx is also developing rapidly in China. Now let's take a look at the knowledge of Nginx dynamic/static separation, which is for reference only.

I. Practical environment:

 
 
  1. System Version: CentOS6.0 X86_64

  2. Nginx version: Nginx-1.2.6

  3. Tomcat version: Tomcat-6.0.18

Ii. Nginx installation:

To install Nginx in the actual environment, first install the pcre Library and then install Nginx:

 
 
  1. # Install pcre to support the rewrite library. You can also install the source code. Note * when installing the source code, specify the pcre path as the path to extract the source code, instead of the compiled path. Otherwise, an error is reported.

  2. Yum install pcre-devel pcre-y


  3. # Download the Nginx source code package

  4. Http://nginx.org/download/nginx-1.2.6.tar.gz cd/usr/src; wget-c


  5. # Decompress the Nginx source code package

  6. Tar-xzf nginx-1.2.6.tar.gz


  7. # Go To The extract directory, and then sed to change the Nginx version information to TDTWS

  8. Cd nginx-1.2.6; sed-I-e's/1.2.6 // G'-e's/nginx \ // TDTWS/G'-e's/"NGINX"/"TDTWS"/g 'src/core/nginx. h


  9. # Pre-compile Nginx

  10. ./Configure -- user = www -- group = www -- prefix =/usr/local/nginx -- with-http_stub_status_module -- with-http_ssl_module

  11. #. After the configure pre-compilation is successful, run the make command to compile


  12. Make

  13. # After make is successfully executed, execute make install to officially install

  14. Make install

  15. # Nginx installation has been completed since now !!!

3. Configure Nginx:

In view of the fact that my 51CTO blog already has Tomcat installation and configuration, ignore this and only configure Nginx.

 
 
  1. # Enter the Nginx application directory

  2. Cd/usr/local/nginx/conf

  3. # Back up the original nginx. conf file

  4. Mv nginx. conf nginx. bak

Create vi nginx. conf and write the following content:

 
 
  1. User www;

  2. Worker_processes 8;

  3. Worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

  4. Pid/usr/local/nginx. pid;

  5. Worker_rlimit_nofile 102400;

  6. Events

  7. {

  8. Use epoll;

  9. Worker_connections 102400;

  10. }

  11. Http

  12. {

  13. Include mime. types;

  14. Default_type application/octet-stream;

  15. Fastcgi_intercept_errors on;

  16. Charset UTF-8;

  17. Server_names_hash_bucket_size 128;

  18. Client_header_buffer_size 4 k;

  19. Large_client_header_buffers 4 32 k;

  20. Client_max_body_size 300 m;

  21. Sendfile on;

  22. Tcp_nopush on;

  23. Keepalive_timeout 60;

  24. Tcp_nodelay on;

  25. Client_body_buffer_size 512 k;

  26. Proxy_connect_timeout 5;

  27. Proxy_read_timeout 60;

  28. Proxy_send_timeout 5;

  29. Proxy_buffer_size 16 k;

  30. Proxy_buffers 4 64 k;

  31. Proxy_busy_buffers_size 128 k;

  32. Proxy_temp_file_write_size 128 k;

  33. Gzip on;

  34. Gzip_min_length 1 k;

  35. Gzip_buffers 4 16 k;

  36. Gzip_http_version 1.1;

  37. Gzip_comp_level 2;

  38. Gzip_types text/plain application/x-javascript text/css application/xml;

  39. Gzip_vary on;

  40. ### 2012-12-19 change nginx logs

  41. Log_format main '$ http_x_forwarded_for-$ remote_user [$ time_local] "$ request "'

  42. '$ Status $ body_bytes_sent "$ http_referer "'

  43. '"$ Http_user_agent" $ request_time $ remote_addr ';


  44. # Here is the configuration of the backend server wugk application cluster, which can be modified according to the actual backend situation. tdt_wugk is the name of the Server Load balancer instance and can be specified arbitrarily.

  45. # However, it must be consistent with the pass segment of the vhosts. conf VM. Otherwise, the backend requests cannot be forwarded.

  46. Upstream tdt_wugk {

  47. Server 10.10.141.30: 8080 weight = 1max_fails = 2fail_timeout = 30 s;

  48. Server 10.10.141.30: 8081 weight = 1max_fails = 2fail_timeout = 30 s;

  49. Server 10.10.141.31: 8080 weight = 1max_fails = 2fail_timeout = 30 s;

  50. Server 10.10.141.31: 8081 weight = 1max_fails = 2fail_timeout = 30 s;

  51. Server 10.10.141.32: 8080 weight = 1max_fails = 2fail_timeout = 30 s;

  52. Server 10.10.141.32: 8081 weight = 1max_fails = 2fail_timeout = 30 s;

  53. }

  54. # Here is the backend APP Server Load balancer configuration, which can be modified based on the actual backend situation. Tdt_app is the Server Load balancer name, which can be specified at will

  55. Upstream tdt_app {

  56. Server 10.10.141.40: 8080 weight = 1max_fails = 2fail_timeout = 30 s;

  57. Server 10.10.141.40: 8081 weight = 1max_fails = 2fail_timeout = 30 s;

  58. Server 10.10.141.41: 8080 weight = 1max_fails = 2fail_timeout = 30 s;

  59. Server 10.10.141.41: 8081 weight = 1max_fails = 2fail_timeout = 30 s;

  60. Server 10.10.141.42: 8080 weight = 1max_fails = 2fail_timeout = 30 s;

  61. Server 10.10.141.42: 8081 weight = 1max_fails = 2fail_timeout = 30 s;

  62. }

  63. # Include references vhosts. conf. This file is mainly used to configure the Nginx Virtual Host

  64. Include vhosts. conf;

  65. }

After nginx. conf is configured, configure the nginx virtual host and create vhosts. conf in the current directory.

Vi vhosts. conf:

 
 
  1. #### Www.wuguangke.cn

  2. Server

  3. {

  4. Listen 80;

  5. Server_name www.wuguangke.cn;

  6. Index index.html index.htm;

  7. # Configure the publishing directory as/data/www/wugk

  8. Root/data/www/wugk;

  9. Location/

  10. {

  11. Proxy_next_upstream http_502 http_504 error timeout invalid_header;

  12. Proxy_set_header Host $ host;

  13. Proxy_set_header X-Real-IP $ remote_addr;

  14. Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;

  15. Proxy_pass http: // tdt_wugk;

  16. Expires 3d;

  17. }

  18. # Send the dynamic page to http: // tdt_wugk, that is, the upstream tdt_wugk balance defined in nginx. conf.

  19. Location ~ . * \. (Php | jsp | cgi )? $

  20. {

  21. Proxy_set_header Host $ host;

  22. Proxy_set_header X-Real-IP $ remote_addr;

  23. Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;

  24. Proxy_pass http: // tdt_wugk;

  25. }

  26. # Configure Nginx static/dynamic separation. The defined static page is directly read from the Nginx release directory.

  27. Location ~ . * \. (Html | htm | gif | jpg | jpeg | bmp | png | ico | txt | js | css) $

  28. {

  29. Root/data/www/wugk;

  30. # Expires defines that the user's browser cache time is 3 days. If the static page is not updated frequently, you can set a longer period, which can save bandwidth and relieve server pressure.

  31. Expires 3d;

  32. }

  33. # Define the path of Nginx output logs

  34. Access_log/data/logs/nginx_wugk/access. log main;

  35. Error_log/data/logs/nginx_wugk/error. log crit;

  36. }


  37. ######### Chinaapp.sinaapp.com 2012-12-19

  38. Server

  39. {

  40. Listen 80;

  41. Server_name chinaapp.sinaapp.com;

  42. Index index.html index.htm;

  43. Root/data/www;

  44. Location/

  45. {

  46. Proxy_next_upstream http_502 http_504 error timeout invalid_header;

  47. Proxy_set_header Host $ host;

  48. Proxy_set_header X-Real-IP $ remote_addr;

  49. Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;

  50. Proxy_pass http: // tdt_app;

  51. Expires 3d;

  52. }

  53. Location ~ . * \. (Php | jsp | cgi )? $

  54. {

  55. Proxy_set_header Host $ host;

  56. Proxy_set_header X-Real-IP $ remote_addr;

  57. Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;

  58. Proxy_pass http: // tdt_app;

  59. }

  60. Location ~ . * \. (Html | htm | gif | jpg | jpeg | bmp | png | ico | txt | js | css) $

  61. {

  62. Root/data/www/app;

  63. Expires 3d;

  64. }

  65. Access_log/data/logs/nginx_app/access. log main;

  66. Error_log/data/logs/nginx_app/error. log crit;

  67. }

6. deployment test:

After the backend is configured with the Tomcat service and started, the released program needs to be synchronized to the/data/www directory of Nginx, because after the static and dynamic separation is configured, the user requests the static page you defined, by default, requests will be sent to the nginx publishing directory instead of to the backend. Therefore, you must ensure that the backend and front-end programs are consistent at this time. You can use Rsync for automatic synchronization on the server.

 
 
  1. # Check whether the Nginx configuration file is correctly configured. The prompt "OK" and "successful" is displayed as follows:

  2. [Root @ WEB-11-151 ~] #/Usr/local/nginx/sbin/nginx-t

  3. Nginx: the configuration file/usr/local/nginx/conf/nginx. conf syntax is OK

  4. Nginx: configuration file/usr/local/nginx/conf/nginx. conf test is successful

  5. # Start the Nginx Service

  6. /Usr/local/nginx/sbin/nginx

  7. # Check whether the Nginx process is started

  8. Ps-ef | grep nginx

This article is just a simple practical case. The configuration and parameters are not described too much here and will be updated later.

This article from the "Wu guangke-Focus on automated O & M" blog, please be sure to keep this source http://wgkgood.blog.51cto.com/1192594/1094236


Related Article

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.