Reverse proxy implements PHP and Java coexistence under the same domain name

Source: Internet
Author: User
Tags php server nginx server nginx reverse proxy

1. Background Introduction

  has been engaged in PHP development work, the company's business is implemented by PHP, recently due to the company structure fine-tuning, assigned a few Java projects to the team, but because the team is not familiar with Java, there are a few people to understand the level of Java is also slightly understand, here said a little understand, really is slightly understand, So I dare not change the Java code directly. So what do we do? After discussing the new requirements of Java project, temporarily or PHP to achieve, but there is a premise: the domain name and URL can not be changed, Java code can not use a domain name, PHP with another, because the project provides the interface is called by the mobile app client, if you change the interface domain name or URL, Client to re-docking, if the future PHP to switch back to Java and re-docking, this is very inconvenient, so the domain name and interface URL can not be changed.

There are two types of new requirements: existing functionality before the first change, and the second added functionality that was not previously available.

For the above two cases, the first case is more complex, meaning that the Java has implemented the function to abandon (the code does not change), implemented in PHP, on this basis to add the functionality of the new requirements, and the URL can not be changed. The second situation requires PHP to implement new functionality, and of course the URL is new. Based on the above analysis, we have to realize which URLs are responsible for the Java code and which URLs are responsible for the PHP code and assign them to the corresponding servers.

2. Program Design

  According to the above background analysis is to implement PHP and Java processing the same domain name of the different URL requests, after the team internal discussion can be implemented with Nginx reverse proxy, so-called reverse proxy is the request from the Internet does not directly access the Web server, Instead, we first access the reverse proxy server (we use nginx here), after which the Nginx server forwards the request to the (intranet) server.

The specific options are as follows:

First, we need to apply an intranet domain name to the PHP site and the Java site, for example (Www.php.domain and Www.java.domain)

1. If the requirement is to change the previously implemented functionality of Java. Because the URL already exists, and can not be changed, we have to transfer the URL to the PHP server processing, such a situation, can be on the reverse proxy server through the regular to match the need to use PHP to implement the existing URL, After that, rewrite him to the PHP processing path and then distributes it to the PHP server processing (Prox_pass www.php.domain).

2, if the demand is to develop new functions. Since the previous URL does not exist, first make a PHP processing URL path Convention (or rule), that is, which path requests are processed in PHP. For example: http://www.abc/php/* all requests under this path are handled in PHP, all requests for other paths are handled in Java, so that the rewrite is matched to the PHP processing path after matching the/php/path according to Nginx. Then distributed to the PHP server (Prox_pass www.php.domain).

3, the rest of the request, or need Java to handle, so when the above two URLs are not matched, the nature is handled by Java, Direct (Prox_pass Www.java.domain)

Here is the schema diagram:

    

3. Nginx Configuration File Code
server{Listen the;    server_name www.abc.com; #PHP处理的请求 Location^~/php/{rewrite^/php/(. *)? (. *) $/index.php?service=$1$2  Break; #重写到PHP处理 (here is just an example, everyone changes according to their own path) Try_files $uri @my_php; }    #原来由java处理, now change the request to PHP processing Location^~/java/urione{rewrite^/java/urione (. *) $/index.php?service=other.gettime$1  Break; #重写到PHP处理 (here is just an example, everyone changes according to their own path) Try_files $uri @my_php; }

#原来该java处理的, it's still handled by Java Location/{try_files $uri @my_java; } location @my_php {proxy_pass http:Www.php.domain: the;Proxy_set_header X-real-IP $remote _addr; Proxy_set_header X-forwarded-For $proxy _add_x_forwarded_for; } location @my_java {proxy_pass http:Www.java.domain: the; Proxy_set_header Host $host; Proxy_set_header X-real-IP $remote _addr; Proxy_set_header X-forwarded-For $proxy _add_x_forwarded_for; }}

Reverse proxy implements PHP and Java coexistence under the same domain name

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.