前后端分离跨域 关于前后端分离开发环境下的跨域访问问题(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端口,而我又想使用 ...
随机推荐
- SAM学习笔记
SAM学习笔记 后缀自动机(模板)NSUBSTR(Caioj1471 || SPOJ 8222) [题意] 给出一个字符串S(S<=250000),令F(x)表示S的所有长度为x的子串中,出现次 ...
- Weex学习与实践(一):Weex,你需要知道的事
Weex学习与实践(一):Weex,你需要知道的事 http://coderyi.com/posts/weex1/ 1.命令行工具:weex-toolkit https://github.com/w ...
- 13. Roman to Integer[E]罗马数字转整数
题目 Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...
- python中struct模块
# #********struct模块********# # 1.按照指定格式将Python数据转换为字符串,该字符串为字节流,如网络传输时, # 不能传输int,此时先将int转化为字节流,然后再发 ...
- SQL Server 内存使用情况
• 查看设置的最大与最小内存: exec sp_configure 'max server memory (MB)' exec sp_configure 'min server memory (MB) ...
- 开发工具 | 利用 deployd 搭建个人博客
前端er,通过利用deployd + mongodb,实现可视化的接口编写,自定义mock数据,数据存储在mongodb中:deployd目前还没有中文网,更多信息请参考http://deployd. ...
- Java标识符规范
1.标识符用来定义包名,类名,方法名,变量名,常量名. 2.标识符必须由字母.下划线.$符号组成,不能以数字开头.不能是Java中的保留关键字.
- Python学习——爬虫篇
requests 使用requests进行爬取 下面是我编写的第一个爬虫的脚本 import requests # 导入reques ...
- python matplotlib数据可视化
#基于python3 Matplotlib构建的3D图形: 使用pycharm的小伙伴把sciview给关掉: 因为sciview显示的是png图片.3d图形一般我们都需要拖拖拽拽的. 参见: htt ...
- Java web课程学习之JSP
JSP jsp隐式对象:JSP隐式对象是JSP容器为每个页面提供的Java对象,开发者可以直接使用它们而不用显式声明.JSP隐式对象也被称为预定义变量. jsp脚本片段 l jsp脚本片段是指 ...