【问题标题】:add access control allow origin in nginx discourse.conf在 nginx discourse.conf 中添加访问控制允许来源
【发布时间】:2014-02-24 09:41:29
【问题描述】:

所以我在forums.awake-gaming.com 上有一个应用程序,并在awake-gaming.com 上有一个页面,其中POST 通过ajax 向forums.awake-gaming.com 发送表单。

正如预期的那样,我得到了一个

XMLHttpRequest cannot load http://forums.awake-gaming.com/posts. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://awake-gaming.com' is therefore not allowed access.

因此,由于我可以访问服务器,因此我将其添加到 nginx/conf.d 目录下的 discourse.conf 文件中。

add_header Access-Control-Allow-Origin  "http://awake-gaming.com/join-us.html";
add_header Access-Control-Allow-Methods: "GET, PUT, POST, DELETE, OPTIONS";
add_header Access-Control-Allow-Headers: "Content-Type, Authorization, X-Requested-With";

我重新启动了 nginx 并尝试再次提交表单,理论上,这应该可以工作,但我仍然在控制台中遇到同样的错误:

而且 ajax 调用也没有返回成功。但很奇怪,数据被发布到forums.awake-gaming.com 并被发布(通过话语 api)。

那我是不是错了?,我试图在 meta.discourse.org 上提问,但他们说这是 nginx 的问题,而不是话语的问题。在.conf 文件中添加标题是否足够?还是我也需要用 Rails 做点什么?

【问题讨论】:

    标签: ajax nginx cors discourse


    【解决方案1】:

    我认为您只需为 Access-Control-Allow-Origin 值指定“http://awake-gaming.com”,而不是“http://awake-gaming.com/join-us.html”,如错误消息所述。实际上,如果您使用的是 Chrome,它会告诉类似“'Access-Control-Allow-Origin' 白名单仅 'http://awake-gaming.com/join-us.html'。来源 'http://awake-gaming.com' 不在列表中,因此不允许访问。”

    顺便说一句,Access-Control-Allow-MethodsAccess-Control-Allow-Headers 之后有多余的冒号,你最好删除它们,但我不认为它们是这里的罪魁祸首。

    【讨论】:

      猜你喜欢
      • 2013-10-11
      • 1970-01-01
      • 2016-04-04
      • 1970-01-01
      • 1970-01-01
      • 2012-09-05
      • 1970-01-01
      • 2016-01-02
      • 2017-12-15
      相关资源
      最近更新 更多