最近在写自己的博客,涉及到跨域的问题,自己捣鼓许久,终于解决了。然后总结一下,记录一下,日后遇到类似的问题的时候也可以得到一些启发。
一、什么是跨域
跨域,指的是浏览器不能执行其他网站的脚本。它是由浏览器的同源策略造成的,是浏览器对javascript施加的安全限制。
所谓同源是指,域名,协议,端口都相同。浏览器执行javascript脚本时,会检查这个脚本属于哪个页面,如果不是同源页面,就不会被执行。
二、跨域问题的解决方案
1、 通过jsonp跨域
2、 document.domain + iframe跨域
3、 location.hash + iframe
4、 window.name + iframe跨域
5、 postMessage跨域
6、 跨域资源共享(CORS)
7、 nginx代理跨域
8、 nodejs中间件代理跨域
9、 WebSocket协议跨域
三、配置Nginx反向代理解决跨域
1、登录服务器,进入到Nginx配置文件: cd /etc/nginx/conf.d
2、打开要配置跨域的相应域名之下:
3、将以下配置信息加进去,因为这个域名我还加上了ssh的配置,所以篇幅看着有点长:
upstream server { server 127.0.0.1:3000; } server { listen 80; server_name server.locusy.top; rewrite ^(.*)$ https://$host$1 permanent; location / { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true'; 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') { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true'; 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'; return 204; } proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forward-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-Nginx-Proxy true; proxy_pass http://47.94.208.76:3000; proxy_redirect off; } } server { listen 443; server_name server.locusy.top; ssl on; ssl_certificate cert/1754906_server.locusy.top.pem; ssl_certificate_key cert/1754906_server.locusy.top.key; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; location / { add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Credentials true; 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') { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true'; 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'; return 204; } proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forward-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-Nginx-Proxy true; proxy_pass http://47.94.208.76:3000; proxy_redirect off; } }
四、nodejs后端插件解决跨域
const cors = require('koa-cors'); app.use(cors({ origin: '*', exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'], maxAge: 500, credentials: true, //cookie allowMethods: ['GET', 'POST', 'DELETE', 'OPTIONS', 'PUT'], allowHeaders: ['Content-Type', 'Authorization', 'Accept'] }));
五、前端axios设置
// axios.defaults.withCredentials = true