jacob
jacob

Reputation: 4956

Trouble with a FCGI application

I'm having trouble getting an FCGI application working in C using nginx. I'm using spawn-fcgi to create the socket and run my application (which I have named paste)

I'm thinking it must be a problem with my application, but I'm fairly certain I copied all the relevant parts from the example source located here.

This is the error nginx is giving me:

[error] 53300#0: *4 upstream prematurely closed connection while
reading response header from upstream, client: 127.0.0.1, server: 
localhost, request: "GET /test HTTP/1.1", upstream: 
"fastcgi://unix:/tmp/cfcgi.sock:", host: "localhost"

Here's the source for the application:

#include <fcgi_stdio.h>
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char **argv) {
    while(FCGI_Accept() >= 0) {
        printf("Content-type: text/html\r\n\r\n");
        printf("Hostname: %s", getenv("SERVER_HOSTNAME"));
    }
    return EXIT_SUCCESS;
}

The relevant config changes in nginx:

location /test {
    include fastcgi_params;
    fastcgi_pass unix:/tmp/cfcgi.sock;
}

And the spawn-fcgi command:

spawn-fcgi -s /tmp/cfcgi.sock -M 0777 -P cfcgi.pid -- paste

Upvotes: 0

Views: 514

Answers (1)

Ivan Tsirulev
Ivan Tsirulev

Reputation: 2811

The variables that Nginx passes to a FastCGI server are listed in /etc/nginx/fastcgi_params, which you include in your configuration file. And there is no such variable as SERVER_HOSTNAME. The closest one is SERVER_NAME.

The function getenv() returns 0 if the requested variable is not found, which happens in your case. Then this value is referenced by printf (%s), which causes a segmentation fault to occur.

So, to fix the problem you can either add the parameter SERVER_HOSTNAME to your fastcgi_params file (don't forget to reload Nginx after that), or replace SERVER_HOSTNAME with SERVER_NAME in your application.

Upvotes: 2

Related Questions