This article introduces the relationship between PHP-FPM,Nginx and FastCGI, as well as the configuration of Nginx reverse proxy and load balancing.
PHP-FPM,Nginx,FastCGI
FastCGI is a protocol that acts as a bridge between applications and WEB servers. Instead of communicating directly with phP-FPM, Nginx hands requests to php-FPM through FastCGI.
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}Copy the code
In this case, fastcgi_pass forwards all PHP requests to phP-FPM for processing. Using the netstat command, you can see that the process running on port 127.0.0.1:9000 is php-fpm.
Nginx reverse proxy
The most important Nginx reverse proxy directive is proxy_pass, as in:
location ^~ /seckill_query/ {
proxy_pass http://ris.filemail.gdrive:8090/;
proxy_set_header Host ris.filemail.gdrive;
}
location ^~ /push_message/ {
proxy_pass http://channel.filemail.gdrive:8090/;
proxy_set_header Host channel.filemail.gdrive;
}
location ^~ /data/ {
proxy_pass http://ds.filemail.gdrive:8087/;
proxy_set_header Host ds.filemail.gdrive;
}Copy the code
The url path is matched by location and forwarded to another server for processing.
Reverse proxies can also be implemented with load-balancing upstreams.
Nginx load balancing
Upstream:
The load balancing module is used to select a host from the list of back-end hosts defined by the “upstream” directive. Nginx uses the load balancing module to find a host and then uses the upstream module to interact with that host.
Load balancing configuration:
upstream php-upstream {
ip_hash;
server 192.168.0.1;
server 192.168.0.2;
}
location / {
root html;
index index.html index.htm;
proxy_pass http://php-upstream;
}Copy the code
This example defines a PHP-upstream load balancing configuration that is applied via the proxy_pass reverse proxy directive. The IP_hash algorithm used here is not a list of load balancing algorithms.
Load balancing can also be used on FastCgi_pass.
Such as:
fastcgi_pass http://php-upstreamCopy the code
The problem
What is the relationship between reverse proxy and load balancing
The terms reverse proxy and load balancing are often used together, but they are actually different concepts. Load balancing is more about an algorithm or strategy that distributes requests to different machines, and therefore effectively acts as a reverse proxy.
Proxy_pass vs. fastcgi_pass
One is the reverse proxy module and the other is forwarded to the FacTCGI backend processing.
The original address: blog. Tanteng. Me / 2017/11 / ngi…