Implement reverse proxy server using the Apache forwarding Module

Source: Internet
Author: User

Implement reverse proxy server using the Apache forwarding Module
Another problem

The company's LAMP server uses Apache's VirtualHost function to deploy multiple websites with independent domain names. The httpd. conf configuration file is as follows:

# Omitted unrelated parts of this article Listen 80 # www.xxx.com
  
   
ServerAdmin xxx@126.com DocumentRoot "/var/www/xxx" ServerName www.xxx.com
  # Www.yyy.com
  
   
ServerAdmin yyy@126.com DocumentRoot "/var/www/yyy" ServerName yyy.com ServerAlias www.yyy.com
  # Omit other website configurations

It can be seen that Apache listens on port 80 and distributes requests to different website directories based on the domain name.

Today, the company decided to add a website on this server, which has an independent domain name zzz.com, developed using JavaEE and runs based on Tomcat.

Because Apache already listens on port 80, Tomcat, which runs independently, cannot listen on this port. And the customer requirement must access the website in the form of a http://www.zzz.com, rather than a http://www.zzz.com: 8080. Therefore, you must integrate Tomcat into Apache.

Solution

There are two main ways to integrate the Tomcat website into Apache. First, Tomcat is used as the worker of Apache through the AJP protocol, and second, the mod_proxy and mod_proxy_http modules are used to forward requests to Tomcat.

The first method should be highly efficient. After all, Tomcat is also a product of Apache, which is quite stable and easy to integrate.
The second method is highly versatile. It can be forwarded not only to Tomcat, but also to any HTTP server programs, such as IIS and other Apache instances.

Based on my hobbies, I chose the second method.

Solution

First, let Tomcat listen on port 8080.

Then modify httpd. conf.

# Load the forwarding module LoadModule proxy_module modules/mod_proxy.soLoadModule proxy_http_module modules/mod_proxy_http.so # forward all access to zzz.com to Tomcat  ServerAdmin zzz@126.com ServerName zzz.com ServerAlias www.com ProxyPass/http: // localhost: 8080/ProxyPassReverse/http: // localhost: 8080/ 

In this way, when the user accesses the http://www.zzz.com, Apache will access http: // localhost: 8080 instead of the user, and then send the retrieved http data to the user. This is the reverse proxy function of Apache. The proxy process is imperceptible to browser customers. To some extent, it also protects and hides the Tomcat Service (because Apache uses the Intranet http: // 127.0.0.1: 8080 access Tomcat service, http://www.zzz.com: 8080 is not visible to external Internet ).

Which corresponds to the Reverse Proxy is the Forward Proxy function of Apache, which requires the browser to set the Proxy server.

Future expansion

This solution is highly scalable and meaningful for Web servers that lack public IP addresses. For example, you need to deploy an IIS-based website one day. The website domain name is aaa.com and runs on an intranet Windows machine. The IP address is 172.16.35.220, and IIS listens on port 80. You can deploy the service through the reverse proxy function of Apache.

  ServerAdmin aaa@126.com ServerName aaa.com ServerAlias www.aaa.com ProxyPass / http://172.16.35.220/ ProxyPassReverse / http://172.16.35.220/ 

In this way, Apache reads data from http: // 172.16.35.220/and forwards the results to the user when the user accesses the http://www.aaa.com.

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.