什么是通用网关接口 (CGI)?

发布于 2024-08-18 15:42:57 字数 2096 浏览 6 评论 0原文

CGI 是通用网关接口。顾名思义,它是所有事物的“通用”网关接口。从名字上看,它是如此琐碎和幼稚。我觉得我明白了这一点,每次遇到这个词都有这样的感觉。但坦白说,我没有。我还是很困惑。

我是一名具有 Web 开发经验的 PHP 程序员。

用户(客户端)请求页面--->网络服务器(->嵌入式 PHP 翻译)---->服务器端(PHP)脚本---> MySQL 服务器。

现在假设我的 PHP 脚本可以从 MySQL 服务器获取结果MATLAB 服务器和其他一些服务器。

那么,现在的PHP Script就是CGI吗?因为它的接口用于网络服务器和网络服务器之间。所有其他服务器?我不知道。有时他们称 CGI 为一种技术和技术。有时他们将 CGI 称为程序或其他服务器。

  • CGI到底是什么?

  • /cgi-bin/*.cgi 有什么大不了的?这是怎么回事?我不知道服务器上的这个cgi-bin目录是做什么用的。我不知道为什么它们有 *.cgi 扩展名。

  • 为什么 Perl 总是碍事。 CGI 和Perl(语言)。我也不知道这两个人怎么样了。我几乎一直听到这两个组合在一起的“CGI & Perl”。这本书是另一个很好的例子使用 Perl 进行 CGI 编程。为什么不是“使用 PHP/JSP/ASP 进行 CGI 编程”?我从来没有见过这样的事情。

  • CGI 编程,让我很困惑。 “C 语言”?严重地??我不知道该说什么。我只是很困惑。 “C 语言”?这改变了一切。程序需要编译并执行。这完全改变了我对网络编程的看法。我什么时候编译?程序如何执行(因为它将是机器代码,所以它必须作为独立进程执行)。它如何与网络服务器通信?工控机?并使用套接字编程与所有服务器(在我的示例中为 MATLAB 和 MySQL)进行交互?我迷路了!!

  • 人们说 CGI 已被弃用并且不再使用。是这样吗?最新更新是什么?

有一次,我遇到了这样的情况: 必须授予 HTTP PUT 请求访问权限 网络服务器(Apache HTTPD)。它是一个长 后退。所以,据我所知这是 我做了什么:

  1. 编辑了Apache HTTPD的配置文件以告诉Web服务器通过 所有 HTTP PUT 请求到某些 put.php (我必须编写这个 PHP 脚本)

  2. 实现put.php来处理请求(将文件保存到该位置 提到)

人们说我写了一个 CGI 脚本。 说真的,我不知道什么 他们正在谈论。

  • 我真的写了CGI脚本吗?

我希望你明白我的困惑是什么。 (因为我自己也不知道自己迷茫在哪里)。我请求你们的答案尽可能简单。我真的听不懂任何花哨的技术术语。至少在这种情况下不是。

编辑:

我发现了这个很棒的教程“CGI编程很简单!” - CGI 教程,以最简单可能的方式解释概念。阅读本文后,您可能需要阅读C 语言 CGI 编程入门 用实际的代码示例来补充您的理解。我还将本教程的这些链接添加到维基百科的文章中: http://en.wikipedia.org/wiki /Common_Gateway_Interface

CGI is a Common Gateway Interface. As the name says, it is a "common" gateway interface for everything. It is so trivial and naive from the name. I feel that I understood this and I felt this every time I encountered this word. But frankly, I didn't. I'm still confused.

I am a PHP programmer with web development experience.

user (client) request for page ---> webserver(->embedded PHP
interpreter) ----> Server side(PHP) Script ---> MySQL Server.

Now say my PHP Script can fetch results from MySQL server & MATLAB server & some other server.

So, now PHP Script is the CGI? Because its interface for the between webserver & All other servers? I don't know. Sometimes they call CGI, a technology & other times they call CGI a program or some other server.

  • What exactly is CGI?

  • Whats the big deal with /cgi-bin/*.cgi? What's up with this? I don't know what is this cgi-bin directory on the server for. I don't know why they have *.cgi extensions.

  • Why does Perl always comes in the way. CGI & Perl (language). I also don't know what's up with these two. Almost all the time I keep hearing these two in combination "CGI & Perl". This book is another great example CGI Programming with Perl. Why not "CGI Programming with PHP/JSP/ASP"? I never saw such things.

  • CGI Programming in C, confuses me a lot. "in C"?? Seriously?? I don't know what to say. I'm just confused. "in C"?? This changes everything. Program needs to be compiled and executed. This entirely changes my view of web programming. When do I compile? How does the program gets executed (because it will be a machine code, so it must execute as a independent process). How does it communicate with the web server? IPC? and interfacing with all the servers (in my example MATLAB & MySQL) using socket programming? I'm lost!!

  • People say that CGI is deprecated and isn't in use anymore. Is that so? What is the latest update?

Once, I ran into a situation where I
had to give HTTP PUT request access to
web server (Apache HTTPD). Its a long
back. So, as far as I remember this is
what I did:

  1. Edited the configuration file of Apache HTTPD to tell webserver to pass
    all HTTP PUT requests to some
    put.php ( I had to write this PHP
    script)

  2. Implement put.php to handle the request (save the file to the location
    mentioned)

People said that I wrote a CGI Script.
Seriously, I didn't have a clue what
they were talking about.

  • Did I really write CGI Script?

I hope you understood what my confusion is. (Because I myself don't know where I'm confused). I request you guys to keep your answer as simple as possible. I really can't understand any fancy technical terminology. At least not in this case.

EDIT:

I found this amazing tutorial "CGI Programming Is Simple!" - CGI Tutorial, which explains the concepts in simplest possible way. After reading this article you may want to read Getting Started with CGI Programming in C to supplement your understanding with actual code samples. I've also added these links to this tutorial to Wikipedia's article: http://en.wikipedia.org/wiki/Common_Gateway_Interface

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

末が日狂欢 2024-08-25 15:42:57

CGI 是一个接口,它告诉网络服务器如何将数据传入和传出应用程序。更具体地说,它描述了如何在环境变量中传递请求信息(例如请求类型、远程IP地址)、如何通过标准输入传入请求主体以及如何通过标准输出传递出响应。详细信息可以参考CGI规范

要使用您的图像:

用户(客户端)请求页面 --->网络服务器 ---[CGI]---->服务器端程序---> MySQL 服务器。

大多数(如果不是全部)网络服务器都可以配置为以“CGI”形式执行程序。这意味着网络服务器在收到请求后,会将数据转发到特定程序,设置一些环境变量并通过标准输入和标准输出编组参数,以便程序知道在哪里查找以及查找什么内容。

主要好处是,只要网络服务器和程序都知道 CGI 的工作原理,您就可以从网络运行任何可执行代码。这就是为什么您可以使用支持 CGI 的常规 Web 服务器用 C 或 Bash 编写 Web 程序。而且大多数编程环境都可以轻松使用标准输入、标准输出和环境变量。

在您的情况下,您很可能使用另一种特定于 PHP 的脚本和网络服务器之间的通信方式,正如您在问题中提到的那样,这是一个名为 mod_php 的嵌入式解释器。

那么,回答你的问题:

CGI到底是什么?

见上文。

/cgi-bin/*.cgi 有什么大不了的?这是怎么回事?我不知道服务器上的这个 cgi-bin 目录是做什么用的。我不知道为什么他们有 *.cgi 扩展名。

这是 CGI 程序的传统位置,许多网络服务器都预先配置了此目录,以将其中的所有二进制文件作为 CGI 程序执行。 .cgi 扩展名表示预期通过 CGI 运行的可执行文件。

为什么 Perl 总是碍事。 CGI 和Perl(语言)。我也不知道这两个人怎么样了。我几乎一直听到这两个组合在一起的“CGI & Perl”。这本书是另一个很好的例子,用 Perl 进行 CGI 编程,为什么不是“用 PHP/JSP/ASP 进行 CGI 编程”。我从来没有见过这样的事情。

因为 Perl 很古老(比 PHP、JSP 和 ASP 更古老,它们都是在 CGI 很旧的时候出现的,而 Perl 在 CGI 还很新的时候就存在了),并且因为是一种非常好的通过 CGI 提供动态网页的语言而闻名。如今,还有其他替代方案可以在网络服务器中运行 Perl,主要是 mod_perl

CGI 编程这让我很困惑。在C语言中??严重地??我不知道该说什么。我只是很困惑。“在C中”??这改变了一切。程序需要编译和执行。这完全改变了我对Web编程的看法。我什么时候编译?程序如何执行(因为它将是一个机器代码,所以它必须作为一个独立的进程执行)。 /p>

机器代码,所以它必须作为一个独立的进程执行)。它 编译可执行文件一次,网络服务器执行程序并将请求中的数据传递给程序并输出接收到的响应,CGI 指定每个请求将启动一个程序实例,这就是为什么 CGI 效率低下并且有点过时的原因。 。

他们说 CGI 已被弃用。它不再使用了。是这样吗?最新更新是什么?

当性能不是最重要并且需要一种简单的执行代码方法时,仍然使用 CGI。由于前面所述的原因,它效率低下,并且有更现代的方法可以在网络环境中执行任何程序。目前最著名的是FastCGI

CGI is an interface which tells the webserver how to pass data to and from an application. More specifically, it describes how request information is passed in environment variables (such as request type, remote IP address), how the request body is passed in via standard input, and how the response is passed out via standard output. You can refer to the CGI specification for details.

To use your image:

user (client) request for page ---> webserver ---[CGI]----> Server side Program ---> MySQL Server.

Most if not all, webservers can be configured to execute a program as a 'CGI'. This means that the webserver, upon receiving a request, will forward the data to a specific program, setting some environment variables and marshalling the parameters via standard input and standard output so the program can know where and what to look for.

The main benefit is that you can run ANY executable code from the web, given that both the webserver and the program know how CGI works. That's why you could write web programs in C or Bash with a regular CGI-enabled webserver. That, and that most programming environments can easily use standard input, standard output and environment variables.

In your case you most likely used another, specific for PHP, means of communication between your scripts and the webserver, this, as you well mention in your question, is an embedded interpreter called mod_php.

So, answering your questions:

What exactly is CGI?

See above.

Whats the big deal with /cgi-bin/*.cgi? Whats up with this? I don't know what is this cgi-bin directory on the server for. I don't know why they have *.cgi extensions.

That's the traditional place for cgi programs, many webservers come with this directory pre configured to execute all binaries there as CGI programs. The .cgi extension denotes an executable that is expected to work through the CGI.

Why does Perl always comes in the way. CGI & Perl (language). I also don't know whats up with these two. Almost all the time I keep hearing these two in combination "CGI & Perl". This book is another great example CGI Programming with Perl Why not "CGI Programming with PHP/JSP/ASP". I never saw such things.

Because Perl is ancient (older than PHP, JSP and ASP which all came to being when CGI was already old, Perl existed when CGI was new) and became fairly famous for being a very good language to serve dynamic webpages via the CGI. Nowadays there are other alternatives to run Perl in a webserver, mainly mod_perl.

CGI Programming in C this confuses me a lot. in C?? Seriously?? I don't know what to say. I"m just confused. "in C"?? This changes everything. Program needs to be compiled and executed. This entirely changes my view of web programming. When do I compile? How does the program gets executed (because it will be a machine code, so it must execute as a independent process). How does it communicate with the web server? IPC? and interfacing with all the servers (in my example MATLAB & MySQL) using socket programming? I'm lost!!

You compile the executable once, the webserver executes the program and passes the data in the request to the program and outputs the received response. CGI specifies that one program instance will be launched per each request. This is why CGI is inefficient and kind of obsolete nowadays.

They say that CGI is deprecated. Its no more in use. Is it so? What is its latest update?

CGI is still used when performance is not paramount and a simple means of executing code is required. It is inefficient for the previously stated reasons and there are more modern means of executing any program in a web enviroment. Currently the most famous is FastCGI.

与风相奔跑 2024-08-25 15:42:57

CGI到底是什么?

Web 服务器从程序(而不是文件等)获取数据的一种方法。

/cgi-bin/*.cgi 有什么大不了的?

没什么大不了的。这只是一个约定。

我不知道服务器上的这个cgi-bin目录是做什么用的。
我不知道为什么他们有 *.cgi 扩展名。

服务器必须知道如何处理该文件(即,将其视为要执行的程序,而不是简单地提供服务)。 .html 扩展名告诉它使用 text/html 内容类型。具有 .cgi 扩展名告诉它将其作为程序运行。

将可执行文件保存在单独的目录中可以提供额外的保护,防止执行不正确的文件和/或将 CGI 程序作为原始数据提供,以防服务器配置错误。

为什么 Perl 总是碍事。

事实并非如此。 Perl 与 CGI 同时盛行。

我已经很多年没有使用 Perl CGI 了。我使用 mod_perl 很长时间了,最​​近倾向于 PSGI/Plack 和 FastCGI。

这本书是另一个很好的使用 Perl 进行 CGI 编程的示例
为什么不是“使用 PHP/JSP/ASP 进行 CGI 编程”。

CGI 效率不高。与 PHP 大约同时出现的与网络服务器中的程序进行交互的更好方法。 JSP 和 ASP 是与程序对话的不同方法。

CGI 编程这让我很困惑。在C语言中??认真的吗?

它是一种编程语言,为什么不呢?

什么时候编译?

  1. 编写代码
  2. 编译
  3. 访问URL
  4. Web服务器运行程序

程序如何执行(因为它将是机器代码,所以它必须作为独立进程执行)。

它不必作为独立进程执行(您可以用 C 语言编写 Apache 模块),但 CGI 的整个概念是它启动一个外部进程。

它如何与网络服务器通信?工控机?

STDIN/STDOUT 和环境变量 — 如 CGI 规范中所定义。

并使用套接字与所有服务器(在我的示例中为 MATLAB 和 MySQL)连接
编程?

使用您喜欢并受到支持的任何方法。

他们说 CGI 贬值了。它不再使用了。是这样吗?

CGI 效率低下、缓慢且简单。它很少被使用,当它被使用时,是因为它简单。如果性能不是什么大问题,那么简单性就很有价值。

最新更新是什么?

1.1

What exactly is CGI?

A means for a web server to get its data from a program (instead of, for instance, a file).

Whats the big deal with /cgi-bin/*.cgi?

No big deal. It is just a convention.

I don't know what is this cgi-bin directory on the server for.
I don't know why they have *.cgi extensions.

The server has to know what to do with the file (i.e. treat it as a program to execute instead of something to simply serve up). Having a .html extension tells it to use a text/html content type. Having a .cgi extension tells it to run it as a program.

Keeping executables in a separate directory gives some added protection against executing incorrect files and/or serving up CGI programs as raw data in case the server gets misconfigured.

Why does Perl always comes in the way.

It doesn't. Perl was just big and popular at the same time as CGI.

I haven't used Perl CGI for years. I was using mod_perl for a long time, and tend towards PSGI/Plack with FastCGI these days.

This book is another great example CGI Programming with Perl
Why not "CGI Programming with PHP/JSP/ASP".

CGI isn't very efficient. Better methods for talking to programs from webservers came along at around the same time as PHP. JSP and ASP are different methods for talking to programs.

CGI Programming in C this confuses me a lot. in C?? Seriously??

It is a programming language, why not?

When do I compile?

  1. Write code
  2. Compile
  3. Access URL
  4. Webserver runs program

How does the program gets executed (because it will be a machine code, so it must execute as a independent process).

It doesn't have to execute as an independent process (you can write Apache modules in C), but the whole concept of CGI is that it launches an external process.

How does it communicate with the web server? IPC?

STDIN/STDOUT and environment variables — as defined in the CGI specification.

and interfacing with all the servers (in my example MATLAB & MySQL) using socket
programming?

Using whatever methods you like and are supported.

They say that CGI is depreciated. Its no more in use. Is it so?

CGI is inefficient, slow and simple. It is rarely used, when it is used, it is because it is simple. If performance isn't a big deal, then simplicity is worth a lot.

What is its latest update?

1.1

尸血腥色 2024-08-25 15:42:57

CGI 是 Web 服务器(HTTP 服务器)和处理特定请求的某种类型的可执行程序之间的接口规范。

它描述了该请求的某些属性应如何传递到该程序的环境,以及程序应如何将响应传递回服务器,以及服务器应如何“完成”响应以形成对原始 HTTP 请求的有效答复。

有一段时间,CGI 是 IETF 互联网草案,因此有一个到期日。它已过期且没有更新,因此没有 CGI“标准”。它现在是一个信息性 RFC,但因此记录了常见做法,本身并不是标准。 rfc3875.txtrfc3875.html

实现 CGI 接口的程序可以用任何可在目标机器上运行的语言编写。它们必须能够访问环境变量以及通常标准输入,并在标准输出上生成输出。

诸如 C 之类的编译语言以及诸如 perl 之类的脚本语言都被广泛使用,通常使用库来使访问 CGI 环境变得更加容易。

CGI 的一大缺点是为每个请求生成一个新程序,因此维护请求之间的状态可能是一个主要的性能问题。状态可以在 cookie 中处理或在 URL 中编码,但如果它变得很大,则必须将其存储在其他地方并从编码的 url 信息或 cookie 中键入。然后,每个 CGI 调用都必须从某处的存储中重新加载存储的状态。

出于这个原因,并且对于请求和会话的极其简单的接口,Web 服务器和应用程序之间更好的集成环境更加受欢迎。像使用 apache 的现代 php 实现这样的环境可以更好地将目标语言与 Web 服务器集成,并提供对有效服务 http 请求所需的请求和会话对象的访问。它们提供了一种更简单、更丰富的方式来编写“程序”来处理 HTTP 请求。

您是否编写了 CGI 脚本取决于解释。它确实完成了其中一项工作,但更常见的是作为模块运行 php,其中脚本和服务器之间的接口严格来说不是 CGI 接口。

CGI is an interface specification between a web server (HTTP server) and an executable program of some type that is to handle a particular request.

It describes how certain properties of that request should be communicated to the environment of that program and how the program should communicate the response back to the server and how the server should 'complete' the response to form a valid reply to the original HTTP request.

For a while CGI was an IETF Internet Draft and as such had an expiry date. It expired with no update so there was no CGI 'standard'. It is now an informational RFC, but as such documents common practice and isn't a standard itself. rfc3875.txt, rfc3875.html

Programs implementing a CGI interface can be written in any language runnable on the target machine. They must be able to access environment variables and usually standard input and they generate their output on standard output.

Compiled languages such as C were commonly used as were scripting languages such as perl, often using libraries to make accessing the CGI environment easier.

One of the big disadvantages of CGI is that a new program is spawned for each request so maintaining state between requests could be a major performance issue. The state might be handled in cookies or encoded in a URL, but if it gets to large it must be stored elsewhere and keyed from encoded url information or a cookie. Each CGI invocation would then have to reload the stored state from a store somewhere.

For this reason, and for a greatly simple interface to requests and sessions, better integrated environments between web servers and applications are much more popular. Environments like a modern php implementation with apache integrate the target language much better with web server and provide access to request and sessions objects that are needed to efficiently serve http requests. They offer a much easier and richer way to write 'programs' to handle HTTP requests.

Whether you wrote a CGI script rather depends on interpretation. It certainly did the job of one but it is much more usual to run php as a module where the interface between the script and the server isn't strictly a CGI interface.

你如我软肋 2024-08-25 15:42:57

CGI 在 RFC 3875 中指定,尽管这是后来的“官方”编纂原始NCSA 文档。基本上,CGI 定义了一个协议,用于将有关 HTTP 请求的数据从 Web 服务器传递到要处理的程序(任何程序、任何语言)。在编写规范时(1993 年),大多数 Web 服务器仅包含静态页面,“Web 应用程序”是一种罕见的新事物,因此将它们与“正常”静态内容区分开来似乎很自然,例如在除了静态内容之外的 cgi-bin 目录,并以 .cgi 结尾。

当时,还没有像 PHP 这样的专用“Web 编程语言”,而 C 是占主导地位的可移植编程语​​言 - 因此很多人用 C 编写 CGI 脚本。但 Perl 很快就被证明更适合这种类型。有一段时间,CGI 几乎成了 Perl 的代名词。然后出现了 Java Servlet、PHP 和其他一些语言,并占领了 Perl 的大部分市场份额。

The CGI is specified in RFC 3875, though that is a later "official" codification of the original NCSA document. Basically, CGI defines a protocol to pass data about a HTTP request from a webserver to a program to process - any program, in any language. At the time the spec was written (1993), most web servers contained only static pages, "web apps" were a rare and new thing, so it seemed natural to keep them apart from the "normal" static content, such as in a cgi-bin directory apart from the static content, and having them end in .cgi.

At this time, here also were no dedicated "web programming languages" like PHP, and C was the dominating portable programming language - so many people wrote their CGI scripts in C. But Perl quickly turned out to be a better fit for this kind of thing, and CGI became almost synonymous with Perl for a while. Then there came Java Servlets, PHP and a bunch of others and took over large parts of Perl's market share.

若无相欠,怎会相见 2024-08-25 15:42:57

查看 Wikipedia 中的 CGI。 CGI 是 Web 服务器与外部程序或脚本之间的协议,用于处理输入并生成发送到浏览器的输出。

CGI 只是 Web 服务器和程序进行通信的一种方式,仅此而已。这里,服务器管理网络连接和 HTTP 协议,程序处理输入并生成发送到浏览器的输出。 CGI脚本基本上可以是任何可以由网络服务器执行并遵循CGI协议的程序。因此,CGI 程序可以用 C 等语言来实现。但是这种情况极为罕见,因为 C 不太适合该任务。

/cgi-bin/*.cgi 只是人们通常放置 CGI 脚本的路径。 Web 服务器通常默认配置为从该路径获取 CGI 脚本。

CGI 脚本也可以用 PHP 实现,但所有 PHP 程序都不是 CGI 脚本。如果Web服务器嵌入了PHP解释器(例如Apache中的mod_php),则Web服务器和解释器之间更有效的直接协议将跳过CGI阶段。

您是否实现了 CGI 脚本取决于 Web 服务器如何执行您的脚本。

Have a look at CGI in Wikipedia. CGI is a protocol between the web server and a external program or a script that handles the input and generates output that is sent to the browser.

CGI is a simply a way for web server and a program to communicate, nothing more, nothing less. Here the server manages the network connection and HTTP protocol and the program handles input and generates output that is sent to the browser. CGI script can be basically any program that can be executed by the webserver and follows the CGI protocol. Thus a CGI program can be implemented, for example, in C. However that is extremely rare, since C is not very well suited for the task.

/cgi-bin/*.cgi is a simply a path where people commonly put their CGI script. Web server are commonly configured by default to fetch CGI scripts from that path.

a CGI script can be implemented also in PHP, but all PHP programs are not CGI scripts. If webserver has embedded PHP interpreter (e.g. mod_php in Apache), then the CGI phase is skipped by more efficient direct protocol between the web server and the interpreter.

Whether you have implemented a CGI script or not depends on how your script is being executed by the web server.

暮光沉寂 2024-08-25 15:42:57

CGI 本质上将请求传递给使用 Web 服务器配置的任何解释器 - 这可以是 Perl、Python、PHP、Ruby、C 几乎任何东西。 Perl 是当时最常见的语言,这就是为什么你经常在 CGI 中看到它。

CGI 并没有消亡。事实上,大多数大型托管公司将 PHP 作为 CGI 运行,而不是 mod_php,因为它提供用户级配置和其他一些东西,但比 mod_php 慢。 Ruby 和 Python 通常也作为 CGI 运行。它们的主要区别在于,服务器模块作为实际服务器软件的一部分运行 - 与 CGI 一样,它完全在服务器之外。服务器仅使用 CGI 模块来确定如何向外部解释器传递和接收数据。

CGI essentially passes the request off to any interpreter that is configured with the web server - This could be Perl, Python, PHP, Ruby, C pretty much anything. Perl was the most common back in the day thats why you often see it in reference to CGI.

CGI is not dead. In fact most large hosting companies run PHP as CGI as opposed to mod_php because it offers user level config and some other things while it is slower than mod_php. Ruby and Python are also typically run as CGI. they key difference here is that a server module runs as part of the actual server software - where as with CGI its totally outside the server The server just uses the CGI module to determine how to pass and recieve data to the outside interpreter.

天赋异禀 2024-08-25 15:42:57

CGI 是一种机制,Web 服务器调用外部程序来处理请求,并使用环境变量和标准输入将请求数据提供给程序。尽管用某些语言编写 CGI 程序比用其他语言更容易,但外部程序所使用的具体语言并不重要。

由于 CGI 脚本需要执行权限,因此出于安全目的(现在可能是误导),httpd 默认情况下仅允许运行 cgi-bin 目录中的 CGI 程序。

大多数 PHP 脚本通过 mod_php 在 Web 服务器进程中运行。这不是计算机生成图像。

CGI 速度很慢,因为程序(和相关解释器)必须根据请求启动。现代替代方案是 mod_php 使用的嵌入式执行和 FastCGI 使用的长时间运行的进程。给定的语言可能有自己的实现这些机制的方式,因此在求助于 CGI 之前一定要先询问一下。

CGI is a mechanism whereby an external program is called by the web server in order to handle a request, with environment variables and standard input being used to feed the request data to the program. The exact language the external program is written in does not matter, although it is easier to write CGI programs in some languages versus others.

Since CGI scripts need execute permissions, httpd by default only allows CGI programs in the cgi-bin directory to be run for (possibly now misguided) security purposes.

Most PHP scripts run in the web server process via mod_php. This is not CGI.

CGI is slow since the program (and related interpreter) must be started up per request. Modern alternatives are embedded execution, used by mod_php, and long-running processes, used by FastCGI. A given language may have its own way of implementing those mechanisms, so be sure to ask around before resorting to CGI.

﹏半生如梦愿梦如真 2024-08-25 15:42:57

一个现实生活中的例子:需要在网站上显示的复杂数据库。由于数据库是在 1986 年左右设计的(!),大量数据以不同的方式打包以节省磁盘空间。

随着开发的继续,开发人员不再能够仅用 SQL 解决复杂的数据请求,例如因为排序算法不寻常。

共有三种明智的解决方案:

  1. 快速但肮脏:将未排序的数据发送到 PHP,在那里对其进行排序。显然这是一个非常昂贵的解决方案,因为每次调用页面时都会重复
  2. 向数据库引擎写入插件 - 但管理员还没有准备好允许外部代码在他们的服务器上运行,或者
  3. 您可以在中处理数据程序(C、Perl 等),并输出 HTML。程序本身进入/cgi-bin,并由Web服务器(例如Apache)直接调用,而不是通过PHP。

CGI 运行解决方案 #3 中的脚本并将效果输出到浏览器。您可以享受编译程序的速度、比 SQL 更广泛的语言的灵活性,并且无需向 SQL 服务器编写插件。 (同样,这是一个特定于 SQL 和 C 的示例)

A real-life example: a complicated database that needs to be shown on a website. Since the database was designed somewhere around 1986 (!), lots of data was packed in different ways to save on disk space.

As the development went on, the developers could no longer solve complicated data requests in SQL alone, for example because the sorting algorythms were unusual.

There are three sensible solutions:

  1. quick and dirty: send the unsored data to PHP, sort it there. Obviously a very expensive solution, because this would be repeated every time the page is called
  2. write a plugin to the database engine -- but the admin wasn't ready to allow foreign code to run on their server, or
  3. you can process the data in a program (C, Perl, etc.), and output HTML. The program itself goes into /cgi-bin, and is called by the web server (e.g. Apache) directly, not through PHP.

CGI runs your script in Solution #3 and outputs the effect to the browser. You have the speed of the compiled program, the flexibility of a language broader than SQL, and no need to write plugins to the SQL server. (Again, this is an example specific to SQL and C)

ヅ她的身影、若隐若现 2024-08-25 15:42:57

CGI 脚本是控制台/外壳程序。在 Windows 中,当您使用“命令提示符”窗口时,您会执行控制台程序。当 Web 服务器执行 CGI 脚本时,它使用环境变量或“标准输入”向控制台/shell 程序提供输入。标准输入就像在控制台/shell 程序中输入数据;对于 CGI 脚本,由 Web 服务器进行输入。 CGI 脚本将数据写入“标准输出”,并且该输出作为 HTML 页面发送到客户端(Web 浏览器)。标准输出类似于您在控制台/shell 程序中看到的输出,只不过 Web 服务器读取它并将其发送出去。

CGI 脚本可以从浏览器执行。 URI 通常包括提供给 CGI 脚本的查询字符串。如果方法是“get”,则查询字符串将在名为 QUERY_STRING 的环境变量中提供给 CGI 脚本。如果方法是“post”,则使用标准输入将查询字符串提供给 CGI 脚本(CGI 脚本从标准输入读取查询字符串)。

CGI 脚本的早期用途是处理表单。在 HTML 的早期,HTML 表单通常具有“action”属性和一个指定为“submit”按钮的按钮。当按下提交按钮时,“action”属性中指定的 URI 将被发送到服务器,表单中的数据将作为查询字符串发送。如果“动作”指定一个 CGI 脚本,那么该 CGI 脚本将被执行,然后生成一个 HTML 页面。

RFC 3875“通用网关接口 (CGI)”部分使用 C 定义了 CGI,表示环境变量“由 C 库例程 getenv() 或变量 environ 访问”。

如果您正在使用 C/C++ 开发 CGI 脚本并使用 Microsoft Visual Studio 来执行此操作,那么您将开发一个控制台程序。

A CGI script is a console/shell program. In Windows, when you use a "Command Prompt" window, you execute console programs. When a web server executes a CGI script it provides input to the console/shell program using environment variables or "standard input". Standard input is like typing data into a console/shell program; in the case of a CGI script, the web server does the typing. The CGI script writes data out to "standard output" and that output is sent to the client (the web browser) as a HTML page. Standard output is like the output you see in a console/shell program except the web server reads it and sends it out.

A CGI script can be executed from a browser. The URI typically includes a query string that is provided to the CGI script. If the method is "get" then the query string is provided to the CGI Script in an environment variable called QUERY_STRING. If the method is "post" then the query string is provided to the CGI Script using standard input (the CGI Script reads the query string from standard input).

An early use of CGI scripts was to process forms. In the beginning of HTML, HTML forms typically had an "action" attribute and a button designated as the "submit" button. When the submit button is pushed the URI specified in the "action" attribute would be sent to the server with the data from the form sent as a query string. If the "action" specifies a CGI script then the CGI script would be executed and it then produces a HTML page.

RFC 3875 "The Common Gateway Interface (CGI)" partially defines CGI using C, as in saying that environment variables "are accessed by the C library routine getenv() or variable environ".

If you are developing a CGI script using C/C++ and use Microsoft Visual Studio to do that then you would develop a console program.

ぶ宁プ宁ぶ 2024-08-25 15:42:57

您可能想知道什么不是 CGI,答案是您的 Web 服务器的模块(如果我假设您运行的是 Apache)。这就是很大的区别,因为 CGI 需要外部程序、线程等来实例化 PERL、PHP、C 应用程序服务器,而当您作为模块运行时,该程序本身就是 Web 服务器(apache)。

因此,存在许多性能、安全性、可移植性问题。但最好先知道什么不是 CGI,然后再了解它是什么。

You maybe want to know what is not CGI, and the answer is a MODULE for your web server (if I suppose you are runnig Apache). AND THAT'S THE BIG DIFERENCE, because CGI needs and external program, thread, whatever to instantiate a PERL, PHP, C app server where when you run as a MODULE that program is the web server (apache) per-se.

Because of all this there is a lot of performance, security, portability issues that come into play. But it's good to know what is not CGI first, to understand what it is.

明天过后 2024-08-25 15:42:57

CGI 背后的想法是,程序/脚本(无论是 Perl 还是 C)通过 STDIN(请求数据)接收输入,并通过 STDOUT 输出数据(echo、>printf 语句)。

大多数 PHP 脚本不合格的原因是它们在 PHP Apache 模块下运行。

The idea behind CGI is that a program/script (whether Perl or even C) receives input via STDIN (the request data) and outputs data via STDOUT (echo, printf statements).

The reason most PHP scripts don't qualify is that they are run under the PHP Apache module.

仅此而已 2024-08-25 15:42:57

CGI 是您编写的程序(或 Web API),并将其保存在 Web 服务器站点上。 CGI是一个文件。

该文件位于 Web 服务器上并等待。当客户端浏览器向Web服务器发送执行您的CGI文件的请求时,Web服务器在服务器站点上运行您的CGI文件。此 CGI 程序的输入(如果有)来自客户端浏览器。该 CGI 程序的输出被发送到浏览器。

你用什么语言来编写 CGI 程序?其他帖子已经提到了c、java、php、perl等。

A CGI is a program (or a Web API) you write, and save it on the Web Server site. CGI is a file.

This file sits and waits on the Web Server. When the client browser sends a request to the Web Server to execute your CGI file, the Web Server runs your CGI file on the server site. The inputs for this CGI program, if any, are from the client browser. The outputs of this CGI program are sent to the browser.

What language you use to write a CGI program? Other posts already mention c,java, php, perl, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文