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

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

最新文章

  1. 《Neural Network and Deep Learning》_chapter4
  2. [蓝牙] 6、基于nRF51822的蓝牙心率计工程消息流Log分析(详细)
  3. AC日记——字符串位移包含问题 1.7 19
  4. vim的Tab设置为4个空格
  5. HDU 2096 小明A+B --- 水题
  6. css经验点滴积累
  7. JS焦点图 上下翻动 支持IE6
  8. 线关节(Line Joint)
  9. 杜教筛 && bzoj3944 Sum
  10. session知识总结
  11. CI_SMOKE配置手册
  12. golang中的defer
  13. 使用SQL语句操作数据
  14. EOSIO/appbase
  15. SQL Server 经典案例
  16. WebSocket的原理与优缺点
  17. CSS Transform Style
  18. Django admin 常用方法 model 增加只读权限
  19. Spring Boot Gradle 打包可执行Jar文件!
  20. Hbase shell 常用命令(1)

热门文章

  1. Python的面向对象常见概念解析
  2. MySQL8.0.20安装详解
  3. SpringBoot集成MyBatis-Plus代码生成器(Dao)
  4. iNeuOS工业互联网操作系统,增加设备驱动的自定义参数模板,适配行业个性化设备的应用场景
  5. 利用pyinstaller给工程打包生成python可执行文件
  6. antd递归渲染左侧菜单
  7. SSR远程密码修改
  8. 使用.NET 6开发TodoList应用(23)——实现请求限流
  9. Metasploit生成木马入侵安卓手机
  10. 《剑指offer》面试题38. 字符串的排列