【问题标题】:How to allow Cross domain request in apache2如何在apache2中允许跨域请求
【发布时间】:2015-05-22 21:49:26
【问题描述】:

这是我的配置文件。

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName localhost:80
    DocumentRoot /var/www/XXX
    <Directory />
        Options None
        AllowOverride None
        Order deny,allow
        Deny from all
    </Directory>
    <Directory /var/www/qvbn-app-web-ctrl>
        Options FollowSymLinks
        AllowOverride AuthConfig FileInfo
        Order allow,deny
        Allow from all
        Header set Access-Control-Allow-Origin "*"
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

当我尝试重新加载 apache2 时,iT 给出错误:

   Invalid command 'Header', perhaps misspelled or defined by a module not included in the server configuration
    Action 'configtest' failed.

我不知道如何启用 CORS。我跟着这个: http://enable-cors.org/server_apache.html

【问题讨论】:

    标签: apache cors


    【解决方案1】:
    OS=GNU/Linux Debian
    Httpd=Apache/2.4.10
    

    /etc/apache2/apache2.conf 中的更改

    <Directory /var/www/html>
         Order Allow,Deny
         Allow from all
         AllowOverride all
         Header set Access-Control-Allow-Origin "*"
    </Directory>
    

    添加/激活模块

     a2enmod headers 
    

    重启服务

    /etc/init.d/apache2 restart
    

    【讨论】:

    • 非常感谢..被困在这很长一段时间..我试图通过 LocationMatch 和所有..但这就像一个魅力
    • 工作就像一个魅力。
    • /etc/init.d/apache2 restart
    • Firefox 仍在阻止我的 CORS 请求。最后我发现忽略一个端口上的自签名证书并不适用于 FF 中的另一个端口(在 Chrome 中,它确实如此)。只有在另一个端口上手动启动请求并忽略那里的证书后,FF 才允许 CORS 请求。最好尝试避免使用自签名证书向网站发出 CORS 请求(最好设置一个受信任的证书,这比让它工作更容易)。
    【解决方案2】:

    首先在您的服务器上启用mod_headers,然后您可以在Apache conf 和.htaccess 中使用header 指令。

    1. 启用mod_headers
    • a2enmod headers
    1. .htaccess文件中配置头文件
    Header add Access-Control-Allow-Origin "*"
    Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
    Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"
    

    【讨论】:

    • 另外,启用头模块后重启apache。
    【解决方案3】:

    在 httpd.conf 中

    1. 确保这些已加载:
    LoadModule headers_module modules/mod_headers.so
    
    LoadModule rewrite_module modules/mod_rewrite.so
    
    1. 在目标目录中:
    <Directory "**/usr/local/PATH**">
        AllowOverride None
        Require all granted
    
        Header always set Access-Control-Allow-Origin "*"
        Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
        Header always set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, authorization, accept, client-security-token"
        Header always set Access-Control-Expose-Headers "Content-Security-Policy, Location"
        Header always set Access-Control-Max-Age "600"
    
        RewriteEngine On
        RewriteCond %{REQUEST_METHOD} OPTIONS
        RewriteRule ^(.*)$ $1 [R=200,L]
    
    </Directory>
    
    If running outside container, you may need to restart apache service.
    

    【讨论】:

    • 这是此线程中唯一 解决飞行前OPTIONS 请求的答案。干得好!
    【解决方案4】:

    将以下内容放入站点的 .htaccess 文件(在 /var/www/XXX 中):

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

    而不是 .conf 文件。

    你也会想要使用

    AllowOverride All
    

    在域的 .conf 文件中,以便 Apache 查看它。

    【讨论】:

      【解决方案5】:

      对我有用的 Ubuntu Apache2 解决方案 .htaccess 编辑对我不起作用,我不得不修改 conf 文件。

      nano /etc/apache2/sites-available/mydomain.xyz.conf

      我的配置允许 CORS 支持

      <IfModule mod_ssl.c>
          <VirtualHost *:443>
      
              ServerName mydomain.xyz
              ServerAlias www.mydomain.xyz
      
              ServerAdmin support@mydomain.xyz
              DocumentRoot /var/www/mydomain.xyz/public
      
              ### following three lines are for CORS support
              Header add Access-Control-Allow-Origin "*"
              Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
              Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"
      
              ErrorLog ${APACHE_LOG_DIR}/error.log
              CustomLog ${APACHE_LOG_DIR}/access.log combined
      
              SSLCertificateFile /etc/letsencrypt/live/mydomain.xyz/fullchain.pem
              SSLCertificateKeyFile /etc/letsencrypt/live/mydomain.xyz/privkey.pem
      
          </VirtualHost>
      </IfModule>
      

      然后键入以下命令

      a2enmod 标头

      在尝试之前确保缓存已清除

      【讨论】:

      • “在尝试之前确保缓存是清晰的”谢谢!
      【解决方案6】:

      在 Apache2 中启用 mod_headers 以便能够使用 Header 指令:

      a2enmod headers
      

      【讨论】:

        【解决方案7】:

        我在让它工作时遇到了很多麻烦。傻瓜,不要忘记旧页面 - 即使是子请求 - 也会缓存在您的浏览器中。也许很明显,但请清除您的浏览器缓存。之后,也可以使用Header set Cache-Control "no-store"这对我测试时很有帮助。

        【讨论】:

          【解决方案8】:

          您也可以将以下代码放入 htaccess 文件中,以允许 CORS 使用 htaccess 文件

              ######################## Handling Options for the CORS
              RewriteCond %{REQUEST_METHOD} OPTIONS
              RewriteRule ^(.*)$ $1 [L,R=204]
          
             ##################### Add custom headers
             Header set X-Content-Type-Options "nosniff"
             Header set X-XSS-Protection "1; mode=block"
             # Always set these headers for CORS. 
             Header always set Access-Control-Max-Age 1728000
             Header always set Access-Control-Allow-Origin: "*"
             Header always set Access-Control-Allow-Methods: "GET,POST,OPTIONS,DELETE,PUT"
             Header always set Access-Control-Allow-Headers: "DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,C$
             Header always set Access-Control-Allow-Credentials true
          

          为了提供信息,您还可以查看这篇文章http://www.ipragmatech.com/enable-cors-using-htaccess/,它允许 CORS 标头。

          【讨论】:

            猜你喜欢
            • 2015-10-11
            • 2011-10-13
            • 2013-03-24
            • 2012-04-27
            • 2020-07-22
            • 2012-11-03
            • 2016-03-03
            • 1970-01-01
            • 2012-08-01
            相关资源
            最近更新 更多