找到apache配置文件httpd.conf

找到這行

#LoadModule headers_module modules/mod_headers.so

把#注釋符去掉

LoadModule headers_module modules/mod_headers.so

目的是開啟apache頭信息自定義模塊

在獨立主機配置文件中新增header

Header set Access-Control-Allow-Origin *

例如:

<VirtualHost *:88>
 ServerAdmin admin@example.com
 DocumentRoot "****************"
 ServerName www.jb51.com
 Header set Access-Control-Allow-Origin *

 ErrorLog "***********"
 CustomLog "****************************" common
<Directory "**************">
 SetOutputFilter DEFLATE
 Options FollowSymLinks ExecCGI
 Require all granted
 AllowOverride All
 Order allow,deny
 Allow from all
 DirectoryIndex index.html index.php
</Directory>
</VirtualHost>
ApacheCopy

意思是對這個域名的資源進行訪問時,添加一個頭信息

重啟apache

service httpd restart

2. nginx靜態(tài)資源允許跨域訪問

同理 找到相應(yīng)域名配置文件

在server模塊中添加配置:

add_header ‘Access-Control-Allow-Origin' ‘*';

例:

server {
    listen    80;
    add_header 'Access-Control-Allow-Origin' '*';
    location /Roboto/ {
      root  /home/images;
      autoindex on;
    }
  }

nginx重載

./nginx -s reload

通過以上方法配置完后,再次跨域訪問靜態(tài)資源就沒有問題了

贊(0)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享網(wǎng)絡(luò)內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-62778877-8306;郵箱:fanjiao@west.cn。本站原創(chuàng)內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明出處:西部數(shù)碼知識庫 » 解決nginx/apache靜態(tài)資源跨域訪問問題詳解

登錄

找回密碼

注冊