在 Ubuntu 14.04 上配置 apache 2.4 以启用 CORS

     2023-03-10     280

关键词:

【中文标题】在 Ubuntu 14.04 上配置 apache 2.4 以启用 CORS【英文标题】:Configure apache 2.4 on Ubuntu 14.04 for to enable CORS 【发布时间】:2018-08-24 18:03:25 【问题描述】:

我有这个 HTML / Javascript 代码 ....

<html>
  <head>
    <meta charset='utf-8' />
    <title>Title</title>
    <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
    <!-- *** References for JQuery ... -->
    <script src="http://code.jquery.com/jquery-3.3.1.min.js"></script>
  </head>
  <body>
    <script>
        var city= "Torino";
        $.ajax(
          url: "http://www.mysite1.org/cesarefortelegram/Telegram/OpenProntoSoccorso/API/getProntoSoccorsoDetailsByMunicipality.php?",
          method: "GET",
          crossDomain: true,
          data: municipality: city, distance:0
        )
        .done(function(output) 
            alert("OK!");
        )
        .fail(function() 
          // handle error response
          alert("KO!");
      )
    </script>
  </body>
</html>

...在这里发布...

http://www.mysite2.com/OpenProntoSoccorso/WebMapping/test2.html

在网络服务器(Ubuntu 14.04 上的 Apache 2.4)http://www.mysite1.org/.... 我已经以这种方式修改了我的 apache2.conf 文件....

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

按照这些说明How to allow Cross domain request in apache2。

我的网络浏览器控制台(网络选项卡..)中仍然出现以下错误

Failed to load http://www.mysite1.org/cesarefortelegram/Telegram/OpenProntoSoccorso/API/getProntoSoccorsoDetailsByMunicipality.php?&municipality=Torino&distance=0: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.mysite2.com' is therefore not allowed access.

我哪里做错了?

提前致谢

【问题讨论】:

【参考方案1】:

我发现我的 apache2.conf 文件中有错误....

这是正确的配置(在“www”之后忘记了“html”......对不起......)......

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

现在一切正常...

【讨论】:

【参考方案2】:

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

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

<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 标头

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

【讨论】:

ubuntu14.04配置apache支持多个站点

怎样在一个Ubuntu的机器上(虚拟机)配置Apache支持多个网站呢?比如你有一台独立的Ubuntu虚拟机,配有一个外网的IP(45.46.47.48),并且注册了两个域名AAA.com和BBB.com,将这两个域名DNS解析到你虚机的IP地址。假设你已经安装好了Apache... 查看详情

Moodle - 如何在 apache2 (ubuntu 14.04) 上启用斜线参数

】Moodle-如何在apache2(ubuntu14.04)上启用斜线参数【英文标题】:Moodle-Howtoenableslashargumentsonapache2(ubuntu14.04)【发布时间】:2015-06-2005:34:08【问题描述】:标题说明了一切:)在docs.moodle.org上,它说要在httpd.conf中包含“AcceptPathInfoOn”... 查看详情

sh让我们在apache上加密ubuntu14.04(代码片段)

查看详情

VirtualHost 始终在 Ubuntu 14.04 上使用 Apache 返回默认主机

】VirtualHost始终在Ubuntu14.04上使用Apache返回默认主机【英文标题】:VirtualHostalwaysreturnsdefaulthostwithApacheonUbuntu14.04【发布时间】:2014-07-0522:56:59【问题描述】:我尝试在默认的localhost之外设置一个虚拟主机。每当我尝试调用我的虚... 查看详情

text如何在ubuntu14.04上设置apache虚拟主机(代码片段)

查看详情

在运行 Apache2 + userdir 的 Ubuntu 服务器 14.04 上保护 $HOME 和 public_html

】在运行Apache2+userdir的Ubuntu服务器14.04上保护$HOME和public_html【英文标题】:Securing$HOMEandpublic_htmlonUbuntuserver14.04runningApache2+userdir【发布时间】:2015-05-1002:10:36【问题描述】:我在UbuntuServer14.04上运行Apache2+userdir。用户通过AD验证,... 查看详情

为 Ubuntu 14.04 安装和配置 xampp

】为Ubuntu14.04安装和配置xampp【英文标题】:InstallandconfigurexamppforUbuntu14.04【发布时间】:2017-09-0600:50:36【问题描述】:你好!我一直试图让PHP脚本在Ubuntu14.04上运行5天。我为这些版本安装了PHP5.3、apache2和xampp。我使用Netbeans使用p... 查看详情

检查 Apache 是不是安装在 Ubuntu 机器上

