# Nginx serving R scripts via CGI/FastRWeb

**URL:** <https://www.ruby-forum.com/t/nginx-serving-r-scripts-via-cgi-fastrweb/226726>\
**Category:** NGINX\
**Created:** [February 19, 2013, 1:49pm UTC](https://www.ruby-forum.com/t/nginx-serving-r-scripts-via-cgi-fastrweb/226726 "2013-02-19T13:49:36Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![iamstef](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/6a8cbe/32.png) [@iamstef](https://www.ruby-forum.com/u/iamstef)\
**Post date:** [February 19, 2013, 1:49pm UTC](https://www.ruby-forum.com/t/nginx-serving-r-scripts-via-cgi-fastrweb/226726/1 "2013-02-19T13:49:36Z")

</div>

Hi,

Anyone here testing, experimenting with R and nginx ?  
Im trying to setup nginx to serve R scripts via  
CGI using Rserve, FastRWeb modules as described here:

> **[Setting up FastRWeb/Rserve on Ubuntu](http://jayemerson.blogspot.com/2011/10/setting-up-fastrwebrserve-on-ubuntu.html)**
>
> This blog entry documents my recent (successful) attempt to use Simon Urbanek's Rserve and FastRWeb for CGI scripting with R.  This is a wor...

My nginx is configured like:

```
    location ~ ^/cgi-bin/.*\.cgi$ {
        gzip off;
        fastcgi_pass 

```

unix:/opt/sdr/report/ws/fastcgi\_temp/nginx-fcgi.sock;  
fastcgi\_read\_timeout 5m;  
fastcgi\_index index.cgi;  
#fastcgi\_buffers 8 4k;  
#  
# You may copy and paste the lines under or use include  
directive  
# include /etc/nginx/nginx-fcgi.conf;  
# In this example all is in one file  
#  
fastcgi\_param SCRIPT\_NAME $fastcgi\_script\_name;  
fastcgi\_param QUERY\_STRING $query\_string;  
fastcgi\_param REQUEST\_METHOD $request\_method;  
fastcgi\_param CONTENT\_TYPE $content\_type;  
fastcgi\_param CONTENT\_LENGTH $content\_length;  
fastcgi\_param GATEWAY\_INTERFACE CGI/1.1;  
fastcgi\_param SERVER\_SOFTWARE nginx;  
fastcgi\_param SCRIPT\_NAME $fastcgi\_script\_name;  
fastcgi\_param REQUEST\_URI $request\_uri;  
fastcgi\_param DOCUMENT\_URI $document\_uri;  
fastcgi\_param DOCUMENT\_ROOT /opt/sdr/report/docroot;  
fastcgi\_param SERVER\_PROTOCOL $server\_protocol;  
fastcgi\_param REMOTE\_ADDR $remote\_addr;  
fastcgi\_param REMOTE\_PORT $remote\_port;  
fastcgi\_param SERVER\_ADDR $server\_addr;  
fastcgi\_param SERVER\_PORT $server\_port;  
fastcgi\_param SERVER\_NAME $server\_name;  
}

and Im using fcgiwrap from [http://nginx.localdomain.pl/wiki/FcgiWrap](http://nginx.localdomain.pl/wiki/FcgiWrap).  
CGI scripts work fine but Im not able to any R scripts probable due  
R being not correctly called via nginx.conf …

I am trying something like: [http://localhost/cgi-bin/R/foo.png?n=500](http://localhost/cgi-bin/R/foo.png?n=500)  
and R is a binary file under cgi-bin directory which should call  
FastRWeb …

Any pointers, help ?

thanks,  
Stefan

---

<div class="post-metadata">

**Author:** ![iamstef](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/6a8cbe/32.png) [@iamstef](https://www.ruby-forum.com/u/iamstef)\
**Post date:** [February 19, 2013, 2:50pm UTC](https://www.ruby-forum.com/t/nginx-serving-r-scripts-via-cgi-fastrweb/226726/2 "2013-02-19T14:50:58Z")

</div>

Does the R cgi script filename end in .cgi ? That’s what you specify, it  
appears.

On Tue, Feb 19, 2013 at 7:47 AM, Stefan P.  
\<removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![iamstef](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/6a8cbe/32.png) [@iamstef](https://www.ruby-forum.com/u/iamstef)\
**Post date:** [February 19, 2013, 3:51pm UTC](https://www.ruby-forum.com/t/nginx-serving-r-scripts-via-cgi-fastrweb/226726/3 "2013-02-19T15:51:44Z")

</div>

On 08:50 Tue 19 Feb , Harold Sinclair wrote:

> Does the R cgi script filename end in .cgi ? That’s what you specify, it  
> appears.

No, it does not end with cgi since the R scripts will not be called  
directly  
by nginx.

As I understood I should call the R scripts like:  
[http://localhost/cgi-bin/R/foo](http://localhost/cgi-bin/R/foo) where  
there is a R binary file under cgi-bin which will proxy those to  
FastRWeb  
and Rserve R modules. So under cgi-bin there are no R scripts  
whatsoever,  
but they usually will go under: /opt/sdr/report/var/FastRWeb/web.R  
directory.

Now my problem seems to be related how the R binary under cgi-bin will  
ever be called via CGI and further FastRWeb … since my nginx.conf  
knows nada  
about R being called. Currently my nginx.conf is configured to run  
anything ending .cgi as cgi scripts …

/opt/sdr/report/docroot/cgi-bin  
$ ls -lrt  
total 60  
-rwxr-x— 1 sdr sdr 3715 Feb 19 12:00 initial.cgi  
-rwxr-xr-x 1 sdr sdr 55932 Feb 19 13:53 R

$ file R  
R: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically  
linked (uses shared libs), for GNU/Linux 2.6.24,  
BuildID[sha1]=0x0351c94aaf487ee3559a722446865b7ae0f3b7cc, not stripped

Here the R scripts:

/opt/sdr/report/var/FastRWeb  
$ ls -lrt  
total 16  
drwxr-xr-x 2 sdr sdr 4096 Feb 18 00:08 web  
drwxr-xr-x 2 sdr sdr 4096 Feb 18 00:08 tmp  
drwxr-xr-x 2 sdr sdr 4096 Feb 19 14:27 code  
srw-rw-rw- 1 sdr sdr 0 Feb 19 14:27 socket  
=\>\> drwxr-xr-x 2 sdr sdr 4096 Feb 19 14:28 web.R

all R scripts will go here web.R directory.

stefan

---

<div class="post-metadata">

**Author:** ![iamstef](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/6a8cbe/32.png) [@iamstef](https://www.ruby-forum.com/u/iamstef)\
**Post date:** [February 19, 2013, 5:41pm UTC](https://www.ruby-forum.com/t/nginx-serving-r-scripts-via-cgi-fastrweb/226726/4 "2013-02-19T17:41:03Z")

</div>

On 11:22 Tue 19 Feb , Harold Sinclair wrote:

> Sounds like you ought to try writing a wrapper script ending in .cgi in the  
> cgi dir that grabs the query string and hands the job off to the R  
> executable. Not sure if R is required in the cgi-bin directory. You might  
> have to enable symlinking out for it to work.

yeah not sure how to do it. FastRWeb tell us:

" FastRWeb consists of several parts:

```
Webserver-to-R pipeline, consisting of either a thin CGI or a PHP 

```

client connecting to Rserve which sources and runs the R script. The CGI  
client is called Rcgi and is compiled as part of the package. The PHP  
client is part of Rserve in the clients section.  
"

So Im on option 1: CGI. The client will be this binary which somehow  
needs to be  
executed for each R script. I placed the Rcgi under cgi.bin directory  
and rename it  
as R.cgi.

If I call directly:

[http://localhost:9001/cgi-bin/R.cgi](http://localhost:9001/cgi-bin/R.cgi) the browser returns:  
Error: no function or path specified.

[http://localhost:9001/cgi-bin/R.cgi/foo.png?n=100](http://localhost:9001/cgi-bin/R.cgi/foo.png?n=100)  
I see under error log:  
2013/02/19 18:39:53 [error] 2847#0: \*56 open()  
“/opt/sdr/report/docroot/cgi-bin/R.cgi/foo.png” failed (20: Not a  
directory), client: 127.0.0.1, server: sdrrep, request: “GET  
/cgi-bin/R.cgi/foo.png?n=100 HTTP/1.1”, host: “localhost:9001”

stefan

---

<div class="post-metadata">

**Author:** ![iamstef](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/6a8cbe/32.png) [@iamstef](https://www.ruby-forum.com/u/iamstef)\
**Post date:** [February 19, 2013, 5:22pm UTC](https://www.ruby-forum.com/t/nginx-serving-r-scripts-via-cgi-fastrweb/226726/5 "2013-02-19T17:22:55Z")

</div>

Sounds like you ought to try writing a wrapper script ending in .cgi in  
the  
cgi dir that grabs the query string and hands the job off to the R  
executable. Not sure if R is required in the cgi-bin directory. You  
might  
have to enable symlinking out for it to work.

On Tue, Feb 19, 2013 at 9:49 AM, Stefan P.  
\<removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![iamstef](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/6a8cbe/32.png) [@iamstef](https://www.ruby-forum.com/u/iamstef)\
**Post date:** [February 19, 2013, 5:44pm UTC](https://www.ruby-forum.com/t/nginx-serving-r-scripts-via-cgi-fastrweb/226726/6 "2013-02-19T17:44:34Z")

</div>

> So Im on option 1: CGI. The client will be this binary which somehow needs to be  
> executed for each R script. I placed the Rcgi under cgi.bin directory and rename  
> it  
> as R.cgi.

and I forgot to mention for CGI Im using FcgiWrap:  
[http://nginx.localdomain.pl/wiki/FcgiWrap](http://nginx.localdomain.pl/wiki/FcgiWrap)

stefan

---

<div class="post-metadata">

**Author:** ![iamstef](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/6a8cbe/32.png) [@iamstef](https://www.ruby-forum.com/u/iamstef)\
**Post date:** [February 19, 2013, 6:24pm UTC](https://www.ruby-forum.com/t/nginx-serving-r-scripts-via-cgi-fastrweb/226726/7 "2013-02-19T18:24:54Z")

</div>

On Tue, Feb 19, 2013 at 12:47:23PM +0000, Stefan P. wrote:

Hi there,

This location:

> ```
> location ~ ^/cgi-bin/.*\.cgi$ {
> 
> ```

will only match some requests that end in “.cgi” (before the ?, if  
that applies).

What you possibly want is a separate location just for your “R”  
requests.

Something like (untested by me!)

location ^~ /cgi-bin/R/ { }

in which you have your “fastcgi\_pass” directive, plus whatever  
“fastcgi\_param” directives you need to make it work. That is _probably_

fastcgi\_param REQUEST\_URI $request\_uri;  
fastcgi\_param QUERY\_STRING $query\_string;  
fastcgi\_param SCRIPT\_FILENAME $document\_root/cgi-bin/R;

but the exact details depend on what your fastcgi server wants.

(You may want to set PATH\_INFO, if your application uses that.)

See [Module ngx\_http\_core\_module](http://nginx.org/r/location) for the details of which one location{}  
is chosen for each request, and then see the fastcgi documentation to  
decide exactly what params you need.

Usually, SCRIPT\_FILENAME is “the file on the filesystem that the fastcgi  
server should execute”; and other things are “stuff that the fastcgi  
server or the application can use to decide what to do”.

> and Im using fcgiwrap from [http://nginx.localdomain.pl/wiki/FcgiWrap](http://nginx.localdomain.pl/wiki/FcgiWrap).  
> CGI scripts work fine but Im not able to any R scripts probable due  
> R being not correctly called via nginx.conf …

In this case, yes. Your nginx configuration was such that your requests  
for “R” were not being sent to the fastcgi server.

> I am trying something like: [http://localhost/cgi-bin/R/foo.png?n=500](http://localhost/cgi-bin/R/foo.png?n=500)  
> and R is a binary file under cgi-bin directory which should call  
> FastRWeb …

Provided that the fastcgi server is able to run whatever file you name  
in  
SCRIPT\_FILENAME, and has access to whatever other params it cares about,  
something like the above configuration has a chance of working.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![iamstef](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/6a8cbe/32.png) [@iamstef](https://www.ruby-forum.com/u/iamstef)\
**Post date:** [February 20, 2013, 2:25pm UTC](https://www.ruby-forum.com/t/nginx-serving-r-scripts-via-cgi-fastrweb/226726/8 "2013-02-20T14:25:36Z")

</div>

On Wed, Feb 20, 2013 at 10:00:04AM +0000, Stefan P. wrote:

> > Something like (untested by me!)  
> > location ^~ /cgi-bin/R/ { }

> yep correct. I do have now on cgi-bin directory a binary file called: R.  
> I did change my nginx.conf to have a new location definition for the  
> R calls, like here:
> 
> ```
> location ~ ^/cgi-bin/R$ {
> 
> ```

“~” means “regex”.

“^” means “start of string”.

“$” means “end of string”.

This location will only match requests that are

/cgi-bin/R

or

/cgi-bin/R?something

and not

/cgi-bin/R/something

> But when I try: [http://localhost:9001/cgi-bin/R/foo?n=50](http://localhost:9001/cgi-bin/R/foo?n=50)  
> this returns with same error that there is no such directory, seems somehow  
> the request it is not passed via CGI to FastWebR.

Yes, that’s what you configured.

What happens when you test exactly what was suggested?

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![iamstef](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/6a8cbe/32.png) [@iamstef](https://www.ruby-forum.com/u/iamstef)\
**Post date:** [February 20, 2013, 11:02am UTC](https://www.ruby-forum.com/t/nginx-serving-r-scripts-via-cgi-fastrweb/226726/9 "2013-02-20T11:02:19Z")

</div>

> Something like (untested by me!)  
> location ^~ /cgi-bin/R/ { }

yep correct. I do have now on cgi-bin directory a binary file called: R.  
I did change my nginx.conf to have a new location definition for the  
R calls, like here:

```
    location ~ ^/cgi-bin/R$ {
        gzip off;
        fastcgi_pass 

```

unix:/opt/sdr/report/ws/fastcgi\_temp/nginx-fcgi.sock;  
fastcgi\_param REQUEST\_METHOD $request\_method;  
fastcgi\_param SCRIPT\_FILENAME  
/opt/sdr/report/docroot$fastcgi\_script\_name;  
fastcgi\_param PATH\_INFO $fastcgi\_path\_info;

```
        fastcgi_param REQUEST_URI $request_uri;
        fastcgi_param DOCUMENT_URI $document_uri;
        fastcgi_param DOCUMENT_ROOT /opt/sdr/report/docroot;
        fastcgi_param SERVER_PROTOCOL $server_protocol;
        fastcgi_param REMOTE_ADDR $remote_addr;
        fastcgi_param REMOTE_PORT $remote_port;
        fastcgi_param SERVER_ADDR $server_addr;
        fastcgi_param SERVER_PORT $server_port;
        fastcgi_param SERVER_NAME $server_name;
    }

```

This works if I put in my browser:  
[http://localhost:9001/cgi-bin/R](http://localhost:9001/cgi-bin/R) I get some error in the browser but it  
does  
seem to work since I did not input any parameters to R call.

But when I try: [http://localhost:9001/cgi-bin/R/foo?n=50](http://localhost:9001/cgi-bin/R/foo?n=50)  
this returns with same error that there is no such directory, seems  
somehow  
the request it is not passed via CGI to FastWebR.

2013/02/20 11:52:01 [error] 3637#0: \*9 open()  
“/opt/sdr/report/docroot/cgi-bin/R/foo” failed (20: Not a directory),  
client:  
127.0.0.1, server: sdrrep, request: “GET /cgi-bin/R/foo?n=50 HTTP/1.1”,  
host:  
“localhost:9001”

> In this case, yes. Your nginx configuration was such that your requests  
> for “R” were not being sent to the fastcgi server.

And still they are not passed correctly.  
something still messed up with my nginx.conf

you can see entire nginx.conf here:  
[Page not found](http://www.systemdatarecorder.org/nginx.conf)

Stefan

---

<div class="post-metadata">

**Author:** ![iamstef](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/6a8cbe/32.png) [@iamstef](https://www.ruby-forum.com/u/iamstef)\
**Post date:** [February 20, 2013, 9:30pm UTC](https://www.ruby-forum.com/t/nginx-serving-r-scripts-via-cgi-fastrweb/226726/10 "2013-02-20T21:30:38Z")

</div>

> /cgi-bin/R/something

yep. silly me, I do have a location for all /cgi-bin/R/ calls, like:

```
    location ~ ^/cgi-bin/R/ {
        gzip off;
        fastcgi_pass 

```

unix:/opt/sdr/report/ws/fastcgi\_temp/nginx-fcgi.sock;  
fastcgi\_index index.cgi;  
fastcgi\_param SCRIPT\_FILENAME  
/opt/sdr/report/docroot/$fastcgi\_script\_name;  
fastcgi\_param PATH\_INFO $fastcgi\_path\_info;  
fastcgi\_param QUERY\_STRING $query\_string;  
fastcgi\_param REQUEST\_METHOD $request\_method;  
fastcgi\_param CONTENT\_TYPE $content\_type;  
fastcgi\_param CONTENT\_LENGTH $content\_length;  
fastcgi\_param REQUEST\_URI $request\_uri;  
fastcgi\_param DOCUMENT\_URI $document\_uri;  
fastcgi\_param DOCUMENT\_ROOT /opt/sdr/report/docroot;  
}

and another one for anything \*.cgi:

```
    location ~ ^/cgi-bin/.*\.cgi$ {
        gzip off;
        fastcgi_pass 

```

unix:/opt/sdr/report/ws/fastcgi\_temp/nginx-fcgi.sock;  
fastcgi\_read\_timeout 5m;  
fastcgi\_index index.cgi;  
#fastcgi\_buffers 8 4k;  
#  
# You may copy and paste the lines under or use include  
directive  
# include /etc/nginx/nginx-fcgi.conf;  
# In this example all is in one file  
…  
}

since under my /cgi-bin/ I can have lots of cgi scripts and the  
executable R cgi script.

Testing any calls for /cgi-bin/R/foo returns:  
Cannot get script name, are DOCUMENT\_ROOT and SCRIPT\_NAME (or  
SCRIPT\_FILENAME) set and is the script executable?  
Cannot get script name, are DOCUMENT\_ROOT and SCRIPT\_NAME (or  
SCRIPT\_FILENAME) set and is the script executable?

Im trying to see if fastcgi\_split\_path\_info might help anything.

stefan

---

<div class="post-metadata">

**Author:** ![iamstef](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/6a8cbe/32.png) [@iamstef](https://www.ruby-forum.com/u/iamstef)\
**Post date:** [February 20, 2013, 10:44pm UTC](https://www.ruby-forum.com/t/nginx-serving-r-scripts-via-cgi-fastrweb/226726/11 "2013-02-20T22:44:55Z")

</div>

On Wed, Feb 20, 2013 at 08:28:27PM +0000, Stefan P. wrote:

> ```
> location ~ ^/cgi-bin/R/ {
> 
> ```

That will match anything starting with /cgi-bin/R/, which is (most of)  
what you want.

These requests will…

> ```
> fastcgi_pass unix:/opt/sdr/report/ws/fastcgi_temp/nginx-fcgi.sock;
> 
> ```

be sent to the fastcgi server.

Now, for all of these requests, you want to tell the fastcgi server to  
use the file /opt/sdr/report/docroot/cgi-bin/R. So:

> ```
> fastcgi_index index.cgi;
> 
> ```

you don’t need that, since it will never apply; and

> ```
> fastcgi_param SCRIPT_FILENAME 
> 
> ```

/opt/sdr/report/docroot/$fastcgi\_script\_name;

that should be something that expands to  
/opt/sdr/report/docroot/cgi-bin/R. Which is probably  
$document\_root/cgi-bin/R; you can use whatever matches exactly that  
filename. $fastcgi\_script\_name does not have the correct value by  
default,  
in this case.

> ```
> fastcgi_param PATH_INFO $fastcgi_path_info;
> 
> ```

If your application uses PATH\_INFO, then you’ll want to set it something  
like that. I don’t think that $fastcgi\_path\_info actually has a value  
by default, though.

> ```
> fastcgi_param QUERY_STRING $query_string;
> fastcgi_param REQUEST_METHOD $request_method;
> fastcgi_param CONTENT_TYPE $content_type;
> fastcgi_param CONTENT_LENGTH $content_length;
> fastcgi_param REQUEST_URI $request_uri;
> fastcgi_param DOCUMENT_URI $document_uri;
> fastcgi_param DOCUMENT_ROOT /opt/sdr/report/docroot;
> 
> ```

They are all fairly standard; probably only the first and last matter,  
but it depends on your fastcgi server and your application. The last  
one would usually be written to use $document\_root, but anything that  
ends up with the correct value is good.

Note that your fastcgi server, from the logs you provide, either wants  
SCRIPT\_FILENAME to be correct, or DOCUMENT\_ROOT and SCRIPT\_NAME to be  
correct; and you don’t provide a SCRIPT\_NAME.

That may not matter when SCRIPT\_FILENAME points to the R binary.

> Testing any calls for /cgi-bin/R/foo returns:  
> Cannot get script name, are DOCUMENT\_ROOT and SCRIPT\_NAME (or SCRIPT\_FILENAME)  
> set and is the script executable?  
> Cannot get script name, are DOCUMENT\_ROOT and SCRIPT\_NAME (or SCRIPT\_FILENAME)  
> set and is the script executable?

From the above config, SCRIPT\_FILENAME was probably  
/opt/sdr/report/docroot/cgi-bin/R/foo, which is not an executable file.

> Im trying to see if fastcgi\_split\_path\_info might help anything.

Yes.

> **[Module ngx\_http\_fastcgi\_module](http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_split_path_info)**
>
> The ngx\_http\_fastcgi\_module module allows passing requests to a FastCGI server.

It will change the values of $fastcgi\_script\_name and  
$fastcgi\_path\_info.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
