Use Apache reverse proxy to connect to Tomcat

Source: Internet
Author: User

Apache HTTP Server and Tomcat server are integrated. Generally, users only want to publish the Apache HTTP Server URL, while the Tomcat URL is not published, playing the role of a hero behind the scenes. The HTTP request for accessing Tomcat is forwarded to Tomcat through Apache. After Tomcat completes processing, it returns the HTTP Response to Apache, and then the Apache HTTP response is sent back to the client browser.

There are many methods for direct HTTP data transmission between Apache HTTP Server and Tomcat server.

Method 1: Use mod_jk. Many websites introduce the use of mod_jk.so when integrating Apache HTTP Server and Tomcat server. This is an old method and requires additional downloading of mod_jk. The default configuration files of Apache and tomcat must be modified.

Method 2: URL rewrite, that is, forward a URL in the specified format to a Tomcat URL. The specified format here refers to the regular expression used by Apache. Generally, it is a relatively advanced wildcard similar. This method does not need to be stored in additional files. You only need to configure Apache.

Method 3: mod_proxy_ajp, which is only available in Apache 2.1 and later versions. A new function module is provided by Apache. Apache uses Apache JServ protocol to communicate with Tomcat. No additional files are required. You need to change the default configuration files of Apache and tomcat.

Method 4: mod_proxy. In fact, mod_proxy can be used as a company's LAN Internet sharing proxy like Wingate, or a reverse proxy (reverse proxy ). The reverse proxy function is used here. The client browser does not need to change the proxy server to the Apache address here. Mod_proxy is a built-in function of Apache and is easy to configure.

This article describes how to configure Apache reverse proxy to forward HTTP requests to Tomcat. It is relatively simple and practical.

Download Apache Web Server 2.2. After the installation is complete, modify the conf/httpd. conf file in the installation directory and remove the annotator # from the following two lines.

# Loadmodule proxy_module modules/mod_proxy.so
# Loadmodule proxy_http_module modules/mod_proxy_http.so

Add

Proxypass/app1 http: // <tomcat_server_address>: Port/url1

Proxypassreverse/app1 http: // <tomcat_server_address>: Port/url1

Save the configuration file and restart Apache. When a user accesses

Http: // <apache_server_address>/app1, Apache automatically forwards the request to tomcat in the background. The user only thinks that Apache server is the application server he wants.

The careful Author can see That tomcat has not made any changes, does it mean that if Tomcat is changed to JBoss, IBM WebSphere, or BEA WebLogic, Apache can also be used for forwarding? The answer is, indeed !!

Here, we need to explain that Apache reverse proxy only performs HTTP transparent forwarding. Therefore, if you use the servelet API to obtain the request URL, server info, and so on the Tomcat side, it is tomcat. In the JSP or HTML files on Tomcat, they should all be relative paths. absolute paths should not be used, and URLs with Tomcat addresses should not be used. This should not be too high, right?

Currently, the latest Apache cache can only be configured at the server or virtual host level, but cannot be configured at the url level. Fortunately, the general J2EE application performance problems are generally found in the dynamic web page section. The static Web page is good, so it does not matter much.

For highly demanding applications, such as news websites or Forum websites with a large number of users, if the requirement is to use Apache to buffer dynamic web pages in the background, for example, my homepage can be cached for one minute, in this minute, for example, two thousand user accesses http: // somehost/index. JSP, only for the first user access, Apache will forward the request to Tomcat, and the remaining 999 accesses are all retrieved from the buffer by Apache and returned to the user. In this way, the load on the dynamic Web Page Server in the background will be much smaller. In the latest version of Apache, if reverse proxy fails to meet this requirement, use apaceh to create a virtual host and use URL rewrite. This has nothing to do with the question.

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.