如何为Apache httpd服务器启用CORS?(逐步过程)

10

我想知道是否有人能帮我在我的Apache Web服务器上设置CORS。我需要一步一步的过程,因为网上很多站点告诉我不同的东西。例如我需要在我的httpd.confssl.conf文件中做什么,在哪里放置我的.htaccess文件等等。

2个回答

9
上述答案正确并放在httpd.conf中。这应该更改为您设置的任何文档根目录
Ubuntu上,httpd.conf位于目录/etc/apache2中,apache2.conf也位于/etc/apache2中。
CENTOS 6中,httpd.conf位于路径/etc/httpd/conf/httpd中。
<Directory "/var/www/html">
        Header set Access-Control-Allow-Origin "*"
</Directory>

8
# edit your conf/httpd.conf file.  Verify that the headers module is loaded
LoadModule headers_module modules/mod_headers.so

# to whitelist every origin, use
Header set Access-Control-Allow-Origin "*"

更多信息:http://enable-cors.org/server_apache.html

我应该把 Header set Access-Control-Allow-Origin "*" 放在哪里? - user3851283
你可以把所有东西放在同一个文件中,只需要在加载模块后将其放置在某个位置即可。我认为 .htaccess 也可以(如果启用了覆盖权限)。 - Kevin Seifert
如果我将其添加到 .htaccess 文件中,是否需要在 httpd.conf 文件中进行一些操作以允许查看 .htaccess? - user3851283
是的,在这种情况下,您还需要为该目录设置“AllowOverride FileInfo”。 AllowOverride仅在未使用正则表达式指定的<Directory>部分中有效,而不在<Location>、<DirectoryMatch>或<Files>部分中有效。更多信息:http://httpd.apache.org/docs/2.4/mod/core.html - Kevin Seifert
我刚把 .htaccess 文件放到了 /var/www 目录下,这样可以吗? - user3851283
显示剩余8条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接