CGI Bash con Nginx y Docker

Bash CGI con Nginx

Sigo Serving CGI Scripts With Nginx On Debian Squeeze/Ubuntu 11.04 - Page 3 - Page 3

FROM nginx:latest

RUN apt-get update
RUN apt-get install -y fcgiwrap
RUN update-rc.d fcgiwrap defaults 20
RUN sed -i 's/www-data/nginx/g' /etc/init.d/fcgiwrap

COPY default.conf /etc/nginx/conf.d
COPY hello.cgi /usr/share/nginx/cgi-bin/hello.cgi
RUN  chmod +x  /usr/share/nginx/cgi-bin/hello.cgi

EXPOSE 80
CMD /etc/init.d/fcgiwrap start && nginx -g 'daemon off;'

y el archivo default.conf

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    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/share/nginx
         # (inside this location this means that we are
         # giving access to the files under /usr/share/nginx/cgi-bin)
         root /usr/share/nginx;
         # 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;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}

y un cgi de prueba:

#!/bin/bash
# Construir los encabezados HTTP
echo "Content-type: text/html"
echo ""
# Y ahora el BODY del mensaje HTTP
echo "<html><head><title>Hello World!! </title></head>";
echo "<body><h1>Hello world</h1></body></html>";

worker:

#!/bin/bash

BLOCK="Hola caracola"

NONCE=$1
RESULT=999
while echo -n "$RESULT" | grep ^000 -q -v
do
    RESULT=$(echo $BLOCK:$NONCE | shasum -a 512)
    NONCE=$(expr $NONCE + 1)
done
echo Hash: $RESULT
echo Nonce: $NONCE
.
โ”œโ”€โ”€ bashwebapp
โ”‚   โ”œโ”€โ”€ app
โ”‚   โ”‚   โ”œโ”€โ”€ hello.cgi
โ”‚   โ”‚   โ”œโ”€โ”€ mine.cgi
โ”‚   โ”‚   โ””โ”€โ”€ worker.sh
โ”‚   โ”œโ”€โ”€ default.conf
โ”‚   โ””โ”€โ”€ Dockerfile
โ”œโ”€โ”€ docker-compose.yml
#!/bin/bash
# Construir los encabezados HTTP
echo "Content-type: application/json"
echo ""
# Obtener de la QUERY el bloque a minar
# ?bloque=bloqueaminar
# Minado

. ./worker.sh

BLOQUE=$(echo $QUERY_STRING | cut -d= -f2)
RESULT=$(minar $BLOQUE)

# Devolver respuesta en el BODY del mensaje JSON
cat << FIN
{
	"bloque": "$BLOQUE",
	"hash": "$RESULT"
}
FIN
function minar { BLOCK=$1 NONCE=$RANDOM RESULT=999 while echo -n "$RESULT" | grep ^0000 -q -v do RESULT=$(echo $BLOCK:$NONCE | sha512sum ) NONCE=$(expr $NONCE + 1) done echo $(echo $RESULT | cut -d'-' -f1) }