访问运行不同端口apache2

时间:2017-01-21 02:03:00

标签: php html configuration apache2

我目前正在运行一个来自raspberry pi(32 GB)的主服务器和一个作为文件存储服务器运行的辅助服务器(4 TB)。两台服务器都安装了运行Apache2的Debian Linux。我已经成功地使用以下代码将来自辅助服务器的PHP输出引入主服务器:

主服务器:

<?php
    $results = file_get_contents('http://example.org:700/handle.php');
    echo $results;
?>

辅助服务器

<?php
    header('Access-Control-Allow-Origin: http://example.org');

    //Rest of code
?>

所以我遇到了主服务器能够显示图片并从辅助服务器播放mp3 / wav的问题,但在我的情况下,它不适用于FLAC(带有一些javascript代码),它只是向我展示了这个错误:

XMLHttpRequest cannot load http://example.org:700/music/soundfile.flac. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://example.org' is therefore not allowed access.

我知道javascript编码没有任何问题,因为它会播放来自其他网站的音乐,比如这个例子:Debussy_-_Pour_les_accords.flac并且之前工作得很好。

我只是希望允许两台服务器之间的所有流量都是安全的,并且没有Access-Allow-Origin错误。它是apache2.conf或其他配置文件中的东西可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

https://stackoverflow.com/a/35707821/6802309

看起来很简单。 这是如何解决这个问题:

  1. sudo nano /etc/apache2/apache2.conf

  2. 在底部输入此代码:

  3. <IfModule mod_headers.c>
        Header set Access-Control-Allow-Origin: "http://example.org"
        Header set Access-Control-Allow-Credentials: true
        Header set Access-Control-Expose-Headers: "Content-Length"
        Header set Access-Control-Allow-Methods: "POST, GET, PUT, OPTIONS, DELETE, HEAD"
        Header set Access-Control-Allow-Headers: "Range"
    </IfModule>
    
    1. 在配置文件中启用标头:sudo a2enmod headers

    2. 检查错误:sudo apachectl -k graceful

    3. sudo service apache2 reload

    4. sudo service apache2 restart

    5. 完成