`
zsjg13
  • 浏览: 142498 次
  • 性别: Icon_minigender_1
  • 来自: 安徽
社区版块
存档分类
最新评论

Introduction to FastCGI

 
阅读更多

FastCGI is actually a variation of CGI.

 

1、Understanding the CGI mechanism

The original purpose of a web server was merely to answer requests from clients by
serving files located on a storage device. The client sends a request to download a file
and the server processes the request and sends the appropriate response: 200 OK if
the file can be served normally, 404 if the file was not found, and other variants.


This mechanism has been in use since the beginning of the World Wide Web and it
still is. However, as stated before, static websites are being progressively abandoned
at the expense of dynamic ones that contain scripts that are processed by applications
such as PHP and Python among others. The web serving mechanism thus evolved
into the following:



 When a client attempts to visit a dynamic page, the web server receives the request
and forwards it to a third-party application. The application processes the script
independently and returns the produced response to the web server, which then
forwards the response back to the client.
In order for the web server to communicate with that application, the CGI protocol
was invented in the early 1990s.

 

 

2、Common Gateway Interface (CGI)

As stated in RFC 3875 (CGI protocol v1.1), designed by the Internet Society (ISOC):


The Common Gateway Interface (CGI) allows an HTTP server and a CGI script to
share responsibility for responding to client requests. […] The server is responsible
for managing connection, data transfer, transport, and network issues related to the
client request, whereas the CGI script handles the application issues such as data
access and document processing.

 

CGI is the protocol that describes the way information is exchanged between the web
server (Nginx) and the gateway application (PHP, Python, and so on). In practice,
when the web server receives a request that should be forwarded to the gateway
application, it simply executes the command corresponding to the desired application,
for example, /usr/bin/php. Details about the client request (such as the User Agent
and other request information) are passed either as command-line arguments or in
environment variables, while actual data from POST or PUT requests is transmitted
via the standard input. The invoked application then writes the processed document
contents to the standard output, which is recaptured by the web server.

 

While this technology seems simple and efficient enough at first sight, it comes with
a few major drawbacks, which are discussed as follows:

•  A unique process is spawned for each request. Memory and other context
information are lost from one request to another.

•  Starting up a process can be resource-consuming for the system. Massive
amounts of simultaneous requests (each spawning a process) could quickly
clutter a server.

•  Designing an architecture where the web server and the gateway application
would be located on different computers seems difficult, if not impossible.

 

 

3、Fast Common Gateway Interface (FastCGI)

The issues mentioned in the Common Gateway Interface (CGI) section render the
CGI protocol relatively inefficient for servers that are subject to heavy load. The
will to find solutions led Open Market in the mid-90s to develop an evolution of
CGI: FastCGI. It has become a major standard over the past fifteen years and most
web servers now offer the functionality, even proprietary server software such as
Microsoft IIS.

 

Although the purpose remains the same, FastCGI offers significant improvements
over CGI with the establishment of the following principles:

•  Instead of spawning a new process for each request, FastCGI employs
persistent processes that come with the ability to handle multiple requests.

•  The web server and the gateway application communicate with the use
of sockets such as TCP or POSIX Local IPC sockets. Consequently, both
processes may be on two different computers on a network.

•  The web server forwards the client request to the gateway and receives the
response within a single connection. Additional requests may also follow
without needing to create additional connections. Note that on most web
servers, including Nginx and Apache, the implementation of FastCGI does
not (or at least not fully) support multiplexing(多路复用).

•  Since FastCGI is a socket-based protocol, it can be implemented on any
platform with any programming language.

 

Throughout this chapter, we will be setting up PHP and Python via FastCGI.
Additionally, you will find the mechanism to be relatively similar in the case of 
other applications, such as Perl or Ruby on Rails.

 

Designing a FastCGI-powered architecture is actually not as complex as one might
imagine. As long as you have the web server and the processing application running,
the only difficulty that remains is to establish the connection between both parties.
The first step in that perspective is to configure the way Nginx will communicate
with the FastCGI application. FastCGI compatibility with Nginx is introduced by 
the FastCGI module. This section details the directives that are made available by 
the module.

 

 

4、uWSGI and SCGI

Before reading the rest of the chapter, you should know that Nginx offers two other
CGI-derived module implementations:

•  The uWSGI module allows Nginx to communicate with applications through
the uwsgi protocol, itself derived from Web Server Gateway Interface
(WSGI). The most commonly used (if not the unique) server implementing
the uwsgi protocol is the unoriginally named uWSGI server. Its latest
documentation can be found at http://uwsgi-docs.readthedocs.org.
This module will prove useful to Python adepts(能手;专家) seeing as the uWSGI project
was designed mainly for Python applications.

•  SCGI, which stands for Simple Common Gateway Interface, is a variant of the
CGI protocol, much like FastCGI. Younger than FastCGI since its specification
was first published in 2006, SCGI was designed to be easier to implement and
as its name suggests: simple. It is not related to a particular programming
language. SCGI interfaces and modules can be found in a variety of software
projects such as Apache, IIS, Java, Cherokee, and a lot more.

 

There are no major differences in the way Nginx handles the FastCGI, uwsgi and SCGI
protocols: each of these have their respective module, containing similarly named
directives. The following table lists a couple of directives from the FastCGI module,
which are detailed in following sections, and their uWSGI and SCGI equivalents:



 Directive names and syntaxes are identical. In addition, the Nginx development
team has been maintaining all three modules in parallel. New directives or directive
updates are always applied to all of them. As such, the following sections will be
documenting Nginx's implementation of the FastCGI protocol, but they also apply to
uWSGI and SCGI.

 

 

5、Upstream blocks

With the FastCGI module, and as you will discover in the next chapter with the Proxy
module too, Nginx forwards requests to backend servers. It communicates with
processes using either FastCGI or simply by behaving like a regular HTTP client.
Either way, the backend server (a FastCGI application, another web server, and so on)
may be hosted on a different server in the case of load-balanced architectures:

 

 The general issue with applications (such as PHP) is that they are quite 
resource-consuming, especially in terms of CPU. Therefore, you may find  
yourself forced to balance the load across multiple servers, resulting in the  
following architecture:



 In this case, Nginx is connected to multiple backend servers. To establish such a
configuration, a new module comes into play: the upstream module.

 

5.1、Module syntax

The upstream module allows you to declare named upstream blocks that define 
lists of servers:
upstream phpfpm {
  server 192.168.0.50:9000;
  server 192.168.0.51:9000;
  server 192.168.0.52:9000;
}

When defining the FastCGI configuration, connect to the  upstream block:

server {
    server_name website.com;
    location ~* \.php$ {
        fastcgi_pass phpfpm;
        […]
    }
}

In this case, requests eligible to FastCGI will be forwarded to one of the backend
servers defined in the upstream block.

A question you might ask is, how does Nginx decide which backend server is to be
employed for each request? And the answer is simple: the default method of the
Upstream module is round robin. However, this method is not necessarily the best.
Two requests from the same visitor might be processed by two different servers,
and that could be a problem for many reasons (for example, when PHP sessions are
stored on the backend server and are not replicated across the other servers).
To ensure that requests from a same visitor always get processed by the same backend
server, you may enable the ip_hash option when declaring the upstream block:
upstream phpfpm {
  ip_hash;
  server 192.168.0.50:9000;
  server 192.168.0.51:9000;
  server 192.168.0.52:9000;
}
This will distribute requests based on the visitors IP address employing a regular
round robin algorithm.However, be aware that client IP addresses are sometimes
subject to change for various reasons such as dynamic IP refresh, proxy switching,
Tor. Consequently, the ip_hash mechanism cannot fully guarantee that clients will
always be involved to the same upstream server. Alternatively, you may force Nginx
to select the backend server that currently has the last amount of active connections,
through the use of the least_conn directive.

 

5.2、Server directive

The server directive that you place within upstream blocks accepts several
parameters that influence the backend selection by Nginx:
•  weight=n: This lets you indicate a numeric value that will affect the 
weight of the backend server. If you create an upstream block with 
two backend servers, and set the weight of the first one to 2, it will 
be selected twice more often:
upstream php {
  server 192.168.0.1:9000 weight=2;
  server 192.168.0.2:9000;
}
•  max_fails=n: This defines the number of communication failures 
that should occur (in the time frame specified with the fail_timeout
parameter below) before Nginx considers the server inoperative.

•  fail_timeout=n: This defines the time frame within which the maximum
failure count applies. If Nginx fails to communicate with the backend 
server max_fails times over fail_timeout seconds, the server is 
considered inoperative.
•  down: If you mark a backend server as down, the server is no longer used. 
This only applies when the ip_hash directive is enabled.
•  backup: If you mark a backend server as backup, Nginx will not make 
use of the server until all other servers (servers not marked as backup) 
are down or inoperative.

 

These parameters are all optional and can be used altogether:
upstream phpbackend {
  server localhost:9000 weight=5;
  server 192.168.0.1 max_fails=5 fail_timeout=60s;
  server unix:/tmp/backend backup;
}



 

  • 大小: 11 KB
  • 大小: 27.6 KB
  • 大小: 12.6 KB
  • 大小: 17.6 KB
  • 大小: 20.6 KB
  • 大小: 21.7 KB
分享到:
评论

相关推荐

    fastcgi c++

    fastcgi fastcgi fastcgi fastcgi fastcgi

    FastCGI 下载FastCGI for IIS6下载 1.5(32位&64位)

    **FastCGI技术详解** FastCGI是一种常用于提高Web服务器性能的技术,它作为一个接口,允许Web服务器与外部应用程序(如PHP解释器)进行高效通信。FastCGI的主要优势在于它可以长时间保持活动状态,避免了每次请求都...

    FastCGI SDK

    **FastCGI SDK详解** FastCGI是一种常用于Web服务器上的接口协议,它允许外部程序(如PHP、Perl、Python等脚本语言解释器)作为Web服务器的动态内容生成器,提高了网站性能和响应速度。FastCGI的核心设计目标是避免...

    FastCGI For IIS6.0

    相比之下,FastCGI保持了CGI进程的长期运行,使得多次请求可以复用同一进程,显著降低了进程管理的开销,提高了服务响应速度。 FastCGI for IIS6.0是专为Windows Server 2003上的IIS6.0设计的,它允许IIS服务器与...

    fastcgi中文手册.pdf

    《FastCGI中文手册》是IT领域中关于Web服务器与应用程序接口的重要参考资料,它详细介绍了FastCGI协议的原理、实现以及应用。FastCGI是一种用于提高动态内容生成速度的技术,它通过持久连接来减少每次请求时的启动和...

    FastCGI手册

    ### FastCGI手册知识点解析 #### 一、FastCGI技术概述 FastCGI是一种用于提高Web应用程序性能的技术,尤其适用于提高动态网页处理的速度。它通过优化传统的CGI(Common Gateway Interface)模型,解决了CGI效率低下...

    Java的FastCGI网关 jFastCGI

    **Java的FastCGI网关 jFastCGI** 在Web开发中,FastCGI(Fast Common Gateway Interface)是一种常用于提高动态网站性能的技术,它允许Web服务器与后端应用程序进行高效通信。jFastCGI是Java平台上的一个实现,它为...

    fastcgi头文件和库

    标题中的“fastcgi头文件和库”指的是FastCGI接口的开发资源,这通常包括了C语言编程中使用的头文件(header files)和库文件(library files)。FastCGI是一种常用于Web服务器与应用程序之间通信的协议,它提高了...

    nginx-1.14.0.zip_nginx fastcgi

    在配置Nginx以使用FastCGI时,你需要在Nginx配置文件中定义一个location块,指定哪些URL应该通过FastCGI处理。例如: ```nginx location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/...

    Fastcgi学习总结(C/C++语言版)

    FastCGI是一种高性能的Web服务器接口,它是CGI(通用网关接口)的增强版本,旨在解决传统CGI性能上的问题。FastCGI程序能够长时间运行,而不是每次用户请求时都创建新的进程,从而提高了效率并降低了服务器负载。...

    用VC++开发FastCGI+MySQL的基本源代码

    在IT行业中,FastCGI(Fast Common Gateway Interface)是一种用于提高Web服务器性能的技术,它能够使Web服务器与外部应用程序(如动态脚本语言解释器)进行高效通信。FastCGI的核心理念是保持进程常驻,避免每次...

    mod-fastcgi-2.4.6 for apache2.40

    mod_fastcgi是Apache的一个模块,它实现了FastCGI协议,使得Apache服务器能够与FastCGI进程管理器(如spawn-fcgi或fcgiwrap)通信,进而调用动态语言(如PHP、Perl、Python等)的解释器。这个模块提供了比mod_php等...

    Fastcgi 中文参考手册(DOC)

    **FastCGI 中文参考手册概述** FastCGI 是一种用于 Web 服务器和应用程序之间通信的协议,它旨在提高网站性能,特别是在处理动态内容时。FastCGI 的设计目标是克服 CGI(Common Gateway Interface)的性能瓶颈,...

    fastcgi-2.4.0安装包

    标题中的“fastcgi-2.4.0安装包”指的是FastCGI的2.4.0版本,这是一个软件包,用于在Linux系统中部署和管理FastCGI服务。此版本可能包含了一些性能优化和新的功能特性,以便更好地支持现代Web环境。 描述中提到的...

    windows+IIS 环境下以Fastcgi方式配置php 5.3.3的方法

    下载 Microsoft FastCGI Extension for IIS 5.1 and 6.0,用于启用 Fastcgi 扩展。 步骤 5:配置 FastCGI 扩展 安装 FastCGI 扩展,并配置 fcigext.ini 文件,在文件末尾添加以下内容: [Types] php=PHP [PHP] ...

    Fastcgi 中文参考手册(DOC).rar

    《FastCGI中文参考手册》是一本专注于FastCGI技术的详细教程,旨在帮助开发者深入理解和有效运用这一高效、稳定的Web服务器接口技术。FastCGI是用于提高动态内容生成速度的一种协议,它允许Web服务器与长期运行的...

    IIS FASTCGI安装程序

    用于IIS的FASTCGI模块安装包,版本1.5RC

    fastcgi 数据发送,跳转 实例

    **FastCGI 数据发送与跳转实例** FastCGI(Fast Common Gateway Interface)是一种协议,用于将Web服务器(如Apache)与外部程序(通常为PHP、Python或Perl解释器)进行通信,以处理动态内容。相比传统的CGI,...

Global site tag (gtag.js) - Google Analytics