PHP using nginx How to implement reverse proxy

Source: Internet
Author: User
Tags nginx reverse proxy
This article is mainly for you to introduce the PHP using Nginx to implement reverse proxy method, with a certain reference value, interested in small partners can refer to

One, Proxy server

1. What is a proxy server

Proxy server, the client sends the request, not directly to the destination host, but first sent to the proxy server, the proxy service to accept the client request, then issued to the host, and receive the destination host returned data, stored in the proxy server's hard disk, and then sent to the client.


2. Why to use proxy server

1) Increased access speed

Since the data returned by the target host resides on the hard disk of the proxy server, the next time the customer accesses the same site data, it is read directly from the hard disk of the proxy server, which plays a role in caching, especially for popular sites that can significantly increase the request speed.

2) Firewall function

Because all client requests must have access to the remote site through a proxy server, you can set limits on the proxy server to filter for some unsafe information.

3) access to target sites that are inaccessible through a proxy server

There are many proxy servers developed on the Internet, the client can access the target site through unrestricted proxy server when access is limited, in layman's terms, we use the flip-wall browser is the use of Proxy server, although can not go abroad, but also direct access to the external network.

Second, reverse proxy VS forward Proxy

1, what is the forward proxy? What is a reverse proxy?

The forward proxy, which is set up between the client and the target host, is used only to proxy the Internal network connection request to the Internet, the client must specify a proxy server, and the HTTP request that would otherwise be sent directly to the Web server is sent to the proxy server.


The reverse proxy server is set up on the server side to alleviate the workload of the server by buffering the frequently requested pages, forwarding the client requests to the target servers on the internal network, and returning the results from the server to the clients requesting connections on the Internet. At this point, the proxy server and the target host appear as a server externally.


2. What are the main applications of reverse proxy?

Many large web sites now use reverse proxies. In addition to preventing malicious attacks on the extranet servers, caching to reduce server pressure and access security controls, load balancing can be done to assign user requests to multiple servers.

Third, the direction proxy server Nginx

Nginx as a fire in recent years, the reverse proxy server, installed in the destination host side, mainly for forwarding client requests, the background has a number of HTTP servers to provide services, Nginx function is to forward the request to the back of the server, decide which target host to process the current request. The following shows how to configure the Nginx to work.

1. Simulate n HTTP server as target host

As a test, simply simulate two HTTP servers with 2 Tomcat instances and change the Tomcat port to 8081 and 8082, respectively

2. Configure IP domain name

192.168.72.49 8081.max.com

192.168.72.49 8082.max.com

3, Configuration nginx.conf


Upstream Tomcatserver1 {   server 192.168.72.49:8081;    } upstream Tomcatserver2 {    server 192.168.72.49:8082;   }  server {         listen    ;          server_name 8081.max.com;          #charset Koi8-r;          #access_log Logs/host.access.log main;          Location/{            proxy_pass  http://tomcatserver1;            Index index.html index.htm;      }      } server {           listen    ;          server_name 8082.max.com;            #charset Koi8-r;            #access_log Logs/host.access.log main;           Location/{            proxy_pass  http://tomcatserver2;            Index index.html index.htm;       }        }

Process:

1) Browser access 8081.max.com, through the local host file name resolution, locate the 192.168.72.49 server (install nginx)

2) The Nginx reverse proxy accepts the client request, locates the server node server_name to 8081.max.com, and forwards the request to upstream Tomcatserver1 according to the proxy_pass corresponding HTTP path. A tomcat server with a port number of 8081.

4. Effect display

Request 8081.MAX.COM,TOMCAT1 receive back home

Request 8082.MAX.COM,TOMCAT2 receive back home


Iv. Summary

Through analysis we are not difficult to conclude, take Baidu as an example, if the client's IP and Baidu server (target host) IP in the same network segment, that is the same LAN internal send requests, the speed is very fast.

But if not meet this demand also think to achieve a better request response, Baidu Server can provide a target server in a network segment of the public IP, that is, the reverse proxy service IP, through the proxy server forwarding client requests, decide behind the scenes of N server who to handle this request, And because the reverse proxy server and the target host in a network segment, the access speed will be fast.

Nginx as a reverse proxy server, it is one of the many reverse proxy server, through a simple configuration, specified to the server IP or domain name address can be forwarded to the client request to the specified server processing requests.

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.