Java Reverse proxy j2ep__ Reverse Proxy

Source: Internet
Author: User
Tags stringbuffer
Http://datalab.int-yt.com/archives/27

When deploying different applications using multiple server programs, you may encounter situations where multiple services want to use the default 80 port. However, multiple servers cannot occupy the same port at the same time. A common way to solve this problem is to use a reverse proxy.

reverse proxy has, encryption and SSL acceleration, load balancing, cache static content, compression, deceleration upload, security, extranet release and other functions.

We use the reverse proxy only to use the role of extranet publishing, should be the simplest of a reverse proxy. It is now popular to use Nginx to build a reverse proxy, but since our 80 ports have been tomcat-driven and not easily moved, I'm looking for a reverse proxy that can be based on Tomcat, which is just the equivalent of deploying an application.

Search a bit, tomcat recommended Http://wiki.apache.org/tomcat/ServletProxy looks J2EP is the most presentable, the other two saw the introduction is not interested, so directly downloaded the J2EP.

Look at the document introduction J2EP or relatively good, only need to modify/web-inf/config/data.xml file can add need proxy server, can achieve load balancing, restrict access, rewrite Web page content and other functions, and the rules are more powerful, a variety of rules can be merged. The author wants to implement features such as static caching, SSL encryption, and so on, but after publishing 1.0, it seems that the author has failed to write it down.

Actually tried it is not perfect enough, burst a variety of problems, had to modify: Unable to agent Chinese ... Solution to modify the rewrite method in the Urlrewritingoutputstream class

1 2 3 4 5 public void rewrite (server server) throws IOException {... matcher.appendtail (page); Originalstream.write (string.valueof (page). GetBytes ()); }
Cookie problem, J2EP itself is supported by cookies, but the estimated author is only testing the agent of the entire site, and did not test the agent a part of the site, in data.xml configured path after such as (http:// datalab.int-yt.com/blog//blog is path) cookies are bad because the path in Set-cookie is not replaced correctly. Solutions, modifying Rewritesetcookie methods in Urlrewritingresponsewrapper
1 2 3 4 5 6 7 8 9 10 11 12 13 14-15 16 private string Rewritesetcookie (string value) {     StringBuffer header =  new StringBuffer ();      Matcher Matcher = pathanddomainpattern.matcher (value);      while (Matcher.find ()) {         if (matcher.group (1). Equalsignorecase ("path=")) {             String Path = Server.getrule (). Revert (Matcher.group (2). Replace (Server.getpath (), contextpath));             //Server.getrule (). Revert ( Matcher.group (2));              Matcher.appendreplacement (header,   "$" + path +  ";");         }  Else {              matcher.appendreplacement (header,  "");         }     }      matcher.appendtail (header);      log.debug ("Set-cookie header rewritten \" "+ Value + " \ ">>" + header.tostring ());      return header.tostring (); }
Reverse proxy and reverse proxy encountered problems ... Well, this is a wonderful problem, and normal people don't do it. But it's a problem, and the authors don't expect it either. If you add a layer of reverse proxy may make the page can not be displayed, this should not be a mistake for J2EP, because J2EP returned the content browser is known, but the header has a transfer-encoding, which will cause the next reverse proxy server error. Therefore, the original server should be removed from the transfer-encoding. Solutions, modifying setheaders methods in Responsehandlerbase
1 2 3 4 5 6 7 8 9 10 11 12 13-14 protected void Setheaders (HttpServletResponse response) {     header[] headers = Method.getresponseheaders ();      for (int i= 0; i < headers.length; i++) {         H Eader header = Headers[i];          String name = Header.getname ();          Boolean contentlength = Name.equalsignorecase ("Content-length");          Boolean connection = Name.equalsignorecase ("Connection");          Boolean transferencoding = Name.equalsignorecase (" Transfer-encoding ");          if (!contentlength &&!connection &&!) transferencoding) {             Response.AddHeader ( Name, Header.getvalue ());         }     }      Setviaheader (response); }
can only support the Html,javascript CSS in the connection rewrite, if not more than these kinds of files need to rewrite the connection, then you have to modify the Urlrewritingresponsewrapper in the Processstream, Getoutputstream method, and add code similar to the rewrite function in Urlrewritingoutputstream. URL replacement is only supported with Slash ... Baseserver.java
1 2 3 4 5 6 7 8 9 ten public Server getservermapped (S Tring location) {         String fullpath = getdomainname () + GetPath () +  "/" ;          if (Location.length () < Fullpath.length () &&! Location.endswith ("/")) {             location+= '/';         }          if ( Location.startswith (FullPath) && isrewriting) {              return this;         }  Else {              return null;         }     }

Modified Code J2EP

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.