在 httpd.conf 中启用 CORS

Enable CORS in httpd.conf

我正在开发一个 Web 应用程序,它可以 REST 调用其域外的另一个 Web 应用程序。

但每当它尝试进行 REST 调用时,我都会遇到 chrome 中的 CORS 问题。

错误:

No 'Access-Control-Allow-Origin' header is present on the requested resource.

我知道这个问题已经被问过很多次了,我已经尝试了很多建议,但是 none 对我有用。

我正在使用 httpd 作为 Web 服务器。

下面是我系统的httpd.conf。 我已经按照建议 here.

<Directory> 中添加了 Header set Access-Control-Allow-Origin "*"

求推荐。

谢谢

httpd.conf

ServerRoot "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin root@localhost
<Directory />
    AllowOverride none
  Header set Access-Control-Allow-Origin "*"
    Require all granted
</Directory>
DocumentRoot "/var/www/html"
<Directory "/var/www">
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>
<Directory "/var/www/html">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>
<Files ".ht*">
    Require all denied
</Files>
ErrorLog "logs/error_log"
LogLevel warn

<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common

    <IfModule logio_module>
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>
    CustomLog "logs/access_log" combined
</IfModule>

<IfModule alias_module>
    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"

</IfModule>
<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
</Directory>

<IfModule mime_module>
    TypesConfig /etc/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml
</IfModule>
AddDefaultCharset UTF-8

<IfModule mime_magic_module>
    MIMEMagicFile conf/magic
</IfModule>
EnableSendfile on
IncludeOptional conf.d/*.conf

ProxyRequests Off
ProxyPreserveHost On
<VirtualHost *:80>
  ProxyPass / http://localhost:9000/        # My local sonarqube URL
  ProxyPassReverse / http://myhost.com
  ErrorLog logs/sonar/error.log
  CustomLog logs/sonar/access.log common
</VirtualHost>

根据我的经验,如果 API 有任何问题,通常会返回此错误。

您应该在 Chrome 中尝试在没有网络安全的情况下测试您的网络应用程序,看看问题是否真的是 CORS 问题。有关详细信息,请参阅 here

在基于 Debian 的发行版中。启用 mod_headers 运行

a2enmod headers
sudo service apache2 reload

在 CentOS 和其他基于 RedHat 的发行版中

像 httpd.conf 一样编辑 apache 读取的配置文件并添加

LoadModule headers_module modules/mod_headers.so

并使用 sudo service httpd restart

重新加载 apache

并且在 httpd.conf 或 apache 读取的某些文件中,例如 apache2.conf,文件 *.conf 位于文件夹中,例如 sites-available/ 或 sites-enabled/

Header set Access-Control-Allow-Origin: *

* 或您想要的一个或多个域

Keep in mind this must be placed in the server that receive the request, not the server that send the request, and restrictions are made by browsers, not servers, see this: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin

还有另一种方法,而不是编辑一些 .conf 文件,它在文档根级别创建一个名为 .htaccess 的文件,内部:Header set Access-Control-Allow-Origin *

要启用 CORS,可以使用以下 header 指令:

Header set Access-Control-Allow-Origin "*"