How to implement Nginx+PHP+FastCGI acceleration mode
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>
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
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
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>
[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>
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

Steps to create a Docker image: Write a Dockerfile that contains the build instructions. Build the image in the terminal, using the docker build command. Tag the image and assign names and tags using the docker tag command.
