Nginx: Map a single URI to a single CGI script
Debian
install software
Simple configuration to map a single location to a a single cgi script.
Note: this is not your typical /cgi-bin
folder configuration.
1sudo apt install fcgiwrap nginx
2sudo systemctl enable fcgiwrap.service
3sudo systemctl start fcgiwrap.service
nginx configuration
Most of the configuration is already present as an include
in /etc/nginx/fastcgi.conf
. So in your serve config add the following:
1server {
2 # ...
3
4 # cgi configuration
5 location /uri/to/script {
6 gzip off;
7 root /path/to/base/folder/cgi-bin;
8 fastcgi_pass unix:/var/run/fcgiwrap.socket;
9
10 # configure script paths and uri
11 fastcgi_param SCRIPT_FILENAME $document_root/script.cgi;
12 fastcgi_param SCRIPT_NAME /script.cgi;
13 fastcgi_param REQUEST_URI /script.cgi;
14 fastcgi_param DOCUMENT_URI /script.cgi;
15 fastcgi_param QUERY_STRING $query_string;
16 fastcgi_param REQUEST_METHOD $request_method;
17 fastcgi_param CONTENT_TYPE $content_type;
18 fastcgi_param CONTENT_LENGTH $content_length;
19 }
20
21 # ...
22}
To make the above mapping to a single script work, you must place your script at this specific folder inside the document root:
/path/to/base/folder/cgi-bin/script.cgi
Make it executable for the web server:
1chgrp www-data /path/to/base/folder/cgi-bin/script.cgi
2chmod 750 /path/to/base/folder/cgi-bin/script.cgi
et voilà!
This configuration will mapp https://server.tld/uri/to/script
→ /path/to/base/folder/cgi-bin/script.cgi