How to implement Nginx+PHP+FastCGI acceleration mode

WBOY
Release: 2023-05-14 16:34:12
forward
765 people have browsed it

1. User access process to dynamic PHP web pages

The user's browser initiates access to the web page: http://192.168.1.103/index.php

The user and the nginx server perform a three-way handshake for TCP connection (ignore access control policies including nginx access control policy, nginx firewall and other access control policies)

Step 1: The user sends the http request to the nginx server

Step 2: nginx will judge the request based on the URI and suffix visited by the user

1. For example, if the user accesses index.php, nginx will match it according to the location in the configuration file, for example:

[email protected]:/data/web# cat /etc/nginx/conf.d/blog.conf  server {     root /data/web/blog/;     index index.html index.htm;     server_name www.fwait.com;     location / {         try_files $uri $uri/ /index.html;     }     location /blog/ {         #alias /usr/share/doc/;         auth_basic "authorized users only";         auth_basic_user_file /etc/nginx/passwd.conf;         #autoindex on;         allow 192.168.1.103;         deny all;     }     location ~ \.php$ {         include /etc/nginx/fastcgi_params;         fastcgi_intercept_errors on;         fastcgi_pass 127.0.0.1:9000;     } }<br>
Copy after login

If the user accesses index.php, the location ~ \.php$ will be matched. This means that the resources accessed by the user through the URI are matched in a size-sensitive manner, and the accessed resources end with .php.

After nginx matches the specific location according to the resource requested by the user, it will execute the action corresponding to the location. The meaning of the action in the location is:

include /etc/nginx/fastcgi_params; #Indicates that nginx will call the fastcgi interface

fastcgi_intercept_errors on; #Indicates turning on fastcgi interrupt and error information recording

fastcgi_pass 127.0.0.1:9000; # Indicates that nginx sends the resources requested by the user to 127.0.0.1:9000 for analysis through fastcgi_pass. The nginx and php script parsing servers here are on the same machine, so 127.0.0.1:9000 represents It is a local php script parsing server.

According to the configuration of the nginx server, it can be seen that the user accesses dynamic PHP resources, and nginx will call PHP-related script parsing programs to parse the resources accessed by the user.

Step 3: As can be seen from the second step, the user requests dynamic content. nginx will hand over the request to the fastcgi client and send the user's request to php-fpm through fastcgi_pass

If the user accesses static resources, it is simple. nginx directly returns the static resources requested by the user to the user.

Step 4: After fastcgi_pass hands over the dynamic resources to php-fpm, php-fpm will transfer the resources to the wrapper of the php script parsing server

How to implement Nginx+PHP+FastCGI acceleration mode

How to implement Nginx+PHP+FastCGI acceleration mode

Step 5: After the wrapper receives the request transferred from php-fpm, the wrapper will generate a new thread to call the php dynamic program parsing server

If the user requests to read a MySQL database, for example, the database read operation will be triggered;

If the user requests images/attachments, etc., PHP will trigger a query to the back-end storage server such as a storage cluster stored through NFS;

Step 6: PHP will return the query results to nginx

Step 7: nginx constructs a response message and returns the result to the user

This is just one type of nginx. The user's request and the return of the user's request result are performed asynchronously, that is, the resource requested by the user is transferred in nginx. nginx can synchronize, that is, the parsed resource is returned directly by the server. For users, there is no need to make a transfer in nginx.

2. Related questions

1. Does every user request for dynamic resources need to trigger a complete dynamic resource parsing process?

No, there are two ways to solve this problem:

First, enable the caching function of nginx itself to cache the dynamic resource parsing results. The next time the user accesses the corresponding resource, nginx will perform this cache query. If the query is successful, the static resource after the dynamic resource is parsed will be returned directly. To the user;

Second, deploy a cache machine on the nginx backend, such as deploying a varnish cache cluster to cache resources. The resources requested by the user can be searched on the cache cluster first;

2. Is it feasible to use nginx for caching? Depending on the actual situation, if nginx is not a bottleneck in the entire web architecture, nginx can be used for caching, but this is not recommended because nginx is the only way for user requests and responses to user requests. If nginx appears Bottleneck, no matter how good the performance of other back-end components such as storage clusters is, it is not recommended to enable the caching function of nginx in actual deployment (when nginx is used as an http server). Because enabling the nginx cache function will reduce nginx performance and consume the hardware resources of the corresponding server where nginx is deployed.

3. If you use a picture to represent the relationship between nginx fastcgi wrapper php

How to implement Nginx+PHP+FastCGI acceleration mode

4.What exactly is fastcgi

CGI stands for Common Gateway Interface Commmon Gateway Interface

A tool used for program service communication on HTTP services. CGI programs must run on the network server.

The traditional CGI interface method has poor performance because every time the HTTP server encounters a dynamic program, it needs to restart the parser to perform parsing, and then the results are returned to the HTTP server. This is almost impossible when dealing with high concurrency, so FastCGI was born. In addition, the security of the traditional CGI interface method is also very poor

一个可伸缩地。高速地在HTTP服务器和动态脚本语言间通信的接口

接口在linux下是socket(这个socket可以是文件socket也可以是ip socket)

主要优点把动态语言和HTTP服务器分离开来。多数流行的HTTP服务器都支持FsatCGI包括Apache/Nginx/lighttpd等

支持语言比较流行的是PHP,接口方式采用C/S架构,可以将HTTP服务器和脚本解析器分开,同时在脚本解析服务器上启动一个或者多个脚本解析守护进程。

当HTTP服务器每次遇到动态程序时,可以将其直接交付给FastCGI进程来执行,然后将得到的结果返回给浏览器。这种方式可以让HTTP服务器专一地处理静态请求或者将动态脚本服务器的结果返回给客户端,这在很大程度上提高了整个应用系统的性能。

5.具体的nginx + php的nginx相关配置

[email protected]:/data/web# cat /etc/nginx/nginx.conf|egrep -v "#|^$" user www-data; worker_processes 4; pid /var/run/nginx.pid; events {     worker_connections 768; } http {     sendfile on;     tcp_nopush on;     tcp_nodelay on;     keepalive_timeout 65;     types_hash_max_size 2048;     include /etc/nginx/mime.types;     default_type application/octet-stream;     access_log /var/log/nginx/access.log;     error_log /var/log/nginx/error.log;     gzip on;     gzip_disable "msie6";     include /etc/nginx/conf.d/*.conf;     include /etc/nginx/sites-enabled/*; } [email protected]:/data/web#<br>
Copy after login
[email protected]:/data/web# cat /etc/nginx/conf.d/blog.conf server {     root /data/web/blog/;     index index.html index.htm;     server_name www.fwait.com;     location / {         try_files $uri $uri/ /index.html;     }     location /blog/ {         #alias /usr/share/doc/;         auth_basic "authorized users only";         auth_basic_user_file /etc/nginx/passwd.conf;         #autoindex on;         allow 192.168.1.103;         deny all;     }     location ~ \.php$ {         #include /usr/local/etc/nginx/fastcgi.conf;         include /etc/nginx/fastcgi_params;         fastcgi_intercept_errors on;         fastcgi_pass 127.0.0.1:9000;     } } [email protected]:/data/web#<br>
Copy after login

The above is the detailed content of How to implement Nginx+PHP+FastCGI acceleration mode. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!