加载Apache头文件模块,但不能在htaccess中设置头文件

我有Apache 2.2.29(Unix)设置和运行在我的新开发机器(MAC)。 我正在尝试为一个API项目设置CORS头文件 – 我已经做了很多次了。

该项目的htaccess文件如下所示:

<IfModule mod_headers.c> Header set Access-Control-Allow-Origin "*" Header set Access-Control-Allow-Methods "POST, GET, PUT, OPTIONS, PATCH, DELETE" Header set Access-Control-Allow-Headers "X-Accept-Charset,X-Accept,Content-Type" ServerSignature Off </IfModule> <IfModule mod_rewrite.c> RewriteEngine On RewriteBase /cms/public RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC] RewriteRule (.+) index.php?p=$1 [QSA,L] </IfModule> 

和头文件模块加载我的conf:

 LoadModule headers_module libexec/mod_headers.so 

该模块文件存在于apache conf中显示的位置,并且被加载OK:

 dan$ httpd -M Loaded Modules: core_module (static) mpm_event_module (static) http_module (static) so_module (static) authn_file_module (shared) authn_dbm_module (shared) authn_anon_module (shared) ... headers_module (shared) 

我也检查了我的主要conf和包括虚拟主机conf conf conf覆盖设置:

 <Directory "/Users/dan/Sites"> Options Indexes FollowSymLinks MultiViews AllowOverride All <IfModule mod_authz_core.c> Require all granted </IfModule> <IfModule !mod_authz_core.c> Order allow,deny Allow from all </IfModule> </Directory> 

但是,标题没有按预期设置。 下面是使用上述htaccess文件向API项目发出请求时的响应头文件:

 HTTP/1.1 200 OK Date: Wed, 22 Jul 2015 10:36:05 GMT Server: Apache/2.2.29 (Unix) DAV/2 mod_fastcgi/2.4.6 mod_ssl/2.2.29 OpenSSL/1.0.2c Expires: Wed, 15 Jul 2015 10:36:05 GMT Pragma: no-cache Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Keep-Alive: timeout=5, max=100 Connection: Keep-Alive Transfer-Encoding: chunked Content-Type: application/json; charset=utf-8 

我已经testing了在其他Apache环境中的应用程序设置,它正在按预期工作…所以这绝对是我的设置。 这让我疯狂!

有什么想法吗? 担

如果你仍然有兴趣解决这个问题,我有一些build议。 configuration的基本思想是好的,我testing了一个简单的设置和标题设置正确。

这意味着这个问题可能与htaccess文件和服务器使用它的能力有关。 我会首先尝试把“垃圾”放入htaccess文件(任何无效的Apacheconfiguration)。 如果正在正确读取htaccess文件,所有导致文件读取的请求将生成500 Internal Server Error

接下来运行apachectl -S并检查输出,如果你不确定的话把它放在你的问题中。 您需要确保请求映射到您的问题中的<Directory>块。 恐怕除了通过configuration文件search之外,您可以做的事情很less。 当你这样做,确保AllowOverride没有重写的地方。

另外,您正在使用mod_fastcgi,因此根据您的configuration,请求可能根本不会映射到文件系统上的目录,这是读取htaccess文件所需的。