前后端分离跨域 关于前后端分离开发环境下的跨域访问问题(angular proxy=>nginx )
前后端分离后遇到了跨域访问的问题;
angular1中使用proxy很麻烦,最后还是失败结束;最后总结3种方法如下;
本人使用的第一种方法,只是开发环境下使用很方便!
1:禁掉谷歌的安全策略(Turn off CORS)
For Windows 进入谷歌浏览器的安装目录下(我的目录如下 C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chrome.exe);然后命令行输入
--args --disable-web-security
C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chrome.exe --args --user-data-dir="C://Chrome dev session" --disable-web-security
可以将上述代码写进记事本保存为 .bat后缀名每次点击就可以启动了;
For OSX, open Terminal and run:
$ open -a Google\ Chrome --args --disable-web-security --user-data-dir
For Linux run:
$ google-chrome --disable-web-security
以上方法便可以开启一个关闭掉安全策略的浏览器(谷歌为例);这样就可以进行跨域访问了;
2:使用谷歌插件()
3:使用proxy
请参考http://oskarhane.com/avoid-cors-with-nginx-proxy_pass/;
Avoid CORS with Nginx proxy_pass
I recently had to make cross origin AJAX requests (CORS), which was fine since I had control over the API server and simply adding these headers will make modern browsers ask the API server for permission and then make the request.
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST,GET,PUT,DELETE
Access-Control-Allow-Headers: Authorization, Lang
But, of course, Internet Explorer want to be a pita and IE 8 & 9 does not support this (a part of it is supported, check out this table). And I have to support IE 9 at least.
So, nginx to the rescue like many times in the past.
I want to configure Nginx to take local requests made to /api/ and forward those to the api located at http://apiserver.com.
This is what I have in Nginx since the website is up and running (for all browsers except IE 8&9).
server {
charset UTF-;
listen ;
root /home/web/myclient;
index index.html;
server_name myclient.com;
location ~ /\. {
deny all;
}
location / {
try_files $uri;
}
}
It basically says: listen to myclient.com on port 80 and deny all requests made to files starting with a dot and serve all other files matching filenames from the dir /home/web/myclient.
Alright, lets change so calls starting with /api/ forwards the call to the API server (not including the /api/ part of the requested path). The trailing slash on the /api/ and proxy_pass http://api_server/; are important here.
We’re adding an upstream and a location block.
upstream api_server {
server apiserver.com;
}
server {
charset UTF-;
listen ;
root /home/web/myclient;
index index.html;
server_name myclient.com;
location /api/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://api_server/;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_redirect off;
}
location ~ /\. {
deny all;
}
location / {
try_files $uri;
}
}
Now we just restart the server and it should work to make AJAX calls to http://myclient.com/api/users to get all users.
----------------------------------------------------------
| 转载文章请注明出处! |
----------------------------------------------------------
前后端分离跨域 关于前后端分离开发环境下的跨域访问问题(angular proxy=>nginx )的更多相关文章
- VUE在开发环境下实现跨域
1. 跨域设置 VUE项目的 config文件夹下index.js文件中修改 dev: proxyTable中的内容(默认是没有内容的): 添加内容: '/list': { target: 'http ...
- vue2.0开发环境下解决跨域问题
1.找到vue 项目下的配置文件 /config/index.js 2.找到 proxyTable 配置项 proxyTable: { '/api': { target: 'http://www.xx ...
- vue dev 环境下的跨域访问
概述:被dev环境下的跨域弄晕了好几天,build环境还在研究中 1.config--->index.js---->module.exports---->dev 2.在main.js ...
- 关于vue项目中axios跨域的解决方法(开发环境)
1.在config文件中修改index.js proxyTable: { "/api":{ target: 'https://www.baidu.com/muc/',//你需要跨域 ...
- springMVC前后端分离开发模式下支持跨域请求
1.web.xml中添加cors规则支持(请修改包名) <filter> <filter-name>cors</filter-name> <filter-cl ...
- Vue---vue-cli 中的proxyTable解决开发环境中的跨域问题
使用vue+vue-cli+axios+element-ui开发后台管理系统时,遇到一个问题,后台给了一个接口,我这边用axios请求数据,控制台总是报405错误和跨域错误 错误 405? 没见过!! ...
- Windows 环境下分布式跨域Session共享(转)
出处:http://www.cnblogs.com/stangray/p/3328092.html 为什么还是那句话,在网上找了N篇Session共享,但真正可以直接解决问题的还是没有找到. 一.以下 ...
- Windows 环境下分布式跨域Session共享
为什么还是那句话,在网上找了N篇Session共享,但真正可以直接解决问题的还是没有找到. 一.以下为本人亲测,为防止环境不一致,对本文产生歧义,限定环境如下: 1. IIS7.0 2. Asp.ne ...
- vue-cli 初始化项目时开发环境中的跨域问题
最近刚刚完成自己的毕业设计(基于Vue的信息资讯展示与管理平台),于是想整理一下过程遇到的一些问题. 项目基于Vue开发,使用 Vue-cli 初始化项目文件目录时默认占用8080端口,而我又想使用 ...
随机推荐
- 知网下载pdf文件的方法
title: 知网下载pdf文件的方法 toc: false date: 2018-11-02 17:54:43 categories: methods tags: 知网 平时我们使用的是国内版的知网 ...
- Elasticsearch部署异常Permission denied
异常描述 在Linux上部署ElasticSearch时抛出了一个异常如下: log4j:ERROR setFile(null,true) call failed. java.io.FileNotFo ...
- ROS-SLAM仿真-hector
前言:hector_slam可以很好的在空中机器人,手持构图设备及特种机器人中运行. hector_slam不需要订阅里程计信息/odmo消息,而是直接使用激光估算里程计信息,因此,当机器人速度较快时 ...
- jsoup HTML parser hello world examples--转
原文地址:http://www.mkyong.com/java/jsoup-html-parser-hello-world-examples/ Jsoup, a HTML parser, its “j ...
- Function 和 eval 知识点总结
1 Function 1.1 函数的创建方式 1 函数声明 2 函数表达式 3 new Function // 1 function foo() {} // 2 var foo = function( ...
- Apache2.2伪静态配置
最近由于工作的需要要配置一下Apache的伪静态化,在网上搜了好多都无法完成,所以觉得有必要在这里写一下. 第一步:打开Apache的httpd.conf文件,把LoadModule rewrite_ ...
- ZBrush中如何使用套索工具绘制遮罩
ZBrush®中创建遮罩的方法有很多,可以手动创建矩形遮罩.圆形遮罩和图案遮罩,然而这些遮罩都是固定的形状.使用Zbrush中的套索遮罩能够实现不规则的图形遮罩,游刃有余的发挥创作. 使用套索工具绘制 ...
- java的算法实现冒泡
package xutao3;public class test1 { public static void main(String[] args) { int[] arr={12,88,66,55, ...
- nefu 84 ( 拓展欧几里德模板题 )
链接:传送门 思路:拓展欧几里德模板题,设大圣至少翻转 t 次,大圣起始位置为 x ,大圣目标位置为 y + n * s ( 大圣到达目标位置 y 可能需要多圈,所以用 s 来表示圈数 ),因为只能逆 ...
- 正式版的Linux Kernel 5.1来了,非LTS
大神Linus Torvalds于今天发布了Linux Kernel 5.1内核正式版,在对现有功能进行改进的同时还带来了很多重要的改进.本次版本更新历时一个半月,不过值得注意的是它并非是长期支持版本 ...