最近有同事提出在使用客户端跨域访问的时候,发现服务器对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. 01 Java基础

    Java基础 01 Hello 随便新建一个文件夹,存放代码 新建一个Java文件 文件后缀名为.java Hello.java [注意点]系统可能没有显示文件后缀名,我们需要手动打开 编写代码  p ...

  2. 初识python 之 自动拆分转换文本内容

    上一篇升级版,转换文件内容. #!/user/bin env python # author:Simple-Sir # time:2021/7/9 23:32 def txt_2_list(filen ...

  3. sqlserver - 某字段数据为json串, 获取该json串里的值 的详细方法

    1.前言 某字段的数据为json 但是我想只获取里面的某一个值,该怎么操作? 2.笔记 (1)用 JSON_VALUE(参数1,参数2)函数 ,有两个参数, (2)参数1 为 列名 ,参数2 为 js ...

  4. JAVA并发-AQS知识笔记

    概述 AQS是AbstractQueuedSynchronizer的缩写,翻译成中文就是抽象队列同步器,AbstractQueuedSynchronizer这个类也是在java.util.concur ...

  5. vscode设置vue结构的初始代码片段

    { "Print to console": { "prefix": "vue", "body": [ "< ...

  6. day7 对字母数字的编排

    1.函数fun()的功能:在s数组内寻找并且统计t数组在s数组中出现的次数 输入字符串:函数scanf()函数不好用,会出现很多bug,因此不使用scanf() 效果理想: 2.函数fun()功能:将 ...

  7. 《剑指offer》面试题55 - I. 二叉树的深度

    问题描述 输入一棵二叉树的根节点,求该树的深度.从根节点到叶节点依次经过的节点(含根.叶节点)形成树的一条路径,最长路径的长度为树的深度. 例如: 给定二叉树 [3,9,20,null,null,15 ...

  8. git文件管理与索引,深入理解工作原理

    前言 这一夜,注定是个不眠之夜,小白和cangls的对话已然进入了白热化.小白孜孜不倦的咨询关于git方面的知识,对索引越来越感兴趣.小白以前存的小电影文件可以进行版本的对比,探索哪个版本画质更好. ...

  9. 【刷题-LeetCode】147 Insertion Sort List

    Insertion Sort List Sort a linked list using insertion sort. A graphical example of insertion sort. ...

  10. 0,NULL和nullpter

    #include <iostream> using namespace std; void f(int) { cout<<"f(int)"<<e ...