无法在虚拟主机 apache 上执行 proxy.cgi

Posted

技术标签:

【中文标题】无法在虚拟主机 apache 上执行 proxy.cgi【英文标题】:Failed to execute proxy.cgi on virtualhost apache 【发布时间】:2014-03-03 00:44:26 【问题描述】:

我一直在尝试使用 windows 7 和 xampp 从我的计算机上的 openlayers 执行 proxy.cgi。 当我尝试使用 localhost/mymaps/cgi-bin/proxy.cgi 它执行完美。

但是当我尝试使用虚拟主机执行时,它想出了

服务器错误!

服务器遇到内部错误,无法完成您的请求。

错误信息: 无法创建子进程:720002:proxy.cgi

我看看它说的错误日志

[Thu Feb 06 14:24:16 2014] [error] [client 127.0.0.1] (OS 2)The system cannot find the file specified.  : couldn't create child process: 720002: proxy.cgi
[Thu Feb 06 14:24:16 2014] [error] [client 127.0.0.1] (OS 2)The system cannot find the file specified.  : couldn't spawn child process: C:/xampp/cgi-bin/proxy.cgi

这是我的 httpd-vhosts.conf 文件

<VirtualHost *:80>
    ServerAdmin admin@sidik.com
    DocumentRoot "C:\xampp\htdocs\sidik"
    ServerName sidik.com
    ServerAlias sidik.com
    <Directory "C:\xampp\htdocs\sidik">
    Order allow,deny
    Allow from all
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    </Directory>
</VirtualHost>

这是我的 proxy.cgi

#!/Python27/python.exe -u
"""This is a blind proxy that we use to get around browser
restrictions that prevent the javascript from loading pages not on the
same server as the Javascript.  This has several problems: it's less
efficient, it might break some sites, and it's a security risk because
people can use this proxy to browse the web and possibly do bad stuff
with it.  It only loads pages via http and https, but it can load any
content type. It supports GET and POST requests."""

import urllib2
import cgi
import sys, os

# Designed to prevent Open Proxy type stuff.

allowedHosts = ['www.google.co.id','www.openlayers.org','202.124.205.123:81/~vaonline', 'openlayers.org',
                'labs.metacarta.com', 'world.freemap.in', 
                'prototype.openmnnd.org', 'geo.openplans.org',
                'sigma.openplans.org', 'demo.opengeo.org',
                'www.openstreetmap.org', 'sample.azavea.com',
                'v2.suite.opengeo.org', 'v-swe.uni-muenster.de:8080','172.20.110.20:83','172.20.110.20' 
                'vmap0.tiles.osgeo.org', 'www.openrouteservice.org', '172.20.32.11:8080']

method = os.environ["REQUEST_METHOD"]

if method == "POST":
    qs = os.environ["QUERY_STRING"]
    d = cgi.parse_qs(qs)
    if d.has_key("url"):
        url = d["url"][0]
    else:
        url = "http://www.openlayers.org"
else:
    fs = cgi.FieldStorage()
    url = fs.getvalue('url', "http://www.openlayers.org")

try:
    host = url.split("/")[2]
    if allowedHosts and not host in allowedHosts:
        print "Status: 502 Bad Gateway"
        print "Content-Type: text/plain"
        print
        print "This proxy does not allow you to access that location (%s)." % (host,)
        print
        print os.environ

    elif url.startswith("http://") or url.startswith("https://"):

        if method == "POST":
            length = int(os.environ["CONTENT_LENGTH"])
            headers = "Content-Type": os.environ["CONTENT_TYPE"]
            body = sys.stdin.read(length)
            r = urllib2.Request(url, body, headers)
            y = urllib2.urlopen(r)
        else:
            y = urllib2.urlopen(url)

        # print content type header
        i = y.info()
        if i.has_key("Content-Type"):
            print "Content-Type: %s" % (i["Content-Type"])
        else:
            print "Content-Type: text/plain"
        print

        print y.read()

        y.close()
    else:
        print "Content-Type: text/plain"
        print
        print "Illegal request."

except Exception, E:
    print "Status: 500 Unexpected Error"
    print "Content-Type: text/plain"
    print 
    print "Some unexpected error occurred. Error text was:", E

我做错了什么?

【问题讨论】:

Apache gives me 403 Access Forbidden when DocumentRoot points to two different drives 的可能重复项 @PaulSweatte 当两个虚拟主机配置有不同的驱动器号时,这个问题是关于 403 错误的。对我来说根本不像是重复的:不同的错误,不同的问题描述。 【参考方案1】:

虽然这可能会在很晚的时候出现,但是无论如何,这对我有用。通过在 Windows 上指定我的 python 可执行应用程序的位置,使用如下代码; #!c:/Python27/python.exe 在 cgi 文件顶部代替 #!/Python27/python.exe -u 在您自己的情况下,并确保我有这个配置(在我的 apache httpd.conf 配置中);

<Directory "C:/xampp/cgi-bin">
  AllowOverride All
  Options None
  Require all granted
</Directory>

我能够让 cgi 文件工作。希望这会有所帮助

【讨论】:

以上是关于无法在虚拟主机 apache 上执行 proxy.cgi的主要内容,如果未能解决你的问题,请参考以下文章

apache + mod_proxy + jetty无法访问静态资源的问题

如何使用 apache2 和 mod_proxy_fcgi 禁用缓冲?

Apache的reverse proxy设定

使用 node-http-proxy 从 NodeJS 到 Apache 的反向代理不起作用

对资源“执行 [为 apache-tomcat 绑定启用 mod_proxy]”执行操作“运行”时出错

无法将模块/mod_proxy_wstunnel.so 加载到服务器中