Nginx映射本地静态资源时,浏览器提示跨域问题解决
生活随笔
收集整理的這篇文章主要介紹了
Nginx映射本地静态资源时,浏览器提示跨域问题解决
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
場(chǎng)景
本地html中請(qǐng)求磁盤(pán)中的靜態(tài)資源文件,可以使用Nginx做靜態(tài)資源映射,
可以將本地磁盤(pán)文件映射為網(wǎng)絡(luò)url??墒怯捎跒g覽器的跨域機(jī)制會(huì)提示:
from orihin 'null' has been blocked by CROS policy: Cross origin requersts are only
supported for....
?
注:
博客:
BADAO_LIUMANG_QIZHI的博客_霸道流氓氣質(zhì)_CSDN博客-C#,SpringBoot,架構(gòu)之路領(lǐng)域博主
關(guān)注公眾號(hào)
霸道的程序猿
獲取編程相關(guān)電子書(shū)、教程推送與免費(fèi)下載。
實(shí)現(xiàn)
1、編輯nginx.conf,添加如下代碼
add_header Access-Control-Allow-Origin *;add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';if ($request_method = 'OPTIONS') {return 204;}2、完整server代碼
server {add_header Access-Control-Allow-Origin *;add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';if ($request_method = 'OPTIONS') {return 204;}listen 120;server_name 127.0.0.1;#charset koi8-r;#access_log logs/host.access.log main;location / {root D:/WorkSpace/Gitee/three-js-study/assets/;try_files $uri $uri/ /index.html;index index.html index.htm;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}總結(jié)
以上是生活随笔為你收集整理的Nginx映射本地静态资源时,浏览器提示跨域问题解决的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Java中Map类型数据使用Linked
- 下一篇: 2017年html5行业报告,云适配发布