這篇文章主要介紹了詳解nginx代理天地圖作緩存解決跨域問題,小編以爲挺不錯的,如今分享給你們,也給你們作個參考。一塊兒跟隨小編過來看看吧
做爲一個GISer開發者,天地圖是常常在項目中以底圖的形式出現,其加載地址如:nginx
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個服務器名稱t0,t1,t2…瀏覽器
下面是我以openlayers加載天地圖過程當中遇到跨域問題緩存
一、錯誤的產生條件服務器
// 採用openlayers加載天地圖var layer = new ol.layer.Tile({ source: new ol.source.XYZ({ // crossOrigin: ‘Anonymous’, // 是否請求跨域操做 url: url // 天地圖地址 })});
若是沒有用到crossOrigin屬性就不會產生跨域問題,通常這個參數也不會設置。app
這個參數使用場景以下官網所述:tcp
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. http://www.iis7.com/b/ssyqdq/ide
查閱MDN文檔(https://developer.mozilla.org/zh-CN/docs/Web/HTML/CORS_settings_attributes),能夠發現crossOrigin有兩個取值google
在開發過程當中,每每須要本地運行開發版,服務器運行生產版。當兩個版本在同一個瀏覽器中訪問時,設置了crossOrigin就會出現跨域問題,以下圖所示的錯誤,url
has been blocked by CORS policy: No 'Access-Control-Allow-Origin’header is present on the requested resource.
注:只有天地圖設置了crossOrigin以後會出現這個問題,谷歌底圖是不會出現的,緣由是:
天地圖在返回的request header的Origin屬性設置成當前訪問的IP,而google底圖Origin屬性設置的是*,意味着不一樣IP的系統在瀏覽器緩存了google瓦片以後依然能訪問google底圖。
二、錯誤解決的方法
2.1 簡單暴力的方法
簡單暴力的解決方法就是清除瀏覽器的緩存圖片,在同一時刻,只查看一個其中的一個系統,若是要查看另外一個系統,必須事先清除瀏覽器圖片緩存
2.2 刪除CrossOrigin屬性
從新審視一遍地圖需求,判斷是否真的須要crossOrigin屬性,若是不須要,就根本不會出現這個問題
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 - remoteuser[remote_user [remoteuser[time_local] 「KaTeX parse error: Expected 'EOF', got '#' at position 12: request" ' #̲ 'status bodybytessent"body_bytes_sent "bodybytessent"http_referer」 ’ # ‘「httpuseragent""http_user_agent" "httpuseragent""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; # 關鍵代碼塊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.comweight=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; # 關鍵代碼塊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 uriuriuriis_args$args; proxy_pass http://tianditu_server/DataServer; } }}