Configure a scheme for nginx that supports cgi scripts under ubuntu

  • 2020-05-13 04:28:23
  • OfStack

Support for cgi scripts under nginx is similar to support for node, just do a direct forward on nginx and forward to the corresponding cgi socket.

Using Fcgiwrap

Fcgiqwrap is another CGI package library, similar to Simple CGI.

Install fcgiwrap

apt-get install fcgiwrap

Installed fcgiwrap default has been launched, the corresponding socket is/var run/fcgiwrap socket. If there is no start, use/etc/init d/fcgiwrap manual start.

Configure the vhost file for nginx

Under the path to support the cgi script, add the corresponding server configuration. For example, all cgi are in the cgi-bin path:


server {
[...]
  location /cgi-bin/ {
   # Disable gzip (it makes scripts feel slower since they have to complete
   # before getting gzipped)
   gzip off;
   # Set the root to /usr/lib (inside this location this means that we are
   # giving access to the files under /usr/lib/cgi-bin)
   root /var/www/www.example.com;
   # Fastcgi socket
   fastcgi_pass unix:/var/run/fcgiwrap.socket;
   # Fastcgi parameters, include the standard ones
   include /etc/nginx/fastcgi_params;
   # Adjust non standard parameters (SCRIPT_FILENAME)
   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }
[...]
}

Reload nginx:

nginx -s reload

test

Create hello-world.cgi under cgi-bin


#!/usr/bin/perl -w
   # Tell perl to send a html header.
   # So your browser gets the output
   # rather then <stdout>(command line
   # on the server.)
print "Content-type: text/html\n\n";
   # print your basic html tags.
   # and the content of them.
print "<html><head><title>Hello World!! </title></head>\n";
print "<body><h1>Hello world</h1></body></html>\n";

Set execution permissions

chmod 755 /var/www/www.example.com/cgi-bin/hello_world.cgi

Open the corresponding script in the browser and you will see that it has been configured successfully! http: / / www. example. com/cgi - bin/hello_world cgi


Related articles: