Tomcat 1.1 drops (3) Integration of Apache and Nginx

Source: Internet
Author: User

Tomcat 1.1 drops 3) Integrate Apache and Nginx

Preface:The last two articles describe how to install and deploy Tomcat. However, there is a problem. When Tomcat needs to process static pages, it is slow, at least not as good as Apache, so Apache + Tomcat integration occurs.
However, some people have summarized some reasons for the integration of the two open-source software on the Internet:

1. aggregation. Apache is used as the front-end door and multiple Tomcat instances are deployed at the backend. If an instance encounters a problem, Apache ignores this instance, so that the system administrator can rest assured. Of course, there is no need to integrate Tomcat Based on the hardware Load Balancing Device.
2. Aggregation/security. Of course, if Apache is used as a portal to distinguish Tomcat with different URLs. Every Tomcat is a protected area. From a security perspective, you only need the main Apache server. In this solution, Apache has become a lightweight proxy server.
3. Security. In terms of security, integration and disintegration are the focus of both parties. Java has the security management function, and Apache has a thorough consideration of security. You can use Google to access this information. Integration is the best strategy in a certain scenario, but in another scenario, integration may not be better. However, if you integrate Apache and Tomcat, remember that you need to pay attention to the security of Apache and Tomcat at the same time, rather than one.
4. Additional factors. Running perl, PHP, and CGI on Apache is common. However, Tomcat is not only fast but not formal. Apache can add hundreds of additional software at will. Of course Tomcat also has this capability, but such modules have not yet been developed.
5. decoration. When Apache is the front-end of Tomcat, you can complete a lot of decoration work that Tomcat does not support or has no ready-made code. There is no need to re-develop a set of mod_headers, mod_rewrite, and mod_alias similar to Apache for Tomcat.
6. Speed. Apache's static webpage processing speed is higher than that of Tomcat. However, unless your traffic is high, this is meaningless. In other cases, Tomcat is faster than Apache. Therefore, the combination of the two can accelerate your site.
7. Socket handling stands for connection)/system stability. Apache is better than Tomcat in handling error links. The main reason is that all Tomcat connections are handled by JVM, while JVM is cross-platform. The problem is that cross-platform optimization is a severe test. Java programs are normal most of the time, but when the situation is very bad, for example: very frequent disconnection, invalid data packets, invalid IP address invalid requests. Apache is better than JVM-based programs.
The following describes how to integrate Apache + Tomcat:
Apache and tomcat load cluster integration methods include jk, jk_proxy, and http_proxy.
This experiment is jk. Now, let's start:
1. Prepare and install the package file:
Required packages:
Httpd-2.2.16.tar.gz # Apache source package
Apache-tomcat-6.0.35.tar.gz # Tomcat compressed package
Tomcat-connectors-1.2.30-src.tar.gz # connect Apache and Tomcatmod_jk)

1. First install Apache

 
 
  1. # Tar xzvf httpd-2.2.0.tar.gz
  2. # Cd httpd-2.2.0
  3. #./Configure -- prefix =/usr/local/apache2 -- enable-so # The simplest compilation parameter for testing
  4. # Make & make install

2. Install the java environment before installing Tomcat. Refer to the first article)

 
 
  1. # cp apache-tomcat-6.0.35.tar.gz /usr/local  
  2. # cd /usr/local  
  3. # tar zxf apache-tomcat-6.0.35.tar.gz  
  4. # mv apache-tomcat-6.0.35 tomcat 

3. Finally install the connector mod_jk)

 
 
  1. # Tar zxf tomcat-connectors-1.2.30-src.tar.gz
  2. # Cd tomcat-connectors-1.2.30-src/native
  3. #./Configure -- with-apxs =/usr/local/apache2/bin/apxs
  4. # Make
  5. # Cp./apache-2.0/mod_jk.so/usr/local/apache2/modules/# copy to the directory of the specified Apache module

2. Configure relevant parameter files
1. First, it is related to Apache. The mod_jk module has just been installed and has been copied to the Module Directory of Apache. The following describes how to enable Apache to support it.
Create two configuration files in the conf directory of Apache:
Mod_jk.conf # module configuration file
Workers. properties # define the configuration file for Tomcat

