Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do webserver and cgi process communicate with each other?

I want to understand how the webserver (for example: nginx) and cgi/fastcgi communicate with each other. How does the webserver pass cgi script to cgi process and how does the cgi process respond to the request.

In Nginx, we configure like this to let nginx passes PHP scripts to php-fpm

 location / {
            root   /home/service/public_html;
            fastcgi_pass   unix:/tmp/php-fpm-test.socket;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /home/service/public_html/index.php;
            include        fastcgi_params;
        }

How does it works?

Edit: It would be appreciated if someone could give me a piece of pseudo code to describe the communication between a process (or whatever) and php-fpm unix socket.

like image 534
hungnv Avatar asked Nov 04 '22 08:11

hungnv


1 Answers

A CGI application is simply a standard executable or script - each HTTP request to the web server corresponds to a single execution / instance of that executable or script where environment variables are used to pass information about the request (such as the request URL and request method) and the HTTP request body is passed on the standard input. The script / executable the passes the raw HTTP output through the standard output stream to the web server.

For a example of a CGI application see the wikipedia page for an example perl script and for more detail have a read through of the CGI specification


Fast CGI is an attempt to reduce the overhead of the CGI interface - as starting a new process is a relatively expensive task on many operating systems, Fast CGI attempts to reduce this by allowing a single long-running Fast CGI process to handle many HTTP requests.

Although many parts of Fast CGI are similar to CGI (for example the format of the environment variables), with Fast CGI all information is passed through the standard input stream.

You should try looking at the Fast CGI website for more information - in particular the Fast CGI specification is on there and explains all of this in detail.

like image 195
Justin Avatar answered Nov 09 '22 09:11

Justin