】检查Apache是不是安装在Ubuntu机器上【英文标题】:CheckifApacheisinstalledonaUbuntumachine检查Apache是否安装在Ubuntu机器上【发布时间】:2016-03-1917:46:29【问题描述】:有没有办法检查如果我的Ubuntu 14.04(TrustyTahr)机器上安装了Apache?我... 查看详情

php 脚本在 Ubuntu 14.04 服务器上运行,phpmyadmin 没有

】php脚本在Ubuntu14.04服务器上运行,phpmyadmin没有【英文标题】:phpscriptsrunonUbuntu14.04server,phpmyadmindoesnot【发布时间】:2015-02-1911:13:04【问题描述】:不知道该去哪里,到处寻找答案;所以,我想我会在这里尝试。我有一个运行apac... 查看详情

在 laravel @Apache2 Server Ubuntu 14.04 LTS 中将 Root 设置为公用文件夹

】在laravel@Apache2ServerUbuntu14.04LTS中将Root设置为公用文件夹【英文标题】:SettingtheRoottopublicfolderinlaravel@Apache2ServerUbuntu14.04LTS【发布时间】:2015-03-0812:24:18【问题描述】:我有以下配置,但我无法访问直接使用Laravel。在/etc/apache2/si... 查看详情

ubuntu14.04服务器环境下配置方法如下:(代码片段)

这篇文章主要介绍了Ubuntu14.04服务器环境下配置PHP7.0+Apache2+Mysql5.7的方法,较为详细的分析了Ubuntu14.04操作系统环境下配置PHP7.0+Apache2+Mysql5.7的具体步骤与相关命令使用技巧,需要的朋友可以参考下本文实例讲述了Ubuntu14.04服务器环境... 查看详情

无法让 Laravel 在 Ubuntu 上使用 apache

】无法让Laravel在Ubuntu上使用apache【英文标题】:Can\'tgetLaraveltoworkwithapacheonUbuntu【发布时间】:2014-10-1517:49:17【问题描述】:我已经尝试了几个小时让Laravel与运行Ubuntu14.04的Apache一起工作,但没有结果。如果我去site.dev,我试图... 查看详情

Ubuntu 14.04 Apache 2.4.7 404 未找到

】Ubuntu14.04Apache2.4.7404未找到【英文标题】:Ubuntu14.04Apache2.4.7404NotFound【发布时间】:2016-07-1717:24:42【问题描述】:我一直在阅读论坛并尝试复制他们的修复程序,但我肯定遗漏了一些东西,因为它会从简单的404ErrorNotFound切换到4... 查看详情

在 Ubuntu 上配置 apache ant 时出错

】在Ubuntu上配置apacheant时出错【英文标题】:ErrorwhileconfiguringapacheantonUbuntu【发布时间】:2015-08-0919:12:57【问题描述】:我正在尝试在我的Ubuntu中为python-for-android配置apacheant,因为它是它的prerequisites之一。我按照YouTube上的this教... 查看详情

PyQt 5.3.1 在 ubuntu 14.04 上安装

】PyQt5.3.1在ubuntu14.04上安装【英文标题】:PyQt5.3.1Installingonubuntu14.04【发布时间】:2014-09-0211:58:00【问题描述】:我正在尝试在ubuntu14.04上构建PyQt5.3.1。我需要QtWebKit模块。我按照步骤配置了它pythonconfigure.py-q/usr/lib/x86_64-linux-gnu/qt5... 查看详情

在 Ubuntu Server 14.04 上自动启动 Zookeeper 的最佳方法?

】在UbuntuServer14.04上自动启动Zookeeper的最佳方法?【英文标题】:BestwaytostartZookeeperautomaticallyonUbuntuServer14.04?【发布时间】:2015-05-1500:50:36【问题描述】:我在Ubuntu服务器14.04上使用sudoapt-getinstallzookeeper安装了Zookeeper。我无法理解... 查看详情

Ubuntu 14.04:Apache 2.4.7 虚拟主机不工作/重定向

】Ubuntu14.04:Apache2.4.7虚拟主机不工作/重定向【英文标题】:Ubuntu14.04:Apache2.4.7virtualhostnotworking/redirecting【发布时间】:2014-07-1006:07:57【问题描述】:我在我的Ubuntu14.04机器上安装了Apache2.4.7,但我的一些虚拟主机不同意我的看法... 查看详情

.htaccess 未在 Ubuntu 14.04 (Apache 2.4.7) 中加载

】.htaccess未在Ubuntu14.04(Apache2.4.7)中加载【英文标题】:.htaccessnotbeingloadedinUbuntu14.04(Apache2.4.7)【发布时间】:2014-11-0702:18:21【问题描述】:我正在尝试在Ubuntu14.04(Apache2.4.7)中使用.htaccess文件。这是我的.htaccess的样子#1YEAR<FilesMatch... 查看详情