Nginx + cgi/fastcgi + c/cpp

Source: Internet
Author: User
Tags unix domain socket drupal automake



Http://www.cnblogs.com/skynet/p/4173450.htmlNginx + cgi/fastcgi + c/cpp



2014-12-19 11:05 by Wu Qin, 19794 reading, 6 reviews, Favorites, compilation






Then on the "Nginx Installation and use", this article introduces the principle of cgi/fastcgi, and how to write a simple cgi/fastcgi in C + +, and finally deploy cgi/fastcgi to Nginx. The outline of the content is as follows:






1. CGI






1.1. Environment variables






1.2. Standard input






2. FastCGI






3. Nginx cgi/fastcgi






3.1. Nginx + fastcgi






3.1.1. spawn-fcgi






3.1.2. Writing fastcgi applications






3.1.3. Nginx fastcgi Configuration






3.2. Nginx + CGI






3.2.1 Fastcgi-wrapper






3.2.2. Nginx fcgiwrap Configuration






3.2.3. Writing CGI applications






Reference links










1.CGI



Universal Gateway Interface (Common Gateway Interface/CGI) describes a standard for transferring data between a client and a server program , you can have a client request data from a Web browser to a program that executes on a network server. CGI is independent of any language, and CGI programs can be implemented in any scripting language or fully independent programming language, as long as the language can run on the system. The Unix shell script, Python, Ruby, PHP, Perl, TCL, C + +, and Visual Basic can all be used to write CGI programs. (Http://www.dwz.cn/yFFgQ)






Initially, CGI was developed in 1993 by the National Supercomputer Application Center (NCSA) for the NCSA HTTPd Web server. The Web server uses the UNIX shell environment variable to hold parameters passed from the Web server and then generates a separate process to run the CGI. The CGI processing flow is as follows:












L Step1. The Web server receives a request from the client (browser), initiates a CGI program, and passes the data through environment variables , standard inputs






L STEP2. The CGI process starts the parser, loads the configuration (such as a business-related configuration), connects other servers (such as a database server), and logically processes






L STEP3. The CGI thread passes the processing results to the Web server via standard output , standard error






L STEP4. The Web server receives the result of the CGI return, constructs an HTTP response to return to the client, and kills the CGI process






Web servers and CGI pass data to each other through environment variables, standard inputs, standard outputs, and standard errors.




1.1. Environment variables



Get request, which places the data in the environment variable query_string, and the CGI gets the data from the environment variable query_string. Common environment variables are shown in the following table:




Environmental variables


Content


Auth_type


Access authentication type.


Content_length


The length of the data passed by the standard input to the CGI program, calculated as bytes or the number of characters.


Content_Type


The MIME type of the request.


Gateway_interface


The CGI version number of the server.


Http_accept


The content-types that the browser can receive directly, can have the HTTP Accept header definition.


Http_user_agent


The name, version, and other platform-based additional information for the browser that submitted the form.


Http_referer


Submit the form to the URL of the text, not all browsers send this message, do not rely on it


Path_info


The path information passed to the CGI program.


Query_string


The request parameter passed to the CGI program, which is the "?" The string that is appended to the URL.


Remote_addr


The host name of the client side.


Remote_host


The IP address of the client side.


Remote_user


The user name sent out by the client side.


Remote_method


The client side makes the requested method (such as Get, post).


Script_name


The virtual path where the CGI program resides, such as/cgi-bin/echo.


server_name


The host name or IP address of the server.


Server_port


The server port on which the request was received.


Server_protocol


The communication contract and version number used.


Server_software


The name and version of the server program.


1.2. Standard input

The size of the environment variable is limited, when the amount of data to be transmitted is large, the space of storage environment variables may be insufficient, resulting in incomplete data reception, and even unable to execute CGI program. So then another approach was developed: POST, which is the technique of using I/O re-orientation, so that CGI programs can be communicated directly to the browser by stdin and stdout.


When we specify that the requested data is passed in this way, the Web server receives the data in a block of input buffers, records the size of the data in the CONTENT_LENGTH environment variable, and then calls the CGI program and points the stdin of the CGI application to the buffer. So we can smoothly through the stdin and environmental variables Content_length get all the information, no data size limits.


Summary: CGI makes it possible for external programs to interact with Web servers. The CGI program runs in a separate process and establishes a process for each Web request, which is very easy to implement, but is inefficient and difficult to scale. Faced with a large number of requests, the large number of processes and the extinction of the operating system performance greatly reduced. In addition, resource reuse is restricted because the address space cannot be shared.




2.FastCGI



Fast Universal Gateway Interfaces (Fast Common Gateway interface/fastcgi) are a common Gateway Interface (CGI) improvement that describes a standard for transferring data between client and server programs. Fastcgi is committed to reducing the overhead of interaction between Web servers and CGI programs , thus enabling Server can handle more web at the same time request . Unlike creating a new process for each request, FASTCGI uses a continuous process to process a series of requests. These processes are managed by the FASTCGI process Manager, not the Web server. (HTTP://WWW.DWZ.CN/YFMAP)












When a request comes in, the WEB server passes the environment variable and the page request through a UNIX domain socket (both located on the same physical server) or an IP socket (fastcgi deployed on another physical server) to the fastcgi process.












L Step1. The WEB server starts loading the initialization fastcgi execution environment. such as IIS ISAPI, Apache mod_fastcgi, Nginx ngx_http_fastcgi_module, lighttpd mod_fastcgi






L STEP2. The FASTCGI process Manager itself initializes, initiates multiple CGI interpreter processes, and waits for a connection from the Web server. When you start the fastcgi process, you can configure socket two to start in both IP and UNIX domains.






L STEP3. When a client request arrives at the Web server, the Web server forwards the request to the FASTCGI master process with the socket, fastcgi the main process to select and connect to a CGI interpreter. The WEB server sends CGI environment variables and standard input to the FASTCGI child process.






L STEP4. The FASTCGI child process returns standard output and error information from the same socket connection to the Web server after processing is complete. When the fastcgi child process closes the connection, the request is processed to completion.






L STEP5. The fastcgi child process then waits and processes the next connection from the Web server.






Because the FastCGI program does not need to constantly generate new processes, it can greatly reduce the pressure on the server and produce high application efficiency. It has a minimum speed of 5 times times higher than CGI technology. It also supports distributed deployments where the FASTCGI program can execute on a host other than the Web server.






Summary: CGI is the so-called short-lived application, FastCGI is the so-called long-lived application. FastCGI is like a resident (long-live) CGI, which can be executed all the time, not each time to fork it (this is the most notorious fork-and-execute mode of CGI).




3.nginx cgi/fastcgi



Nginx can not directly execute the external executable program as Apache does, but Nginx acts as a proxy server and forwards the request to the backend server, which is one of the main functions of Nginx. The Nginx supports the fastcgi agent, receives the client's request, and forwards the request to the backend fastcgi process. Here's how to write a cgi/fastcgi in C + + and deploy it to nginx.




3.1. Nginx + fastcgi



As previously described, the fastcgi process is managed by the FASTCGI process Manager, not nginx. This requires a fastcgi management, management we write fastcgi program. This article uses spawn-fcgi as the FASTCGI process Manager.




3.1.1. spawn-fcgi



SPAWN-FCGI is a general-purpose FASTCGI process manager, simple and small, originally part of the LIGHTTPD, and later because of the use of a wide range, so it was migrated out as a standalone project. SPAWN-FCGI uses the Pre-fork model, the main function is to open the listening port, bind the address, and then fork-and-exec create the FASTCGI application process we wrote, exit to complete the work. The fastcgi application initializes and then enters the connection request to the dead loop listening socket.






Install spawn-fcgi:






Get spawn-fcgi Compile the installation package and get the latest version available on Http://redmine.lighttpd.net/projects/spawn-fcgi/wiki.






L UNZIP the spawn-fcgi-x.x.x.tar.gz package.






L go to unzip directory, execute./configure.






L Make & make install






If you encounter the following error: "./autogen.sh:x: Autoreconf:not found" because there is no Automake tool installed, Ubuntu installs it with the following command: sudo apt-get install autoconf Automake Libtool.






spawn-fcgi Help information can be obtained by man spawn-fcgi or SPAWN-FCGI–H, the following are some common spawn-fcgi parameter information:




-F <fcgiapp> Specifies the execution program location of the process calling FastCGI


-A <addr> bind to address addr.


-P <port> bind to ports port.


-S <path> bind to UNIX domain sockets


-C <childs> Specifies the number of processes generated by the fastcgi, which defaults to 5. (PHP only)


-P <path> Specifies the path of the PID file for the resulting process.


-F <childs> Specifies the number of processes generated by the fastcgi (CGI for C with this)


What identity is used by-u and-G fastcgi (-u user-G user group), the Apache user can be used under CentOS, and other configurations such as nobody, Www-data and so on.


3.1.2. Writing fastcgi applications



You can use the FASTCGI SDK or other development frameworks, such as fastcgi++, to write fastcgi applications in C/D + +.






This article uses the FASTCGI Software Development Kit--FCGI (HTTP://WWW.FASTCGI.COM/DRUPAL/NODE/6?Q=NODE/21), which makes it easy to write fastcgi applications and install fcgi:






Get fcgi Compile the installation package and get the latest version available on HTTP://WWW.FASTCGI.COM/DRUPAL/NODE/5.






L UNZIP the fcgi-x.x.x.tar.gz package.






L go to unzip directory, execute./configure.






L Make & make install






If the compilation prompts for an error:






Fcgio.cpp:In destructor ' virtual fcgi_streambuf::~fcgi_streambuf () ':




Fcgio.cpp:50:error: ' EOF ' is not declared in this scope

Fcgio.cpp:In member function ' virtual int fcgi_streambuf::overflow (int) ':

Fcgio.cpp:70:error: ' EOF ' is not declared in this scope

Fcgio.cpp:75:error: ' EOF ' is not declared in this scope

Fcgio.cpp:In member function ' virtual int fcgi_streambuf::sync () ':

Fcgio.cpp:86:error: ' EOF ' is not declared in this scope

Fcgio.cpp:87:error: ' EOF ' is not declared in this scope

Fcgio.cpp:In member function ' virtual int fcgi_streambuf::underflow () ':

Fcgio.cpp:113:error: ' EOF ' is not declared in this scope

MAKE[2]: * * * [Fcgio.lo] Error 1

MAKE[2]: Leaving directory '/root/downloads/fcgi-2.4.1-snap-0910052249/libfcgi '

MAKE[1]: * * * [all-recursive] Error 1

MAKE[1]: Leaving directory '/root/downloads/fcgi-2.4.1-snap-0910052249 '




Make: * * * [ALL] Error 2






Workaround: Add #include <cstdio> in the/include/fcgio.h file, and then compile the installation to pass.






If you are prompted not to find the dynamic library, add the fcgi installation path, such as/usr/local/lib, in Ld_library_path or/etc/ld.so.conf and perform a ldconfig update.






#include "fcgi_stdio.h"






#include <stdlib.h>












int main (void)






{






int count = 0;






while (fcgi_accept () >= 0)






printf ("content-type:text/html\r\n"






"\ r \ n"






"<title>fastcgi hello!</title>"






"




"Request number%d running on host <i>%s</i>\n",






++count, getenv ("SERVER_NAME"));






return 0;




}





Compile the g++ main.cpp-o demo–lfcgi and deploy the demo to the/opt/nginx-1.7.7/cgi-bin/directory






FastCGI program written by spawn-fcgi C + +:/opt/nginx-1.7.7/sbin/spawn-fcgi-a 127.0.0.1-p 8081-f/opt/nginx-1.7.7/cgi-bin/ Demo




3.1.3. Nginx fastcgi Configuration



About Nginx configuration file analysis, you can refer to "Nginx Installation and use" http://www.cnblogs.com/skynet/p/4146083.html, in the previous nginx.conf based on the addition of the following fastcgi configuration.












When the Nginx receives the HTTP://LOCALHOST/DEMO.CGI request, it matches to the location =/demo.cgi block and passes the request to the backend's fastcgi application for processing. As shown below: (Note that number is 80 because I have requested 80 times)










3.2. Nginx + CGI



Nginx can not directly execute external executable program, and CGI is to receive the request to start the CGI process, not like fastcgi will start on the first, so nginx is inherently unsupported CGI. Nginx does not support CGI, but it supports fastcgi. So, we can consider using FASTCGI packaging to support CGI. The principle is as follows: Pre-fork Several common agent fastcgi programs--fastcgi-wrapper,fastcgi-wrapper start executing the CGI and return the execution result of the CGI to Nginx (fork-and-exec).












Once you understand the principle, it's easier to write a fastcgi-warpper. One of the more popular solutions on the web is the fastcgi wrapper script cgiwrap-fcgi.pl from the Nginx wiki (Http://wiki.nginx.org/SimpleCGI) that uses Perl. But I am not very cold to Perl, here is a C/C + + written fastcgi-wrapper.




3.2.1. Fastcgi-wrapper



In fact, the fastcgi-wrapper of C + + is to write a C + + fastcgi, the steps and principles are the same as in the previous section (NGINX+FASTCGI). There are already some open source on GitHub, and C writes the Fastcgi-wrapper:https://github.com/gnosek/fcgiwrap.






Install Fcgiwrap:






L Download (https://github.com/gnosek/fcgiwrap.git)






L UNZIP the Fcgiwrap and enter the unzip directory






L Autoreconf-i






L./configure






L Make && make install






Start Fastcgi-wrapper:/opt/nginx-1.7.7/sbin/spawn-fcgi-f/usr/local/sbin/fcgiwrap-p 8081




3.2.2. Nginx fcgiwrap Configuration



Add the following loaction configuration block in nginx.conf so that all xxx.cgi requests go to Fcgiwrap, and Fcgiwrap will execute the CGI program in the Cgi-bin directory.
















3.2.3. Writing CGI applications









#include <stdio.h>






#include <stdlib.h>












int main (void)






{






int count = 0;






printf ("content-type:text/html\r\n"






"\ r \ n"






"<title>cgi hello!</title>"












"




"Request number%d running on host <i>%s</i>\n",






++count, getenv ("SERVER_NAME"));






return 0;


}


[Email protected]:~/clionprojects/hellofastcgi$ g++ cgi.cpp-o cgidemo-lfcgi






[Email protected]:~/clionprojects/hellofastcgi$ sudo cp cgidemo/opt/nginx-1.7.7/cgi-bin/


















Note that the number of requests in the graph has always been 1, because the CGI pattern is fork-and-exec, and each time it is a new process.
























Nginx + cgi/fastcgi + c/cpp




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.