I. Proxy and reverse proxy

Real life examples

1. Forward proxy: Visit Google.com

As shown above, because Google is blocked, we need to VPN over the wall to access Google.com.

VPNS are perceived by “us” (we connect to VPNS) and are not perceived by “Google servers” (Google only knows HTTP requests are coming). A forward proxy server is a server that can be sensed by humans but not by servers.

2. Reverse proxy: Implements load balancing through reverse proxy

As shown in the figure above, when we visited Baidu.com, Baidu has a proxy server through which load balancing can be done and routes to different servers.

This proxy server, for “us” is not perceptive (we can only perceive the access is Baidu’s server, do not know there is a proxy server to do load balancing). This proxy server, which is perceptive to “server1 server2 server3” (proxy server load balancing routes to different servers) is not perceptive to humans, but is perceptive to servers, we call it a reverse proxy server

conclusion

To put it bluntly: “forward” and “reverse” are relative to people’s perception. The agent that people can feel is the forward agent, and the agent that people can’t feel is the reverse agent.

Nginx and PHP-FPM

What is Nginx

Nginx (” Engine X “) is a high-performance HTTP and reverse proxy server as well as an IMAP/POP3/SMTP server.

What is Php – FPM

1. Cgi, fast-CGI protocol
The history of the cgi

Early WebServers only handled static files such as HTML, but as the technology evolved, dynamic languages such as PHP emerged. Webserver can’t handle it. What can I do? Leave that to the PHP interpreter! Leaving it to the PHP interpreter is fine, but how does the PHP interpreter communicate with the WebServer? In order to solve the communication between different language interpreters (such as PHP, Python interpreters) and webserver, so the CGI protocol appeared. As long as you write programs according to CGI protocol, you can realize the communication between the language interpreter and Webwerver. Such as php-CGI programs.

Fast – improvement of cgi

The CGI protocol solves the problem of PHP interpreter communicating with webServer, and webServer can finally handle dynamic languages. However, every time a Webserver receives a request, it forks a CGI process, kills the process at the end of the request. For 10,000 requests, fork and kill the php-CGI process 10,000 times.

Did you find it a waste of resources?

Thus, a modified version of CGI, fast-CGI, emerged. Fast-cgi does not kill the process after each request is processed, but preserves the process so that it can handle multiple requests at once. This saves you from having to fork the process again each time, greatly increasing efficiency.

2. What is phP-FPM

Php-fpm is an implementation of Fastcgi and provides Process management functionality. Processes include master process and worker process. There is only one master process, which is responsible for listening on the port and receiving requests from the Web Server, while there are generally multiple worker processes (the specific number can be configured according to actual needs). Each process has a PHP interpreter embedded inside, which is where THE PHP code is actually executed.

How to combine Nginx with PHP-FPM

As mentioned above, Nginx can not only handle HTTP requests, but also act as a reverse proxy. Nginx uses reverse proxy functionality to redirect dynamic requests to back-end PHP-FPM.

Let’s configure a new Nginx+ php-fpm

1. Configure the nginx.conf file

Go to the nginx directory and edit the nginx.conf file. As shown, add the include file on the last line of nginx.conf

Image_1b08eroasu1dr0kk0m12pg1572d. PNG – 84.6 kB

2. Add the corresponding server

Go to the path of the above include and add a server.

image_1b08f7sqm1ub71es9jrr1v3h1emp2q.png-119kB

Here we explain the meaning of configuration items:

server { listen 80; Server_name www.example.com; # is website address root/usr/local/etc/nginx/WWW/huxintong_admin; Location / {index index. PHP; # autoindex on jump to www.example.com/index.php; } # when request site under PHP file, reverse proxy to PHP - FPM location ~ \. ${PHP include/usr/local/etc/nginx/fastcgi. Conf. Nginx fastCgi_intercept_errors on; Fastcgi_pass 127.0.0.1:9000; #nginx fastcgi process listener IP address and port}}Copy the code

To sum up: when we visit www.example.com, the process looks like this:

Routing to www.example.com/index.php www.example.com | | Nginx | | | | loading Nginx fast - cgi module | | fast - cgi listening in127.0. 01.:9000Address | | www.example.com/index.php request arrives127.0. 01.:9000| | waiting to be processed...Copy the code

Let’s enable PHP’s php-fpm to handle this request

Open the php-fpm.conf file and see the following configuration:

image_1b08gcs3g1msg9mf1ie7rks1b7o37.png-96kB

That is, the phP-fPM module listens for port 127.0.0.1:9000 and waits for requests to be processed.

Four,

Combining Nginx with PHP-FPM, the complete flow looks like this.

Routing to www.example.com/index.php www.example.com | | Nginx | | | | loading Nginx fast - cgi module | | fast - cgi listening in127.0. 01.:9000Address | | www.example.com/index.php request arrives127.0. 01.:9000| | PHP - FPM listening in127.0. 01.:9000| | PHP - FPM receives the request, to enable the worker process request processing | | - FPM PHP processes the request, returned to the nginx | | nginx results through HTTP return to the browserCopy the code

5. Effect display

1. Start nginx and phP-Fpm module

Starting successfully, let’s look at the php-fpm process

As shown above, there is one master process and three worker processes.

2, in the website directory to create a file

We edit the file as shown below:

3. Visit the website

More wonderful, please pay attention to the public number “chat code”, let us talk about the “left hand code right hand poem” thing.