I. Proxy and reverse proxy

Real life examples

1. Forward proxy: Visit Google.com

! [](https://pic4.zhimg.com/80/v2-40c121cd04b6bf7c5e55cef441ad55b4_720w.png)

As shown above, since Google is blocked, we need vpnFQ to access Google.com. Virtual Private Network A Virtual Private Network (VPN) is used to establish a Private Network on a public Network for encrypted communication.

VPNS are perceptible to “us” (we connect to VPNS). VPNS are not perceptible to “Google servers” (Google only knows HTTP requests are coming in).

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

! [](https://pic1.zhimg.com/80/v2-d34bfedcbd748ee10160682981282b3b_720w.jpg)

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

! [](https://pic4.zhimg.com/80/v2-73e6e08341f226b812a762b97a7b91c5_720w.jpg)

2. Add the corresponding server

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

! [](https://pic4.zhimg.com/80/v2-7d0b73801bc04326ca51c6a04d986cbb_720w.jpg)

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 to monitor 127.0.0.1:9000 address | | www.example.com/index.php request arrives at 127.0.0.1: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:

! [](https://pic3.zhimg.com/80/v2-17f272cc74e4455a0093bb9b495b4f89_720w.jpg)

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 to monitor 127.0.0.1:9000 address | | www.example.com/index.php request arrives at 127.0.0.1:9000 | | PHP - FPM listening 127.0.0.1:9000 | | PHP - FPM receives the request, Enable the worker process request processing | | PHP - FPM 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

! [](https://pic2.zhimg.com/80/v2-47b9edacc5282c23551ca56917f86381_720w.jpg)

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

! [](https://picb.zhimg.com/80/v2-bfce31d736b8a3f71fef4e23605bfb11_720w.jpg)

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:

! [](https://picb.zhimg.com/80/v2-ac62b2639310c089eb99e00e97b5a4c3_720w.png)

3. Visit the website

! [](https://pic4.zhimg.com/80/v2-8f200a13e8d484c8367048b01f2297e1_720w.jpg)

I hope the above content can help you. Many PHPer will encounter some problems and bottlenecks when they are advanced, and they have no sense of direction when writing too many business codes. I have sorted out some information, including but not limited to: Distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, Laravel, YII2, Redis, Swoft, Kafka, Mysql optimization, shell scripting, Docker, microservices, Nginx, etc. Many knowledge points are free to share with you