最近有同事提出在使用客户端跨域访问的时候,发现服务器对option请求返回了403,后来查看了网络添加了一段配置,发现option服务返回204了,但是后续的put操作也直接返回了204导致无法使用图片上传功能,经过一番查询才发现,原来put等请求也需要定义,不然会直接使用option那段配置的请求

#首先nginx需要支持dav_module模块

./configure --prefix=/home/zqlx/apps/usr/webserver/nginx-1.12.0 --with-http_stub_status_module --with-http_ssl_module --user=zqlx --group=zqlx --with-pcre --with-pcre-jit --add-module=/tmp/nginx_upstream_check_module-master --with-stream --with-http_dav_module

#配置文件加上以下配置

location /aaa {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
#if ($request_method = "OPTIONS") {
# add_header 'Access-Control-Allow-Origin' "*";
# add_header 'Access-Control-Allow-Credentials' "true";
# add_header 'Access-Control-Max-Age' 86400;
# add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, DELETE';
# add_header 'Access-Control-Allow-Headers' 'reqid, nid, host, x-real-ip, x-forwarded-ip, event-type, event-id, accept, content-type';
# add_header 'Content-Length' 0;
# add_header 'Content-Type' 'application/json, charset=utf-8';
# return 204;
#}
dav_methods PUT DELETE;
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST,PUT,DELETE,OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
add_header 'Access-Control-Max-Age' 3600;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST,PUT,DELETE,OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST,DELETE,PUT,OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
if ($request_method = 'PUT') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST,PUT,DELETE,OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
if ($request_method = 'DELETE') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST,DELETE,PUT,OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
} proxy_pass http://aaa/aaa;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

我这里是允许所有,也可以指定域名

nginx实现跨域访问并支持(GET, POST,PUT,DELETE, OPTIONS)的更多相关文章

  1. nginx配置跨域访问

    前端要在本地测试ajax接口,无法跨域访问,所以在测试环境的nginx配置了跨域支持,方法如下: 在nginx.conf文件, http块下配置 42 #support cross domain ac ...

  2. jquery + node 通过 CORS 实现跨域访问,支持cookie和自定义header

    跨域有多种方式,现在的情况看来还是CORS更适合一些,有很多优点,比如浏览器正式支持.支持post.可以控制跨域访问的网站等. 我们来看看node如何实现cors方式的跨域.在网上找到了一些代码,考过 ...

  3. Nginx允许跨域访问的配置问题

    网站项目动静分离,静态资源服务器A 业务服务器B  B中静态资源由A加载 出现如下问题: @font-face { font-family: 'iconfont'; src: url('../font ...

  4. SpringMVC对于跨域访问的支持

    原文地址:http://docs.spring.io/spring/docs/5.0.0.RC2/spring-framework-reference/web.html#mvc-introductio ...

  5. Nginx开启跨域访问

    CORS on Nginx The following Nginx configuration enables CORS, with support for preflight requests. # ...

  6. Spring Boot 2中对于CORS跨域访问的快速支持

    原文:https://www.jianshu.com/p/840b4f83c3b5 目前的程序开发,大部分都采用前后台分离.这样一来,就都会碰到跨域资源共享CORS的问题.Spring Boot 2 ...

  7. 详解SpringBoot应用跨域访问解决方案

    一.什么是跨域访问 说到跨域访问,必须先解释一个名词:同源策略.所谓同源策略就是在浏览器端出于安全考量,向服务端发起请求必须满足:协议相同.Host(ip)相同.端口相同的条件,否则访问将被禁止,该访 ...

  8. Angular2中对ASP.NET MVC跨域访问

    应用场景 项目开发决定使用angular2进行前后端分离开发,由我负责后端服务的开发,起初选择的是web api进行开发.对跨域访问通过API中间件+过滤器对跨域访问进行支持.开发一段后,通知需要移植 ...

  9. 【转】silverlight 跨域访问

    作者:MIDI  来源:博客园  发布时间:2010-01-01 17:39  阅读:204 次  原文链接   [收藏]    在 Silverlight 使用 WebService .WCF.We ...

随机推荐

  1. CS5265 新出TYPEC转HDMI 4K60 高清投屏转接方案|可替代RTD2172

    CS5265是一种高度集成的单芯片,主要用于设计typec转HDMI转接线或者typeC转HDMI转换器,应用在各种手机或者电脑显示端设备当中.用CS5265设计的TYPEC转HDMI 4K高清投屏线 ...

  2. Android程序设计基础 • 【第1章 Android程序入门】

    全部章节   >>>> 本章目录 1.1 Android 平台介绍 1.1.1 Android 简介 1.1.2 Android 平台的发展 1.1.3 Android 平台架 ...

  3. jsp:useBean的scope属性

    目录 1.分类 2.page 3.request 4.session 5.application 1.分类 对于jsp:useBean创建的对象,由jsp:useBean的可选属性scope指定,可以 ...

  4. playwright--自动化(一):快速上手

    Playwright为现代 Web 应用程序提供可靠的端到端测试. 在JavaScript 和 TypeScript.Python..NET和Java 中都可以使用 Playwright 本人选择py ...

  5. 在 GitHub 复活 80 年代的游戏代码,它们出自第一本售出百万册的计算机书籍

    今儿我在 GitHub 看到了一个很眼熟的名字和头像,但是第一时间没想起来他是谁.算了先看看是个什么神仙开源项目,竟然能登上今天的 GitHub 趋势榜首. 该项目是把<BASIC Comput ...

  6. Cause: org.postgresql.util.PSQLException: 栏位索引超过许可范围:13,栏位数:12

    Cause: org.postgresql.util.PSQLException: 栏位索引超过许可范围:13,栏位数:12. 这个报错的原因是在mapper文件中的sql语书写错误 <inse ...

  7. (onlyoffice)在CentOS及其衍生产品上安装Linux的Document Server

    介绍 Document Server是一个在线办公套件,包括用于文本,电子表格和演示文稿的查看器和编辑器,与Office Open XML格式完全兼容:.docx,.xlsx,.pptx,并支持实时协 ...

  8. 怎样修改自己的登录shell

    问题 我只是Linux系统的普通用户,没有权限调用chsh命令修改自己的登录shell. bash-4.1$ chsh --help Usage: chsh [ -s shell ] [ --list ...

  9. web.xml文件配置模板

    直接贴完整代码,当然,spring的核心控制器依赖包需要通过mean提前配置 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.// ...

  10. 第10组 Beta冲刺 (5/5)(组长)

    1.1基本情况 ·队名:今晚不睡觉 ·组长博客:https://www.cnblogs.com/cpandbb/p/14018671.html ·作业博客:https://edu.cnblogs.co ...