How to process only first path element in the URL as the CGI executable

Francis Daly francis at
Sat Oct 9 14:12:19 MSD 2010

On Fri, Oct 08, 2010 at 09:10:27PM +0100, Frank Church wrote:
> On 8 October 2010 16:55, Francis Daly <francis at> wrote:
> > On Fri, Oct 08, 2010 at 04:02:48PM +0100, Frank Church wrote:

Hi there,

> > > With nginx instead of executing spidersample.cgi it treats
> > > spidersample.cgi/hello as meaning a hello file in a spidersample.cgi
> > > directory, and returns with a 404 error as there is no such directory.

> > >         location ~ \.cgi$ {

> > Your request for "/spidersample.cgi/hello" doesn't match, and so doesn't
> > use this config.

> I removed the "$" and now this is the error message I get:
> 2010/10/08 21:06:46 [error] 14628#0: *22 FastCGI sent in stderr: "Cannot
> chdir to script directory
> (/home/rchurch/Data/Lazarus/CgiApps/spidersample.cgi)" while reading
> response header from upstream, client:, server: localhost,
> request: "GET /spidersample.cgi/hello HTTP/1.1", upstream:
> "fastcgi://unix:/tmp/rchurch_cgi.sock:", host: "localhost:8118", referrer: "
> http://localhost:8118/spidersample.cgi"

That suggests that this location block is now being used
for this request, which is good. The error is because the
fastcgi processor was told that the SCRIPT_FILENAME was
which is untrue. SCRIPT_FILENAME is

Also, the script itself almost certainly uses PATH_INFO to determine
which module of its own to use -- "hello", "echo", or some other --
so you'll want to set that too.

The easiest way to do both is probably to use fastcgi_split_path_info. See
the two new lines below that include the string "path_info".

Note -- it is possible that all of the fastcgi_param lines are unnecessary
if they are already in the included /etc/nginx/fastcgi_params. You can
experiment with removing them at leisure.

    location ~ \.cgi {
            fastcgi_split_path_info (.*\.cgi)(/.*);
            include /etc/nginx/fastcgi_params;
            fastcgi_pass unix:/tmp/sysman_cgi.sock;
            fastcgi_param  SCRIPT_FILENAME $document_root$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  PATH_INFO        $fastcgi_path_info;

> > Then you can change the location definition to include all and only the
> > requests you want handled this way.

That bit should still be done.

All the best,

Francis Daly        francis at

More information about the nginx mailing list