One of the major principles of--nginx fastcgi operation principle

Source: Internet
Author: User
Tags file url

First,FastCGI1.Introduction

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.

2. configuration file Comment vim/application/nginx/conf/fastcgi.conf

Fastcgi_param script_filename $document The path of the _root$fastcgi_script_name;# script file request

Fastcgi_param query_string $query _string; # the requested parameters ; like app=123?

Fastcgi_param Request_method $request _method; # Requested action (get,post)

Fastcgi_param Content_Type $content _type; # content-type Field in the request header

Fastcgi_param content_length $content _length; # the Content-length field in the request header .

Fastcgi_param script_name $fastcgi _script_name; # Script Name

Fastcgi_param Request_uri $request _uri; # The requested address has no parameters

Fastcgi_param Document_uri $document _uri; # same as $uri.

Fastcgi_param document_root $document _root; # The root directory of the Web site. the value specified in the root directive in the server configuration

Fastcgi_param server_protocol $server _protocol; # the protocol used for the request, usually http/1.0 or http/1.1.

Fastcgi_param gateway_interface cgi/1.1; #cgi version

Fastcgi_param server_software nginx/$nginx _version; #nginx version number, can be modified, hidden

Fastcgi_param remote_addr $remote _addr; # Client IP

Fastcgi_param Remote_port $remote _port; # Client Port

Fastcgi_param server_addr $server _addr; # Server IP address

Fastcgi_param server_port $server _port; # Server Port

Fastcgi_param server_name $server _name; # server name, domain name specified in the Server configuration server_name

#fastcgi_param path_info $path _info;# customizable variables

# PHP only, required if PHP is Builtwith--enable-force-cgi-redirect

#fastcgi_param Redirect_status 200;

The above service environment variables can be printed in PHP

Example:echo$_server[' REMOTE_ADDR ')

3.FastCGISummary of key features:

1) is an interface or tool for communication between HTTP server and dynamic scripting language!

2) The advantage is to separate the dynamic language parsing and the HTTP server!

3) Nginx, Apache, lighttpd, and most dynamic languages support fastcgi.

4) interface mode using C/s structure, divided into HTTP server (client) and dynamic Language resolution server (service side)

5) PHP Dynamic Language server can start multiple fastcgi daemons such as: PHP-FPM (fcgi process mangemnt))

6) HTTP Server via example (Nginx Fastgi_pass) fastcgi client and dynamic language fastcgi service-side communication (for example: PHP-FPM)


Second, NginxOperating principle of fastcgi

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/77/B0/wKiom1Zrxh6zrjyeAAConWz_aOc661.png "title=" 7.png " alt= "Wkiom1zrxh6zrjyeaaconwz_aoc661.png"/>

Nginx does not support direct invocation or parsing of external dynamic programs, and all external programs (including PHP) must be called through the FastCGI interface. The FastCGI interface is a socket under Linux (the socket can be either a file socket or an IP socket). In order to invoke a CGI program, wrapper is also required to bind to a fixed socket, such as a port or a file socket. When Nginx sends the CGI request to the socket, through the fastcgi interface, Warpper anticipation to the request, and then sends out a new thread, which invokes the interpreter or external program to process the script and reads the return data, then Wrapper the returned data through the FastCGI interface, along the fixed socket to nginx; Finally, Nginx sends the returned data to the client, which is the whole process of nginx+fastcgi.

Nginx fastcgi Access PHP

1, the user sends the HTTP request message to the Nginx server

2, Nginx will be based on the file URL and suffix to determine the request

3, if the request is static content, Nginx will return the results directly to the user

4, if the request is dynamic content, Nginx will send the request to the FASTCGI client, through Fastcgi_pass send this request to PHP-FPM

5. PHP-FPM will give the request to wrapper

6. Wrapper receive the request will generate a new thread called PHP Dynamic program parsing server

7, if the user requested a blog post, or content, PHP will request the results of MySQL query

8, if the user requested a picture, attachment, PHP will request the NFS storage query results

9. PHP will give the results of the query to Nginx

10, Nginx will generate a response message returned to the user


Knowledge Supplement:

The two programs on the network realize the exchange of data through a two-way communication connection, one end of this connection is called a Socket .

Socketthe original meaning of English is"Hole"or"Socket". As aBsdunixof theProcess Communicationmechanism, take the latter one meaning. Often also known as" sockets ", used to describeIPThe address and port, which is a handle to a communication chain, can be used to implement communication between different virtual machines or different computers. In theInterneton theHostIt typically runs multiple service software, while providing several services. Each of these services opens aSocket, and bound to a port, different ports correspond to different services. Socketlike a porous socket, as its English original intended. A host is like a room full of sockets, each receptacle has a number, and some sockets provide -Volts Alternating Current, some provide theAC, while others offer cable TV programs. Customer software plug into different numbered sockets, you can get different services.

This article is from the "Dream Boy" blog, please be sure to keep this source http://runningyongboy.blog.51cto.com/8234857/1722299

One of the major principles of--nginx fastcgi operation principle

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.