在使用OpenLayers过程中,若在IIS下部署,需要在IIS启用CGI,当前以IIS7为例进行介绍如何来启用CGI和相关的设置。启用CGI的目的是使用WFS的前提,因为WFS在请求过程中会出现跨域问题,所以根据OpenLayers的建议,只需要使用其自带的代理CGI。

1. 安装python

由于OpenLayers下的默认内置CGI是python实现的,所有要在机器上预先安装python运行环境,关于其安装,本文也不过多介绍,可以去官网http://www.python.org上下载安装即可。

2. 启用CGI

关于如下在IIS7中启用CGI,请参考本人前面的博文《在IIS7中启用CGI》,里面已做详细介绍,在此不做赘述,最终设置如下图。

3. 添加代理域

打开OpenLayers下的proxy.cgi,前部分代码如下:

 
  
  1. #!/usr/bin/env python 
  2.  
  3. """This is a blind proxy that we use to get around browser 
  4. restrictions that prevent the Javascript from loading pages not on the 
  5. same server as the Javascript.  This has several problems: it's less 
  6. efficient, it might break some sites, and it's a security risk because 
  7. people can use this proxy to browse the web and possibly do bad stuff 
  8. with it.  It only loads pages via http and https, but it can load any 
  9. content type. It supports GET and POST requests.""" 
  10.  
  11. import urllib2 
  12. import cgi 
  13. import sys, os 
  14.  
  15. # Designed to prevent Open Proxy type stuff. 
  16.  
  17. allowedHosts = ['www.openlayers.org''openlayers.org'
  18. '192.168.0.201:8088'
  19.                 'labs.metacarta.com''world.freemap.in',  
  20.                 'prototype.openmnnd.org''geo.openplans.org'
  21.                 'sigma.openplans.org''demo.opengeo.org'
  22.                 'www.openstreetmap.org''sample.avencia.com'
  23.  
  24. method = os.environ["REQUEST_METHOD"
  25. # ...

python变量allowedHosts中,默认已添加了openlayers相关的网站域名,在实际应用过程中,本人的GeoServer的IP为192.168.0.201:8088,将其添加到其中即可。

4. 指定代理地址

在openlayers在使用wfs时,只需要添加如下一行代码即可,proxy.cgi的路径可以跟据实际情况来指定,由于本人实际应用中,是将proxy.cgi放在当前程序的相同目录。

 
  
  1. OpenLayers.ProxyHost = "proxy.cgi?url="

P.S. 设置比较简单,持续应用中。