使用NGINX运行C FastCGI脚本

时间:2016-07-11 18:24:32

标签: c nginx cgi fastcgi

这应该是关于FastCGI和NGINX的最后一个问题(我已经问过太多了),但我有一个关于在Web服务器上运行C FastCGI脚本的问题,特别是NGINX。所以现在这就是我的nginx.conf文件的样子:

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;
        }

    }
}

我有一个简单的C FastCGI脚本打印出Hello World。我知道为了运行这个脚本,我首先必须编译导致二进制文件的C脚本。然后,我使用spawn-fcgi -p 8000 -n <binary>cgi-fcgi -start -connect localhost:8000 ./<binary>执行此二进制文件。我已经成功地做到了并显示了正确的结果。但是,当我这样做时,CGI脚本是Web服务器上唯一显示的内容。我无法访问任何.html页面或任何其他页面。即使我输入一个随机扩展名,导致404 Page not Found错误,也会显示CGI脚本。基本上我正在尝试将index.html作为主页,然后当用户单击按钮时,用户将进入显示C CGI脚本的新页面。

这可能吗?如果是的话,我怎么能这样做?我花了几个小时试图在线找到解决方案,但没有成功。如果问题太模糊/不清楚或者您需要更多信息,请告诉我们!谢谢!

1 个答案:

答案 0 :(得分:3)

我能想到两种可能性。您可以为CGI程序分配URI并使用它来访问它。或者您可以向CGI程序发送任何无效的URI。

在第一种情况下,您可以使用:

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;
}

因此,任何以/api开头的URI都将被发送到CGI程序。其他URI将由nginx提供,除非未找到,否则将返回404响应。

在第二种情况下,您可以使用:

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;
}

因此,任何不存在的URI都将被发送到CGI程序。

try_files指令见this documentlocation指令见this document

相关问题