濮阳杆衣贸易有限公司

主頁 > 知識庫 > 詳解nginx代理天地圖做緩存解決跨域問題

詳解nginx代理天地圖做緩存解決跨域問題

熱門標(biāo)簽:如何分析地圖標(biāo)注 電銷機器人價值 高德地圖標(biāo)注好做嗎 大連400電話如何申請 外呼系統(tǒng)坐席費計入會計哪個科目 撫順地圖標(biāo)注 新余高德地圖標(biāo)注怎么修改 達亞電銷機器人官網(wǎng) 電銷機器人怎么接線路

作為一個GISer開發(fā)者,天地圖是經(jīng)常在項目中以底圖的形式出現(xiàn),其加載地址如:

1.天地圖矢量:http://t{0-6}.tianditu.com/DataServer?T=vec_w&x={x}&y={y}&l={z}
2.天地圖影像:http://t{0-6}.tianditu.com/DataServer?T=img_w&x={x}&y={y}&l={z}
3.天地圖地形:http://t{0-6}.tianditu.com/DataServer?T=ter_w&x={x}&y={y}&l={z}

其中t{0-6}是天地圖提供的7個服務(wù)器名稱t0,t1,t2....

下面是我以openlayers加載天地圖過程中遇到跨域問題

1、錯誤的產(chǎn)生條件

// 采用openlayers加載天地圖
var layer = new ol.layer.Tile({
  source: new ol.source.XYZ({
    // crossOrigin: 'Anonymous', // 是否請求跨域操作
    url: url // 天地圖地址
  })
});

如果沒有用到crossOrigin屬性就不會產(chǎn)生跨域問題,一般這個參數(shù)也不會設(shè)置。

這個參數(shù)使用場景如下官網(wǎng)所述:

The crossOrigin attribute for loaded images. Note that you must provide a crossOrigin value if you are using the WebGL renderer or if you want to access pixel data with the Canvas renderer. See https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image for more detail.

查閱MDN文檔(https://developer.mozilla.org/zh-CN/docs/Web/HTML/CORS_settings_attributes),可以發(fā)現(xiàn)crossOrigin有兩個取值

在開發(fā)過程中,往往需要本地運行開發(fā)版,服務(wù)器運行生產(chǎn)版。當(dāng)兩個版本在同一個瀏覽器中訪問時,設(shè)置了crossOrigin就會出現(xiàn)跨域問題,如下圖所示的錯誤,

has been blocked by CORS policy: No 'Access-Control-Allow-Origin'header is present on the requested resource.

注:只有天地圖設(shè)置了crossOrigin之后會出現(xiàn)這個問題,谷歌底圖是不會出現(xiàn)的,原因是:

天地圖在返回的request header的Origin屬性設(shè)置成當(dāng)前訪問的IP,而google底圖Origin屬性設(shè)置的是*,意味著不同IP的系統(tǒng)在瀏覽器緩存了google瓦片之后依然能訪問google底圖。

2、錯誤解決的方法

2.1 簡單暴力的方法

簡單暴力的解決方法就是清除瀏覽器的緩存圖片,在同一時刻,只查看一個其中的一個系統(tǒng),如果要查看另一個系統(tǒng),必須事先清除瀏覽器圖片緩存

2.2 刪除CrossOrigin屬性

重新審視一遍地圖需求,判斷是否真的需要crossOrigin屬性,如果不需要,就根本不會出現(xiàn)這個問題

2.3 nginx代理解決

如果前面的方法都感覺不合適,那就用nginx來代理解決吧,它可以解決跨域問題,也可以將瓦片緩存至本地,加快訪問速度。

直接上配置文件哈。

#user nobody;
worker_processes 4;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid    logs/nginx.pid;


events {
  worker_connections 1024;
}

http {
  include    mime.types;
  default_type application/octet-stream;

  #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  #         '$status $body_bytes_sent "$http_referer" '
  #         '"$http_user_agent" "$http_x_forwarded_for"';

  #access_log logs/access.log main;

  sendfile    on;
  #tcp_nopush   on;

  #keepalive_timeout 0;
  keepalive_timeout 65;

  #gzip on;
  
  client_max_body_size 20M;
  
   # 關(guān)鍵代碼塊1
  proxy_temp_path ../proxy_cache/tianditu_temp;
  proxy_cache_path ../proxy_cache/tianditu levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
  upstream tianditu_server {
    server t0.tianditu.com weight=1 max_fails=2 fail_timeout=30s;
    server t1.tianditu.com weight=1 max_fails=2 fail_timeout=30s;
    server t2.tianditu.com weight=1 max_fails=2 fail_timeout=30s;
    server t3.tianditu.com weight=1 max_fails=2 fail_timeout=30s;
    server t4.tianditu.com weight=1 max_fails=2 fail_timeout=30s;
    server t5.tianditu.com weight=1 max_fails=2 fail_timeout=30s;
    server t6.tianditu.com weight=1 max_fails=2 fail_timeout=30s;
  }
  
  server {
    listen    8088;
    server_name localhost;

    #charset koi8-r;

    #access_log logs/host.access.log main;

     # 關(guān)鍵代碼塊2
    location /DataServer {
      more_set_headers 'Access-Control-Allow-Origin: *';
      add_header Access-Control-Allow-Headers X-Requested-With;
      add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
      
      proxy_cache cache_one;
      proxy_cache_key $uri$is_args$args;
      proxy_pass http://tianditu_server/DataServer;
    }
  }

}

下面解釋一下配置文件:

關(guān)鍵代碼塊1:

1、采用nginx upstream配置一組服務(wù)地址,做負(fù)載均衡用,其效果優(yōu)于openlayers順序遍歷t0至t6

2、設(shè)置了代理緩存臨時地址和緩存地址,這里可以采用相對路徑

關(guān)鍵代碼塊2

匹配到DataServer之后,需要

1、設(shè)置跨域header,這里用了一個新的nginx模塊——headers-more,需要在編譯nginx時加入,如果是windows下用nginx,可以用這個網(wǎng)站的安裝包:https://openresty.org,它預(yù)編譯了很多nginx實用模塊

2、用proxy_pass將地址代理到 http://tianditu_server/DataServer地址上,其中tianditu_server就是上面配置負(fù)載均衡的服務(wù)組名稱。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

標(biāo)簽:黃石 海東 遼源 楊凌 衡水 南通 湖南 新鄉(xiāng)

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《詳解nginx代理天地圖做緩存解決跨域問題》,本文關(guān)鍵詞  詳解,nginx,代理,天,地圖,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《詳解nginx代理天地圖做緩存解決跨域問題》相關(guān)的同類信息!
  • 本頁收集關(guān)于詳解nginx代理天地圖做緩存解決跨域問題的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    扶绥县| 冷水江市| 香港 | 临沭县| 富裕县| 常熟市| 无锡市| 温宿县| 自贡市| 定远县| 万源市| 七台河市| 金湖县| 神池县| 年辖:市辖区| 盐津县| 康平县| 霞浦县| 双柏县| 黔西县| 延边| 昌都县| 永兴县| 井研县| 凤冈县| 荔浦县| 若尔盖县| 甘孜县| 肥城市| 依兰县| 化德县| 红河县| 蕲春县| 和田市| 独山县| 定结县| 东至县| 永宁县| 兰州市| 沁水县| 玉门市|