Development Tip

CGI (Common Gateway Interface) 란 무엇입니까?

yourdevel 2020. 9. 29. 18:49
반응형

CGI (Common Gateway Interface) 란 무엇입니까?


CGI는 공통 게이트웨이 인터페이스입니다. 이름에서 알 수 있듯이 모든 것을위한 "공통"게이트웨이 인터페이스입니다. 이름에서 너무 사소하고 순진합니다. 나는 이것을 이해하고이 단어를 만날 때마다 이것을 느꼈다. 하지만 솔직히 그렇게하지 않았습니다. 아직도 혼란 스러워요.

저는 웹 개발 경험이있는 PHP 프로그래머입니다.

페이지에 대한 사용자 (클라이언트) 요청 ---> webserver (-> embedded PHP interpreter) ----> Server side (PHP) Script ---> MySQL Server.

이제 내 PHP 스크립트가 MySQL 서버 및 MATLAB 서버 및 다른 서버에서 결과를 가져올 수 있다고 가정합니다.

이제 PHP 스크립트가 CGI입니까? 웹 서버와 다른 모든 서버 간의 인터페이스 때문입니까? 모르겠어요. 때때로 그들은 CGI, 기술 및 다른 경우 CGI를 프로그램 또는 다른 서버라고 부릅니다.

  • CGI는 정확히 무엇입니까?

  • 큰 거래는 /cgi-bin/*.cgi무엇입니까? 이게 무슨 일이야? cgi-bin서버 에서이 디렉토리 가 무엇인지 모르겠습니다 . * .cgi 확장자가있는 이유를 모르겠습니다.

  • Perl이 항상 방해가되는 이유는 무엇입니까? CGI 및 Perl (언어). 나는 또한이 두 사람이 무슨 일인지 모릅니다. 거의 항상 "CGI와 Perl"의 조합으로이 두 가지를 계속 듣습니다. 이 책은 Perl을 사용한 CGI 프로그래밍의 또 다른 훌륭한 예 입니다. "PHP / JSP / ASP를 사용한 CGI 프로그래밍"이 아닌 이유는 무엇입니까? 나는 그런 것을 본 적이 없습니다.

  • C의 CGI 프로그래밍 은 저를 많이 혼란스럽게합니다. " in C "?? 진심 ?? 나는 무엇을 말해야할지 모르겠다. 그냥 혼란 스러워요. " in C "?? 이것은 모든 것을 바꿉니다. 프로그램을 컴파일하고 실행해야합니다. 이것은 웹 프로그래밍에 대한 나의 견해를 완전히 바꿉니다. 언제 컴파일합니까? 프로그램은 어떻게 실행됩니까 (기계 코드이므로 독립적 인 프로세스로 실행되어야하기 때문). 웹 서버와 어떻게 통신합니까? IPC? 소켓 프로그래밍을 사용하여 모든 서버 (제 예제 MATLAB 및 MySQL에서)와 상호 작용합니까? 나는 길을 잃었다 !!

  • 사람들은 CGI가 더 이상 사용되지 않으며 더 이상 사용되지 않는다고 말합니다. 그렇습니까? 최신 업데이트는 무엇입니까?

한 번은 웹 서버 (Apache HTTPD)에 대한 HTTP PUT 요청 액세스 권한을 부여해야하는 상황에 직면했습니다. 긴 등. 그래서 내가 기억하는 한 이것은 내가 한 일입니다.

  1. 웹 서버에 모든 HTTP PUT 요청을 일부에게 전달하도록 Apache HTTPD의 구성 파일을 편집했습니다 put.php(이 PHP 스크립트를 작성해야 함).

  2. 요청을 처리하기 위해 put.php를 구현하십시오 (언급 된 위치에 파일을 저장하십시오).

사람들은 내가 CGI 스크립트를 작성했다고 말했습니다. 진지하게, 나는 그들이 말하는 것에 대한 단서가 없었습니다.

  • 정말 CGI 스크립트를 작성 했습니까?

내 혼란이 무엇인지 이해하시기 바랍니다. (나는 내가 어디에서 혼란스러워하는지 모르기 때문에). 가능한 한 간단하게 답변 해 주시기 바랍니다. 정말 멋진 기술 용어를 이해할 수 없습니다. 적어도이 경우에는 그렇지 않습니다.

편집하다:

이 놀라운 튜토리얼 "CGI 프로그래밍은 간단합니다!" - 가장 간단한 방법으로 개념을 설명하는 CGI Tutorial . 이 기사를 읽은 후 실제 코드 샘플로 이해를 보완하기 위해 C에서 CGI 프로그래밍 시작하기 를 읽을 수 있습니다 . 이 튜토리얼에 대한 링크를 Wikipedia의 기사에 추가했습니다. http://en.wikipedia.org/wiki/Common_Gateway_Interface


CGI는 웹 서버에 응용 프로그램에서 데이터를 전달하는 방법을 알려주는 인터페이스입니다. 보다 구체적으로, 요청 정보가 환경 변수 (예 : 요청 유형, 원격 IP 주소)에 전달되는 방법, 요청 본문이 표준 입력을 통해 전달되는 방법 및 응답이 표준 출력을 통해 전달되는 방법을 설명합니다. 자세한 내용 CGI 사양 을 참조하십시오.

이미지를 사용하려면 :

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

전부는 아니지만 대부분의 웹 서버는 프로그램을 'CGI'로 실행하도록 구성 할 수 있습니다. 즉, 웹 서버는 요청을 받으면 데이터를 특정 프로그램으로 전달하여 일부 환경 변수를 설정하고 표준 입력 및 표준 출력을 통해 매개 변수를 마샬링하여 프로그램이 찾을 위치와 대상을 알 수 있습니다.

주요 이점은 웹 서버와 프로그램이 CGI의 작동 방식을 알고 있기 때문에 웹에서 실행 가능한 코드를 실행할 수 있다는 것입니다. 그렇기 때문에 일반 CGI 지원 웹 서버를 사용하여 C 또는 Bash로 웹 프로그램을 작성할 수 있습니다. 그리고 대부분의 프로그래밍 환경은 표준 입력, 표준 출력 및 환경 변수를 쉽게 사용할 수 있습니다.

귀하의 경우에는 스크립트와 웹 서버 사이의 통신 수단 인 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 프로그래밍"은 어떻습니까? 나는 그런 것을 본 적이 없습니다.

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.


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


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.


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.


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.


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.


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.


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.


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 better than SQL, and no need to write plugins to the SQL server. (Again, this is an example specific to SQL and C)


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.


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.


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 because they are run under the PHP Apache module.

참고URL : https://stackoverflow.com/questions/2089271/what-is-common-gateway-interface-cgi

반응형