xinxuaw231 发表于 2018-11-8 12:19:07

lumen+nginx+405 not allow +跨域

  使用lumen做的后台API,和前端调试时前端在调用需要jwt认证的接口时出现405not allow 错误。
  找了一堆发现可能是跨域问题。
  http://to-u.xyz/2016/06/30/nginx-cors/ 讲的不错。主要就是复杂请求时会先发送一次OPTIONS预请求然后再正式请求。。。。。
  当时找了一天没找明白是为什么。最后才定位到NGINX服务器上。
  特此记录!
  在nginx.conf server块加上这段过滤就行。
  

if ($request_method = 'OPTIONS') {  add_header Access-Control-Allow-Origin *;
  add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS;
  #其他头部信息配置,省略...
  return 204;
  
}
  

  参考:
  http://www.ruanyifeng.com/blog/2016/04/cors.html
  http://to-u.xyz/2016/06/30/nginx-cors/
  Nginx如何进行跨域配置,才能使用DELETE,PUT请求方法
  请添加链接描述
  Cross-Origin Resource Sharing (CORS)


页: [1]
查看完整版本: lumen+nginx+405 not allow +跨域