Principle and relationship of nginx-->-----Principle-->nginx+php+fastcgi

Source: Internet
Author: User
Tags fpm php script nginx server

First, the user on the dynamic PHP Web Access process

User browser initiates access to a webpage: http://192.168.1.103/index.php

User and Nginx server three handshake for TCP connection (ignoring including Nginx access control policy, Nginx Firewall and other access control policies)

First step: the user sends an HTTP request to the Nginx server

The second step:Nginx will judge the request according to the URI and suffix that the user accesses.

1. For example, a user-accessed Index.php,nginx is matched against a 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; allow192.168.1.103;    Deny all; } Location~\.php$ {include/etc/nginx/Fastcgi_params;        Fastcgi_intercept_errors on; Fastcgi_pass127.0.0.1:9000; }}

The user accesses the index.php, which is matched to location ~ \.php$, which means matching the size of the resource that the user accesses through the URI, and the resource accessed ends in. php.

Nginx according to the user requested resources match to the specific location, will perform location corresponding action, location in the meaning of the action is:

include/etc/nginx/fastcgi_params; #表示nginx会调用fastcgi这个接口

fastcgi_intercept_errors on; #表示开启fastcgi的中断和错误信息记录

Fastcgi_pass 127.0. 0.1:9000; # indicates that Nginx sends a user-requested resource to 127.0 through Fastcgi_pass. 0.1:9000 to parse, here Nginx and PHP script parsing server is on the same machine, so 127.0. 0.1:9000 represents the local PHP script parsing server.

According to the configuration of Nginx server, we can see that the user accesses the dynamic PHP resources, and Nginx invokes the PHP-related script resolver to parse the resources accessed by the user.

The third step: through the second step, we can see that the user is requesting dynamic content, Nginx will send the request to the FASTCGI client, through Fastcgi_pass the user's request sent to PHP-FPM

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

Fourth step:after fastcgi_pass the dynamic resources to PHP-FPM, PHP-FPM will transfer the resources to the PHP script parsing server Wrapper

Fifth Step:wrapper receives PHP-FPM turn over request, wrapper will generate a new thread to call the PHP dynamic program parsing server

If the user requests the need to read such as MySQL database, etc., will trigger the read library operation;

If the user requests a slice/accessory, PHP will trigger a query to the backend storage server such as storage cluster via NFS;

Sixth step:PHP will return the results of the query to Nginx

Seventh Step:Nginx Constructs a response message to return the result to the user

This is only one of Nginx, the user request and return user request results are asynchronous, that is, the resources requested by the user in Nginx to do a relay, Nginx can be synchronized, that is, to resolve the resources, the server directly return the resources to the user, do not have to do a transfer in Nginx.

Second, related questions

1. Is it necessary to trigger a complete dynamic resource parsing process every time a user requests a dynamic resource?

No, there are two ways to solve this problem:

First, to enable the Nginx itself has the caching function, the dynamic resource parsing results cache, the next time the user to access the corresponding resources, Nginx cache query, if the query is successful, the direct dynamic resources are parsed static resources returned to the user;

Second, in the Nginx backend deployment cache machine, such as the deployment of varnish cache cluster, the resources are cached, the user requested resources, you can first on the cache cluster to find;

2. Is it possible to cache with Nginx? Look at the actual situation, if the entire Web architecture, Nginx is not a bottleneck, nginx can be used to do the cache, but it is not recommended to do so, because Nginx is the user request and answer user requests, if there is a bottleneck, the backend of other such as storage cluster performance is no good , it is not recommended to enable Nginx caching in real-world deployments (in the case of Nginx as an HTTP server). Because the Nginx caching feature is enabled, the first is to reduce nginx performance, and the second is to consume the hardware resources of the corresponding server that deploys nginx.

3. If you use a graph to indicate the relationship between Nginx fastcgi wrapper PHP

4.fastcgi What exactly is a thing?

CGI Full name Universal Gateway Interface Commmon Gateway Interface

A tool that is used to communicate with the Program service on the HTTP service, and the CGI program must be running on a network server.

Traditional CGI interface performance is poor, because each time the HTTP server encounters a dynamic program that needs to restart the parser to perform parsing, then the result is returned to the HTTP server. This is almost impossible when dealing with high concurrency, so fastcgi was born. In addition, the traditional CGI interface method is also very poor security

One can be scaled. Interface for high-speed communication between HTTP servers and dynamic scripting languages

Interface under Linux is the socket (this socket can be a file socket or an IP socket)

The main advantage is separating the dynamic language from the HTTP server. Most popular HTTP servers support fsatcgi including APACHE/NGINX/LIGHTTPD, etc.

Support language is more popular is PHP, interface mode with the C/s architecture, you can separate the HTTP server and script parser, at the same time on the script resolution server to start one or more script parsing daemon.

Each time the HTTP server encounters a dynamic program, it can be delivered directly to the fastcgi process to execute, and the resulting results are returned to the browser. This approach allows the HTTP server to handle static requests exclusively or return the results of the dynamic script server to the client, which greatly improves the performance of the entire application system.

5. Specific Nginx + PHP nginx related configuration

[Email protected]:/data/web#Cat/etc/nginx/nginx.conf|Egrep-V"#|^$"User www-data;worker_processes4;p ID/var/run/nginx.pid;events {worker_connections768;}    HTTP {sendfile on;    Tcp_nopush on;    Tcp_nodelay on; Keepalive_timeout $; Types_hash_max_size2048; Include/etc/nginx/mime.types; Default_type Application/octet-stream; Access_log/var/log/nginx/Access.log; Error_log/var/log/nginx/Error.log; gzipOn ; Gzip_disable"Msie6"; Include/etc/nginx/conf.d/*. conf; include/etc/nginx/sites-enabled/*;} [Email protected]:/data/web#

[Email protected]:/data/web#Cat/etc/nginx/conf.d/Blog.confserver {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; allow192.168.1.103;    Deny all; } Location~\.php$ {#include/usr/local/etc/nginx/fastcgi.conf; Include/etc/nginx/Fastcgi_params;        Fastcgi_intercept_errors on; Fastcgi_pass127.0.0.1:9000; }}[email protected]:/data/web#

Reference Links:

http://runningyongboy.blog.51cto.com/8234857/1722299

Principle and relationship of nginx-->-----Principle-->nginx+php+fastcgi

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.