【问题标题】:Running a C FastCGI script using NGINX使用 NGINX 运行 C FastCGI 脚本
【发布时间】:2016-11-13 19:11:28
【问题描述】:

这应该是我关于 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 脚本的新页面。

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

【问题讨论】:

    标签: c nginx cgi fastcgi


    【解决方案1】:

    我能想到两种可能性。您可以为您的 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 document,有关location 指令的信息请参见this document

    【讨论】:

    • 完美!应该更好地理解如何操作 .conf 文件,但您的回答有所帮助!
    猜你喜欢
    • 2010-09-27
    • 2016-11-07
    • 1970-01-01
    • 1970-01-01
    • 2019-12-04
    • 1970-01-01
    • 2011-10-26
    • 1970-01-01
    • 2012-06-06
    相关资源
    最近更新 更多