4

This should be my last question regarding FastCGI and NGINX (I've asked way too many already), but I had a question regarding running C FastCGI scripts on a web server, specifically NGINX. So right now this is how my nginx.conf file looks like:

user nobody nobody;

events {
    worker_connections 1024;
}    

http {
    server {
        listen 80;
        server_name localhost;

        location / {
            root            /nginx/html;
            index index.html index.htm new.html;
            autoindex on;
            fastcgi_pass   127.0.0.1:8000;
        }

    }
}

I have a simple C FastCGI script that prints out Hello World. I know that in order to run this script, I first have to compile the C script which would result in a binary. I then execute this binary using spawn-fcgi -p 8000 -n <binary> or cgi-fcgi -start -connect localhost:8000 ./<binary>. I have had success doing this and displaying the correct results. However, when I do this, the CGI script is the only thing that is being displayed on the web server. I cannot go to any .html page or any other page for that matter. Even if I type in a random extension which should result in a 404 Page not Found error, the CGI script is being displayed. Basically I'm trying to have a index.html be the home page and then when the user clicks on a button, the user is taken to a new page that displays the C CGI script.

Is this possible? If so, how could I do it? I have spent hours trying to find a solution online but have had no success. Please let me know if the question is too vague/unclear or if you need more information! Thank you!

0

1 Answer 1

5

There are two possibilities that I can think of. You can assign a URI to your CGI program and use that to access it. Or you can send any invalid URI to your CGI program.

In the first case, you could use:

root /nginx/html;
index index.html index.htm new.html;

location / {
    try_files $uri $uri/ =404;
}
location /api {
    fastcgi_pass   127.0.0.1:8000;
}

So, any URI beginning with /api will be sent to the CGI program. Other URIs will be served by nginx, unless not found, in which case a 404 response will be returned.


In the second case, you could use:

root /nginx/html;
index index.html index.htm new.html;

location / {
    try_files $uri $uri/ @api;
}
location @api {
    fastcgi_pass   127.0.0.1:8000;
}

So any URI that does not exist will be sent to the CGI program.

See this document for the try_files directive, and this document for the location directive.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! Should've done a better job understanding how exactly to manipulate the .conf file, but your answer helped!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.