Cgi-Cgi-Skript für Nginx: Antwort ist binäres Format

557
Junchao Gu

Ich versuche, ein C ++ - CGI-Skript auf Nginx auszuführen. Ich verwende FCGIWrap mit einem Skript von der Nginx-Website. Der Programmcode sieht so aus:

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

Und ich habe mit g ++ -o start.cgi start.cpp kompiliert. Wenn ich also ./start.cgi ausführte, erhielt ich die korrekte Ausgabe. Aber wenn ich curl localhost / cgi-bin / start.cgi verwende, habe ich eine binäre Ausgabe erhalten (und tatsächlich Cout-Antworten und Informationen wie GCC sehen ... Also vermute ich, dass es sich um die kompilierte ausführbare Datei handelt)

Meine 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 { } } } 
0

1 Antwort auf die Frage

0
Richard Smith

Die Syntax Ihrer locationAnweisung ist ungültig. Wenn Sie alle Dateien mit einer .cgiErweiterung im /cgi-bin/Verzeichnis abgleichen möchten, sollten Sie Folgendes verwenden:

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

Einzelheiten finden Sie in diesem Dokument .

Außerdem $fastcgi_script_namewird auf den Wert gesetzt, $uridessen /cgi-bin/Element den Pfadnamen enthält. Also SCRIPT_FILENAMEsollten Sie wahrscheinlich auf Folgendes eingestellt sein:

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;