Nginx提供C ++ cgi脚本:响应是二进制格式


0

我试图在nginx上运行C ++ CGI脚本。我正在使用来自nginx网站的脚本的FCGIWrap。程序代码是这样的:

#include <iostream>
using namespace std;

int main ()
{

   cout << "Content-type:text/html\n\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 ++ -o start.cgi start.cpp编译。所以当我运行./start.cgi时,我得到了正确的输出。但是当我使用curl localhost / cgi-bin / start.cgi时,我得到了二进制输出(实际上看到cout响应和GCC之类的信息......所以我怀疑它是已编译的可执行文件)

我的nginx.conf:

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /var/www;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location /cgi-bin/*\.cgi {
          gzip off;
          fastcgi_pass unix:/var/run/fcgiwrap.sock;
          include /etc/nginx/fastcgi_params;
          fastcgi_param SCRIPT_FILENAME /var/www/cgi-bin$fastcgi_script_name;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}

Answers:


0

你的语法 location 指令无效。如果你想用a匹配所有文件 .cgi 内部扩展 /cgi-bin/ 目录,您应该使用:

location ~* ^/cgi-bin/.*\.cgi$ { ... }

看到 这个文件 详情。

也, $fastcgi_script_name 将被设置为的值 $uri 其中包括 /cgi-bin/ 路径名称的元素。所以你的 SCRIPT_FILENAME 应该设置为:

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.