如何在Nginx中使用CGI(3)


运行上述程序将会将其绑定到位于/var/run/nginx/cgiwrap-dispatch.sock的Unix套接字。取保Nginx工作进程具有文件的读写权限。该脚本不会自我终结(?),需要将其运行为后台程序(Bash中在执行命令后添加&)

If this all works, then the next part is to setup Nginx:

如果工作了,接下来就是启动Nginx:


http {
  root  /var/www/htdocs;
  index index.html;
  location ~ ^/cgi-bin/.*\.cgi$ {
    gzip off; #gzip makes scripts feel slower since they have to complete before getting gzipped
    fastcgi_pass  unix:/var/run/nginx/cgiwrap-dispatch.sock;
    fastcgi_index index.cgi;
    fastcgi_param SCRIPT_FILENAME /var/www/cgi-bin$fastcgi_script_name;
    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 GATEWAY_INTERFACE  CGI/1.1;
    fastcgi_param SERVER_SOFTWARE    nginx;
    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;
  }
}
Restart Nginx and point your browser at your CGI program. The above sample config will execute any .cgi file in cgi-bin with the cgiwrap-fcgi.pl wrapper, tweak this to your heart's content.
重启Nginx并将浏览器定向到CGI程序的URL。上面的简单配置将会使用cgiwrap-fcgi.pl包装执行位于cgi-bin所有.cgi后缀的文件,请谨记这一点,这可能会很危险。

I've been able to run Python, Perl, and C++ cgi apps with this - apps that use GET or POST.

可以使用GET或POST方法的C++,Perl,Python的cgi应用都可运行。

You may find that $document_root does not point to your actual document root (hardcoded upon build), so you can replace the fastcgi param DOCUMENT_ROOT with "/the/real/path". Also replace the SCRIPT_FILENAME param if that is used by your CGI wrapper (the one above does)


可以看到,$document_root没有指向实际的文档root(依赖构建的硬编码),需要将fastcgi参数DOCUMENT_ROOT替代为真实可用的参数,并将SCRIPT_FILENAME参书替代为CGI包装器使用的真实值。

后记
虽然,没有写过CGI代码,也没有配置安装过Nginx,但我觉的这些东西都很有趣,等我学完Rails,一定要好好研究研究Nginx,和Apahce一起比对学习。

更多Nginx相关教程见以下内容

CentOS 6.2实战部署Nginx+MySQL+PHP

使用Nginx搭建WEB服务器

搭建基于Linux6.3+Nginx1.2+PHP5+MySQL5.5的Web服务器全过程

CentOS 6.3下Nginx性能调优

CentOS 6.3下配置Nginx加载ngx_pagespeed模块

CentOS 6.4安装配置Nginx+Pcre+php-fpm

Nginx安装配置使用详细笔记

Nginx日志过滤 使用ngx_log_if不记录特定日志

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/a7505f4d89a7630979e00cdf36b27a0c.html