<!-- review: finished -->

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

# CGI

The module adds support for CGI.

It is important to note that CGI **is not** suitable for:

- high QPS;
- heavy traffic;
- high concurrency.

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

## Installation

To [install](https://en.angie.software//angie/docs/installation/index.md#install-packages) the module, use one of the
following packages:

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

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

## Loading the Module

To load the module in the `main{}` context:

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

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

## Configuration Example

```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 Script

An example test executable script `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>

## Placing the Script

According to the configuration, the script must be placed in the
`/usr/share/angie/cgi-bin/` directory.
The file must have read and execute permissions.

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

## Example of Request Execution

```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>

## Additional Information

Detailed documentation and source code are available at:
[https://github.com/pjincz/nginx-cgi](https://github.com/pjincz/nginx-cgi)
