【发布时间】:2011-06-27 01:25:03
【问题描述】:
我正在尝试将 Apache 配置为远程服务器的代理,以允许使用 CORS 进行跨域 AJAX。为此,我需要 Apache 响应 2 个 HTTP 动词,如下所示:
选项: 使用一些简单的 HTTP 标头响应这个 CORS 的“飞行前”请求。我想到这可能是一个简单的 CGI 脚本 (options.pl)。
发布: 将所有 POST 请求代理到远程服务器,但添加 Access-Control-Allow-Origin "*" 标头以允许跨域请求发生。
我可以独立地满足这两个要求,但我无法配置 Apache 来同时满足这两个要求。问题是当配置了ProxyPass和ProxyPassReverse时,OPTIONS请求不再命中CGI脚本,它们被代理到远程服务器。
我当前的配置如下。我想用一个纯网络服务器解决方案来解决这个问题,例如Apache/Nginx(而不是运行一些应用程序代码),如果可能的话。
<VirtualHost *:80>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
DocumentRoot /var/www
<Location "/">
# Disallow all verbs except OPTIONS and POST
order deny,allow
deny from all
# OPTIONS should be handled by a local CGI script
<Limit OPTIONS>
allow from all
Script OPTIONS /cgi-bin/options.pl
</Limit>
# POST requests are proxied to a remote server
<Limit POST>
allow from all
ProxyPass http://somewhere-else/
ProxyPassReverse http://somewhere-else/
Header add Access-Control-Allow-Origin "*"
</Limit>
</Location>
</VirtualHost>
【问题讨论】: