【问题标题】:Nginx + FastCGI + C throws 502 Bad GatewayNginx + FastCGI + C 抛出 502 Bad Gateway
【发布时间】:2013-08-19 02:02:35
【问题描述】:

我正在尝试使用 FASTCgi 运行 Nginx,以通过 C 应用后端处理请求。

我正在使用本教程:http://www.kutukupret.com/2010/08/20/nginx-fastcgi-hello-world-in-c/

我的nginx配置文件:

server
  {
    listen 80;
    listen [::]:80 default ipv6only=on;

    server_name localhost;

    location /
    {
      fastcgi_pass   127.0.0.1:8000;

      fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
      fastcgi_param  SERVER_SOFTWARE    nginx;
      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  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
      fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
      fastcgi_param  REQUEST_URI        $request_uri;
      fastcgi_param  DOCUMENT_URI       $document_uri;
      fastcgi_param  DOCUMENT_ROOT      $document_root;
      fastcgi_param  SERVER_PROTOCOL    $server_protocol;
      fastcgi_param  REMOTE_ADDR        $remote_addr;
      fastcgi_param  REMOTE_PORT        $remote_port;
      fastcgi_param  SERVER_ADDR        $server_addr;
      fastcgi_param  SERVER_PORT        $server_port;
      fastcgi_param  SERVER_NAME        $server_name;
    }
  }

我的 C 文件“hello.c”:

#include <fcgi_stdio.h>
int main( int argc, char *argv[] )
{
   while( FCGI_Accept() >= 0 ) {
      printf( "Content-Type: text/plainnn" );
      printf( "Hello world in Cn" );
   }
   return 0;
}

我用 FastCGI 开发工具包编译 C 文件,它会生成一个 hello 二进制文件。

最后我运行这一行:spawn-fcgi -a 127.0.0.1 -p 8000 -n /var/www/example.com/bin/hello

但是当我在浏览器中输入http://localhost 时,它会抛出“502 Bad Gateway”。

谁能给我点灯?

【问题讨论】:

    标签: c nginx fastcgi


    【解决方案1】:

    您的 printf 行有错误 -

    printf( "Content-Type: text/plainnn" );
    

    应该是-

    printf( "Content-Type: text/plain\r\n\r\n" );
    

    注意\r\n\r\n,这是 HTTP 标头和 HTTP 正文之间的强制分隔符。

    【讨论】:

      【解决方案2】:

      我建议查看 nginx 错误日志:

      sudo tail -f /var/log/nginx/error.log
      

      我遇到了同样的错误,但是 C++ 服务器使用 CppCMS 库和 unix 套接字与 nginx 通信。从错误日志可以看出socket文件的文件权限有问题:

      2014/05/28 14:52:01 [crit] 1818#0: *172 connect() to unix:/tmp/fcgi-socket failed (13: Permission denied) while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET /server HTTP/1.1", upstream: "fastcgi://unix:/tmp/fcgi-socket:", host: "localhost:8080"
      

      只需将它们设置为 777 即可解决我的问题。

      【讨论】:

        【解决方案3】:

        看起来你的 php5-fpm 没有运行你在 nginx conf 中包含的 om 8000 端口

        fastcgi_pass   127.0.0.1:8000;
        

        您可以使用以下命令验证您的 php5-fpm 是否在该端口上运行

        grep -Hr "8000" /etc/php5/fpm/pool.d
        

        在 8000 上设置 php5-fpm 打开以下文件并更改监听 8000 端口

        vim etc/php5/fpm/pool.d/www.conf
        

        现在搜索listen并将其替换为以下行

        listen = 127.0.0.1:8000
        

        保存文件并重启php5-fpm

        【讨论】:

        • 您好,感谢您的回复,但我没有使用 PHP,仅使用 Nginx 和二进制 C 来处理通过 FASTCgi 的请求。
        • 使用netstat -ntluap | grep 8000确保端口8000打开
        • 交叉检查你的 fastcgi 正在监听端口 8000 或 9000
        • 是的,我之前检查过端口并且都打开了:(
        【解决方案4】:

        我过去也遇到过同样的错误。下面的配置对我有用。

        location / {
            include fastcgi_params;
            fastcgi_pass  localhost:8000;
        }
        

        【讨论】:

          猜你喜欢
          • 2018-11-12
          • 2020-08-01
          • 1970-01-01
          • 2017-12-30
          • 2014-10-17
          • 2017-05-03
          • 2019-05-25
          • 1970-01-01
          • 2023-03-20
          相关资源
          最近更新 更多