Vim mod_jk.conf

 
 
  1. JkWorkersFile/usr/local/apache2/conf/workers. properties # specify the location of workers. properties
  2. JkLogFile/usr/local/apache2/logs/mod_jk.log # specify the log output file of jk.
  3. JkLogLevel info # specify the Log Level
  4. JkLogStampFormat "[% a % B % d % H: % M: % S % Y]" # specify the log output timestamp format
  5. JkOptions + ForwardKeySize + ForwardURICompat-ForwardDirectories # JkOptions indicates the size of the SSL key sent
  6. JkRequestLogFormat "% w % V % T" # specify the content following the timestamp in the log: % w: Working tomcat instance % V: Target ip address % T: Time consumed
  7. JkMount/servlet/* worker1 # indicates that the URL points to worker1 if a servlet exists.
  8. JkMount/*. jsp worker1 # indicates the page with the URL/*. jsp, which is processed by worker1.

Vim workers. properties

 
 
  1. Worker. list = worker1 # specify the tomcat nodes to work on. If multiple nodes are separated by commas (,),
  2. Worker. worker1.type = ajp13 # specify worker1 to communicate with the Tomcat process using the ajpv13 Protocol
  3. Worker. worker1.host = localhost # specify the location of worker1
  4. Worker. worker1.port = 8009 # specify the worker port of worker1
  5. Worker. worker1.cachesize = 10 # specify the cache size of worker1
  6. Worker. worker1.cache _ timeout = 600 # specify the time for retaining an open socket in the cache of worker1
  7. Worker. worker1.socket _ keepalive = 1 # This configuration item enables the OS to send KEEP_ALIVE information every second when there is a firewall between Apache and Tomcat, to prevent the firewall from disconnecting the inactive network.
  8. Worker. worker1.socket _ timeout = 300 # specify how long the connection on worker1 lasts without being activated. Apache will take the initiative to cut off

The following figure shows how to modify the main configuration file httpd. conf of Apache.
A. Add index. jsp ### 166 rows to DirectoryIndex
B. modify it to: DocumentRoot "/var/wwwroot" ### row 104
C. modify it to <Directory "/var/wwwroot" >### 131 rows # the number of rows may be less than the difference
D. Add:
LoadModule jk_module modules/mod_jk.so
Include/usr/local/apache2/conf/mod_jk.conf

2. Modify the Tomcat configuration file server. xml.
Add:
<Context path = "" docBase = "/var/wwwroot" reloadable = "true" crossContext = "true"> </Context> # specify the project directory

The configuration is complete. The following test:

Iii. Test and Acceptance
Mkdir/var/wwwroot
Echo "Hello, The time is <% = new java. util. Date () %>">/var/wwwroot/index. jsp # create a test webpage

/Usr/local/apache2/bin/apachectl start # start Apache
/Usr/local/tomcat/bin/startup. sh # start Tomcat

Login access: http: // ip or http: // ip: 8080 should be the same content.

650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/114H5LB-0.jpg "/>

That is, the current system time is displayed on the webpage:
Hello, the time is Tue Feb 26 11:18:04 CST 2013

The above is a simple method for integrating Apache and Tomcat.

----------------------
If you have high concurrency requirements, you can consider Nginx. This is one of the reasons why Nginx is very popular. Next we will talk about how this lightweight Web server is integrated with Tomct.
Experiment: Nginx + Tomcat
1. install and configure Nginx
Required packages:
Nginx-1.0.10.tar.gz # Nginx source package
Pcre-8.10.tar.gz # Nginx dependent lightweight function library

1. Basic installation

 
 
  1. # Tar zxf pcre-8.10.tar.gz
  2. # Cd pcre-8.10
  3. #./Configure
  4. # Make & make install
  5.  
  6. # Tar zxf nginx-1.0.10.tar.gz
  7. # Cd nginx-1.0.10
  8. #./Configure -- with-http_stub_status_module -- with-http_ssl_module # Start server status and https Module
  9. # Make & make install

Ii. configuration files
After Nginx is installed, the default path is/usr/local/nginx.
Create a proxy configuration file under/usr/local/nginx/conf:
Vim proxy. conf

 
 
  1. Proxy_redirect off; # disable Proxy Redirection
  2. Proxy_set_header Host $ host;
  3. Proxy_set_header X-Real-IP $ remote_addr; # obtain the Real ip Address
  4. # Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for; # obtain the real ip address of the proxy
  5. Client_max_body_size 10 m; # maximum number of bytes allowed for client requests per file
  6. Client_body_buffer_size 128 k; # maximum number of bytes for the buffer proxy to buffer client requests
  7. Proxy_connect_timeout 90; # timeout for backend server connection
  8. Proxy_send_timeout 90; # backend server data return time
  9. Proxy_read_timeout 90; # time when the backend server processes the request
  10. Proxy_buffer_size 4 k; # Set the buffer size
  11. Proxy_buffers 4 32 k; # Set the buffer size and quantity
  12. Proxy_busy_buffers_size 64 k; # used to control the number of buffers simultaneously transmitted to the client
  13. Proxy_temp_file_write_size 64 k; # sets the cache folder size. If it is greater than this value, it will be transmitted from the upstream Server

Then modify the Nginx main configuration file nginx. conf.
Add the following parameters to server:
Root/var/wwwroot; # specify the root directory of the webpage
Location ~ . *. Jsp $ {# define all elements ending with. jsp
Index. jsp; # The default webpage is index. jsp.
Proxy_pass http: // localhost: 8080; # The proxy address is port 8080 of the local machine.
}
To test the accuracy of the Nginx configuration file:
/Usr/local/nginx/sbin/nginx-t
As shown below, there is no problem:
Nginx: the configuration file/usr/local/nginx/conf/nginx. conf syntax is OK
Nginx: configuration file/usr/local/nginx/conf/nginx. conf test is successful

/Usr/local/nginx/sbin/nginx # Start nginx

Netstat-antp | grep nginx # Check whether the nginx port is enabled properly

Iii. Test
This experiment is continued on the basis of Apache + Tomcat above, so I didn't install Tomcat any more. That is, the root directory of the website created above is/var/wwwroot, and the test webpage has been completed on it.

Open the browser and enter http: // ip/index. jsp to see the effect.

650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/114H51041-1.jpg "/>

Nginx + Tomcat has been completed, but it is just a simple integration. The next article will continue to describe Apache + Tomcat cluster configuration, Nginx + Tomcat cluster configuration, and Session synchronization.

 

Follow-up:Tomcat 1.1 drop 3) Cluster deployment and session sharing
  Tomcat full series notes:

Tomcat 1.1 drops) basic setup

Tomcat 1.1 drops 2) configure the application

Tomcat 1.1 drops 3) Integrate Apache and Nginx

Tomcat 1.1 drops 4) Cluster deployment and session sharing

Tomcat 1.1 drops 5) Performance Tuning

This article is from the "____ documents. Time ^ notes" blog, please be sure to keep this http://xtony.blog.51cto.com/3964396/1140591

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.