如何配置Nginx虚拟主机以使用FastCGI运行C ++程序?

时间:2015-09-26 11:33:27

标签: c++ nginx

我想创建c ++网站,我处于零级别。需要使用nginx + fastcgi

运行我的第一个c ++文件

我在c ++上编译了一个程序:

#include <iostream>
using namespace std;

int main ()
{

   cout << "Content-type:text/html\r\n\r\n";
   cout << "<html>\n";
   cout << "<head>\n";
   cout << "<title>Hello World - First CGI Program</title>\n";
   cout << "</head>\n";
   cout << "<body>\n";
   cout << "<h2>Hello World! This is my first CGI program</h2>\n";
   cout << "</body>\n";
   cout << "</html>\n";

   return 0;
}

编译它:

g ++ 1.cpp -lfcgi -o hello

并放入网站目录:/var/www/f.ci/public/bin/hello

然后开始产生过程:

spawn-fcgi -a 127.0.0.1 -p 8000 -n /var/www/f.ci/public/bin/hello

nginx虚拟主机配置是:

server
{
    listen  80;
    server_name     f.ci www.f.ci *.f.ci;
    root            /var/www/f.ci/public;

    location /
        {
        try_files   $uri $uri/  =404;
        }

        location ~ \.php$ 
        {
        try_files $uri =404;
                include fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
        }

}

确切地说这是文件/ etc / nginx / fastcgi_params:

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     $request_filename;
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   GATEWAY_INTERFACE   CGI/1.1;
fastcgi_param   SERVER_SOFTWARE     nginx/$nginx_version;

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;

fastcgi_param   HTTPS           $https if_not_empty;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param   REDIRECT_STATUS     200;

我在配置虚拟主机时重启了nginx。 当我输入浏览器时,我的服务器让我下载hello文件: f.ci/bin/hello 但不要显示它的消息

请问如何正确配置服务器?

1 个答案:

答案 0 :(得分:0)

你的端口错误“fastcgi_pass 127.0.0.1:9000;”
将9000更改为8000

“spawn-fcgi -a 127.0.0.1 -p 8000 -n /var/www/f.ci/public/bin/hello” “fastcgi_pass 127.0.0.1:9000;”

端口号必须匹配。