为什么会产生跨域(同源策略):
虑到浏览器的安全问题,没有同源策略容易受到网络攻击。
同源策略限制的内容:
本地存储,dom节点,ajax的请求;
跨域产生条件:
协议、域名、端口任意一个不同则会产生跨域。
跨域并不是请求发不出去,请求能发出去,服务端能收到请求并正常返回结果,只是结果被浏览器拦截了;表单不存在跨域问题,因为提交的form表单数据不需要返回。
nginx访问前端静态资源
第一种:在本地有文件的,比如dist文件
server {
listen 8081;
server_name localhost;
// 请求接口跨域
location ~ ^/api {
proxy_pass https://xxx; // 请求接口的地址
}
// 拿取静态资源
## 端口随意 资源在D:/code/faq-center/dist目录下
## 入口文件 本地刷新避免出现404:try_files $uri /index.html;
location /welcome {
alias D:/code/faq-center/dist;
index index.html;
try_files $uri /index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
第二种:
server {
listen 8081;
server_name localhost;
location ~ ^/api {
proxy_pass https://xxx;
}
location / {
proxy_pass http://localhost:3000;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}