前后端分离后遇到了跨域访问的问题;

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 )的更多相关文章

  1. VUE在开发环境下实现跨域

    1. 跨域设置 VUE项目的 config文件夹下index.js文件中修改 dev: proxyTable中的内容(默认是没有内容的): 添加内容: '/list': { target: 'http ...

  2. vue2.0开发环境下解决跨域问题

    1.找到vue 项目下的配置文件 /config/index.js 2.找到 proxyTable 配置项 proxyTable: { '/api': { target: 'http://www.xx ...

  3. vue dev 环境下的跨域访问

    概述:被dev环境下的跨域弄晕了好几天,build环境还在研究中 1.config--->index.js---->module.exports---->dev 2.在main.js ...

  4. 关于vue项目中axios跨域的解决方法(开发环境)

    1.在config文件中修改index.js proxyTable: { "/api":{ target: 'https://www.baidu.com/muc/',//你需要跨域 ...

  5. springMVC前后端分离开发模式下支持跨域请求

    1.web.xml中添加cors规则支持(请修改包名) <filter> <filter-name>cors</filter-name> <filter-cl ...

  6. Vue---vue-cli 中的proxyTable解决开发环境中的跨域问题

    使用vue+vue-cli+axios+element-ui开发后台管理系统时,遇到一个问题,后台给了一个接口,我这边用axios请求数据,控制台总是报405错误和跨域错误 错误 405? 没见过!! ...

  7. Windows 环境下分布式跨域Session共享(转)

    出处:http://www.cnblogs.com/stangray/p/3328092.html 为什么还是那句话,在网上找了N篇Session共享,但真正可以直接解决问题的还是没有找到. 一.以下 ...

  8. Windows 环境下分布式跨域Session共享

    为什么还是那句话,在网上找了N篇Session共享,但真正可以直接解决问题的还是没有找到. 一.以下为本人亲测,为防止环境不一致,对本文产生歧义,限定环境如下: 1. IIS7.0 2. Asp.ne ...

  9. vue-cli 初始化项目时开发环境中的跨域问题

    最近刚刚完成自己的毕业设计(基于Vue的信息资讯展示与管理平台),于是想整理一下过程遇到的一些问题. 项目基于Vue开发,使用 Vue-cli 初始化项目文件目录时默认占用8080端口,而我又想使用 ...

随机推荐

  1. xargs用例一个

    ls -a *.doc|awk -F. '{print $1}' |xargs -I {} java -jar ~/soft/jodconverter-2.2.2/lib/jodconverter-c ...

  2. javascript系列-class12.事件

    1.默认行为          什么是默认行为:默认行为就是浏览器自己触发的事件.比如:a链接的跳转,form提交时的跳转,鼠标右键跳转:   oncontexmenu当点击右键菜单的时候:   re ...

  3. Redis学习笔记(五) 基本命令:Hash操作

    原文链接:http://doc.redisfans.com/hash/index.html 学习前先明确一下概念,这里我们把Redis的key称作key(键),把数据结构hash中的key称为fiel ...

  4. C++中的pair,make_pair学习

    std::pair主要的作用是将两个数据组合成一个数据,两个数据可以是同一类型或者不同类型.例如std::pair<int,float> 或者 std::pair<double,do ...

  5. kettle的下载、安装和初步使用(Ubuntu 16.04平台下)(图文详解)

    不多说,直接上干货! 能够看我这篇博客的博友们,想必是已经具备一定基础了. 扩展博客 kettle的下载.安装和初步使用(windows平台下)(图文详解) kettle的下载 žKettle可以在h ...

  6. Java基础——GridBagLayout布局

    1.GridBagLayout布局管理器非常灵活,每个 GridBagLayout 对象维持一个动态的矩形单元网格: 2.需要和它的约束类(GridBagConstraints类)一起使用: 3.Gr ...

  7. EasyUI——DataGrid的自定义单元格点击事件

    1.当点击的单元格需要传递参数,并且传递的是row的值时,需要进行转义 function initCompareTable(){ $("#deviceCompareTable"). ...

  8. 判断浏览器是PC设备还是移动设备

    var browser={ versions:function(){ var u = navigator.userAgent, app = navigator.appVersion; return { ...

  9. HDU 1558 Segment set( 判断线段相交 + 并查集 )

    链接:传送门 题意:输入一个数 n 代表有 n 组操作,P 是在平面内加入一条线段,Q x 是查询第 x 条线段所在相交集合的线段个数 例如:下图 5 与 1.2 相交,1 与 3 相交,2 与 4 ...

  10. Golang-and-package-version-managment

    参考文章 学习Golang之后对golang中的版本管理,包管理等机制一直没有很好的琢磨,偶然想起还是觉得很有必要进行归纳,包管理使用起来简单,无非就是install,uninstall,list等, ...