Nginx + FastCGI Program (c + +) to build a high-performance Web service Demo

Source: Internet
Author: User

http://blog.csdn.net/chdhust/article/details/42645313 Nginx + FastCGI Program (c + +) to build a high-performance Web service demo 1. IntroductionNginx-high-performance Web server, this needless to say, we all know.    FASTCGI Program-resident CGI program, which is a language-independent, extensible architecture of CGI open-extension, whose main behavior is to keep the CGI interpreter process in memory and thus achieve high performance. Nginx to invoke the FASTCGI program, you need to use the FASTCGI process Manager (because Nginx can not directly execute the external CGI program, we can use the spawn-fastcgi in lighttpd to allow Nginx to support external CGI operation. There are other ways to install nginx-fcgi to let Nginx support CGI, here is the method of using spawn-fastcgi, to achieve the purpose of invoking the FASTCGI program. Nginx itself does not have the integration of similar modules, and Apache has the function module, so there is no need to install the FASTCGI process management program. 2. Working principleNginx does not support direct invocation or parsing of external 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, you also need a fastcgi wrapper (wrapper can be understood as the program used to start another program), which is bound 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, the wrapper receives the request, and then derives a new thread, which invokes the interpreter or the external program to process the script and reads the return data; The wrapper then passes the returned data through the FastCGI interface, passing it to nginx along a fixed socket, and finally, Nginx sends the returned data to the client. This is the whole process of nginx+fastcgi, as shown in 1. Figure 1 nginx+fastcgi running process?  The FastCGI interface mode initiates one or more daemons on the Script resolution server (CGI application server) to parse the dynamic script, which is the FASTCGI process Manager, or the fastcgi engine. Both spawn-fcgi and PHP-FPM are fastcgi process managers (PHP and C + +).        3. Environment Deployment 3.1.Nginx installation, deployment and configuration Reference connection: http://blog.csdn.net/chdhust/article/details/9188937 installation, deployment, and configuration of 3.2.spawn_fastcgispawn_fastcgi https://github.com/lighttpd/spawn-fcgi     Here is the version of 1.6.3 https://github.com/lighttpd/spawn-fcgi/releases/tag/v1.6.3 download and then unzip and install it (please remember to read the Readme) if there is no Configure, please first perform ./autogen.sh, Generate configure ./configure MakeAfter compiling, move the executable file to the Nginx sbin directory CP./src/spawn-fcgi/usr/local/nginx/sbin/(CP to nginx installation directory) 3.3.fastcgi Library Installation (library is definitely not necessary, think the technology good Daniel can write himself)     Library Address  Http://www.fastcgi.com/dist/fcgi.tar.gz    After download, unzip and install (default installation) ./configure Make Make Install      4.Demo and Web publishing 4.1.Demo Program [CGI program] [CPP]View Plaincopy
  1. #include <fcgi_stdio.h>
  2. #include <stdlib.h>
  3. int main () {
  4. int count = 0;
  5. While (fcgi_accept () >= 0) {
  6. printf ("content-type:text/html\r\n"
  7. "\ r \ n"
  8. ""  
  9. "FastCGI hello!"
  10. "Request number%d running on host%s"
  11. "Process ID:%d\n", ++count, getenv ("SERVER_NAME"), Getpid ());
  12. }
  13. return 0;
  14. }
[Compile] g++ demo.cc-o demo-lfcgi?Run the executable directly to see if it works. If there is a missing library libfcgi.so.0, you need to manually /usr/local/lib/libfcgi.so.0The library establishes a link to the/usr/lib/directory: ln-s/usr/local/libfcgi.so.0/usr/lib/(or add So's library path to/etc/ld.so.conf and perform ldconfig update) 4.2.Web Release1) Move the CGI executable to the Nginx installation directory/usr/local/nginx/cgibin (the folder does not exist and create it Yourself) 2) Start the SPAWN-FCGI management process and bind the server IP and port (do not coincide with Nginx listening port) /usr/local/nginx/sbin/spawn-fcgi-a 127.0.0.1-p 8088-f/usr/local/nginx/cgibin/demo
Check to see if Port 9002 is successful: Netstat-na | grep 8088? 3) Change nginx.confConfig file, let Nginx forward request add configuration under the nodes of the HTTP node-"Server section" point Location ~ \.cgi$ {
Fastcgi_pass 127.0.0.1:8088;
Fastcgi_index index.cgi;
Fastcgi_param script_filename Fcgi$fastcgi_script_name;

include Fastcgi_params;     }       4) Restart Nginx or reload configuration file Reload profile sudo kill-hup [pid]Or     Re-start Nginx      killall Nginx ? ./nginx       5) Open a browser to access it http://localhost/demo.cgi   The results show:      Main references:1. http://blog.csdn.net/allenlinrui/article/details/19419721

Nginx + FastCGI Program (c + +) to build a high-performance Web service Demo

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.