<!-- review: finished -->

<a id="external-cgi"></a>

# CGI

该模块添加了对 CGI 的支持。

需要注意的是,CGI **不** 适用于:

- 高 QPS;
- 大流量;
- 高并发。

<a id="installation-6"></a>

## 安装

要 [安装](https://cn.angie.software//angie/docs/installation/index.md#install-packages) 该模块,请使用以下软件包之一:

- Angie:`angie-module-cgi`
- Angie PRO:`angie-pro-module-cgi`

<a id="loading-the-module-5"></a>

## 加载模块

在 `main{}` 上下文中加载模块:

```nginx
load_module modules/ngx_http_cgi_module.so;
```

<a id="configuration-example-82"></a>

## 配置示例

```nginx
server {
    listen 80;

    root /usr/share/angie/html;
    index index.html index.htm;

    location /cgi {
        alias /usr/share/angie/cgi-bin;
        cgi on;
    }
}
```

<a id="test-script"></a>

## 测试脚本

一个示例测试可执行脚本 `test.sh`:

```bash
#!/bin/sh
echo "Content-Type: text/plain" # Add header to the response
echo "" # Separator between headers and body of the response

# Environment variables
echo "query string: $QUERY_STRING"
echo "server addr: $SERVER_ADDR"
echo "server port: $SERVER_PORT"

# Request headers via environment variables
echo "http host: $HTTP_HOST"
echo "http accept: $HTTP_ACCEPT"
echo "http Some-Field: $HTTP_SOME_FIELD"

body=$(cat) # Reads the request body into a variable
echo "Request body: $body"
```

<a id="placing-the-script"></a>

## 放置脚本

根据配置,脚本必须放置在 `/usr/share/angie/cgi-bin/` 目录中。
该文件必须具有读取和执行权限。

<a id="example-of-request-execution"></a>

## 请求执行示例

```console
$ curl  -H 'Some-Field:some text' -d '{"key1":"value1", "key2":"value2"}' -i \
  'http://127.0.0.1/cgi/hello.sh?a=valueA&b=valueB'

HTTP/1.1 200 OK
Server: Angie/|angie_version|
Date: |sampledatelong| 19:15:35 GMT
Transfer-Encoding: chunked
Connection: keep-alive
Content-Type: text/plain

query string: a=valueA&b=valueB
server addr: 127.0.0.1
server port: 80
http host: 127.0.0.1
http accept: */*
http Some-Field: some text
Request body: {"key1":"value1", "key2":"value2"}
```

<a id="additional-information-6"></a>

## 其他信息

详细文档和源代码可在以下位置获取:
[https://github.com/pjincz/nginx-cgi](https://github.com/pjincz/nginx-cgi)
