PHP Nginx Reverse Proxy

Source: Internet
Author: User

  

I. Conceptual Understanding1. 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 a proxy server① Improve 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.

② 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.

③ access to target sites that cannot be accessed 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 Fq browser to use the proxy server, although we can not go abroad, but also direct access to the external network.

3. Forward Proxy & Reverse proxy① forward 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.

② Reverse Proxy

The reverse proxy server is set up on the server side, by buffering the frequently requested pages to alleviate the workload of the server, forwarding the client request to the target server on the internal network, and returning the results from the server to the client requesting connections on the Internet, where the proxy server and the target host External performance as a server.

4. Application 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.

5. Why use Nginx

Nginx is a lightweight, high-performance Web server that can be used directly as an HTTP server instead of Apache, requires fastcgi processor support for PHP, and can be used as a reverse proxy server for load balancing. Although Apache's mod_proxy and Mod_cache can also implement reverse proxy and load balancing for multiple app servers, Nginx has great advantages in handling concurrency.

Ii. Preparatory work1. Server

Because we want to use the Nginx reverse proxy server to achieve a load balancing effect, so we need to build at least three servers to simulate this effect. I used VMware to build three virtual machines, the system is centos6.5.

2. Environment

Because a server is required to act as a reverse proxy server, and I am phper, the reverse proxy server is linux+nginx+mysql+php. The remaining two real servers are linux+apache+mysql+php, because Apache performance is more stable, more modules and fewer bugs than nginx.

3. Build the Environment

This thing to say too much, here is not described, I have two articles written how to build these two environments, links:

LAMP http://www.cnblogs.com/lina520/p/7240680.html

Lnmp http://www.cnblogs.com/lina520/p/7240328.html

What questions can I leave a message, if I will try to solve.

third, server configuration  

This is after I fixed the virtual machine IP and then used the SecureCRT tool link. 35 This is a reverse proxy server, 36,37 is two real servers.

1. Writing Test Files

#CD var/www/html; # Here is the default Web directory, according to your actual situation to write an accessible php file

#VI index.php#这里可以随便写点东西, but to differentiate from another server

  

Another server is the same

  

2. Configuring the Reverse proxy Server

  #CD Etc/nginx#跳转到nginx的配置页面

#VI nginx.conf Modify configuration file

  

  We add the following configuration under the HTTP module, mainly a upstream and server   

Upstream Blance {
Server 192.168.0.36:80; A server
Server 192.168.0.37:80; b Server
}

  

server{
Listen 80;//The port number of the real server
server_name blance;//is the name of our access to the reverse proxy server domain name, note the above server cluster name consistent
Location /{
Proxy_pass http://blance;//the same as above
proxy_set_header Host $host;//header of forwarded request
proxy_set_header x-real-ip $remote _addr;
proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
}
}

3. Configure the access domain name

Because it is a test, there is no virtual domain name, but you can make one yourself. My physical machine is a Windows system, open C:\Windows\System32\drivers\etc and then edit the Hosts file to add the domain name you want to request at the end of the file

  

  After saving, exit open browser to access this domain name, the result is as follows:

  

  

Four, load balancing strategy 1.RR strategy

   Is what we usually call the polling policy, each request in chronological order to a different back-end server, if the backend server down, can be automatically rejected.

  

2. Weighted strategy

   Specifies the polling probability, proportional to the weight and access ratios, for situations where the performance of the backend server is uneven.

  As long as you modify the upstream, the server will follow the weight to access. As the above configuration, the probability of a server access is 2/3,b is 1/3, then access a two times, Access B once.

3.ip_hash

  Each request is allocated according to the hash result of the access IP, so that each visitor has fixed access to a back-end server that resolves the session issue.

  

  Using the Ip_hash policy will be fixed for each IP access to a back-end server, will not go to the other servers, the session is maintained, you can solve the session problem

The above only wrote a few commonly used things, there are many I do not know, did not write to welcome everyone to communicate!

  

  

  

PHP Nginx Reverse Proxy

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.