【问题标题】:Configure ngrok's CORS headers配置 ngrok 的 CORS 标头
【发布时间】:2017-03-12 16:18:55
【问题描述】:

我正在运行一个本地网络服务器,它向 ngrok 服务器运行 XHR 请求,也从我的 PC 运行。

我收到XMLHttpRequest cannot load http://foo.ngrok.io/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access。

ngrok FAQ 似乎提到了 CORS 标头,但仅与基本身份验证有关 - 它没有提及如何设置标头以便我可以在开发中测试我的应用程序。

如何更改 ngrok 的 CORS 选项以允许从 localhost 加载请求?

更新:不同的用例。此解决方案的赏金:

我收到以下错误:

login.php:1 Access to XMLHttpRequest at 'http://localhost/lagin/public/login.php'
from  origin 'http://062b-96-230-240-153.ngrok.io' has been blocked by CORS 
policy: The request client is not a secure context and the resource is in more- 
private address space `local`. 

我查看了配置 ngrok 的 CORS 标头,但仍不确定如何继续。当我尝试 ngrok http -host-header=rewrite 80 时,它说标头未定义。

我看过 10 或 12 个 youtube 视频,它们都很好地解释了 CORS 是什么,但解释如何修复它却很糟糕。

我在 Windows 10 机器上运行 virtualbox 并创建一个 linux 虚拟机。在 linux 端,我将 xampp 作为本地服务器运行。

我很乐意提供更多详细信息,但我只是不知道需要哪些额外信息。

我可以在 ngrok 上看到我网站的登录页面,但是一旦我进行 axios 调用,我就会收到上述错误。

另外,我在 chrome 中尝试了 //flags/#block-insecure-private-network-requests 并设置为禁用。当我这样做时,我不再收到错误,但网站无法正常工作。

附加信息:

我与 ngrok 交谈,他们说:...听起来您的应用程序正试图在 ajax 请求中的某处调用 localhost。您需要调整该调用以确保它通过 ngrok 路由。

这就是我正在做的事情:

responseData = sendData2('http://localhost/lagin/public/login.php',emailPass);

这里是 sendData2(只是为了完整性)

function sendData2(url,emailPass){

    let bodyFormData = new FormData()
    for (const [key, value] of Object.entries(emailPass)) {
       //console.log(key,value)
       bodyFormData.append(key,value)
    }

    return axios({
            method: 'POST',
            url: url,
            data: bodyFormData,
            headers: {'Content-Type': 'multipart/form-data'}
            })
                .then(function(response){
                    return response.data
                })
                .catch(function(response){
                    return response
                })
}

更新:每次我们通过隧道进入 ngrok 时,我们都会得到一个类似 https://2634-96-230-240-153.ngrok.io 的地址,如果我们将 send2() 调用更改为

sendData2('http://96-230-240-153.ngrok.io/lagin/public/login.php',emailPass);

它可以工作,但这需要我在每次有新隧道时更改代码。调整 CORS 政策会解决这个问题吗?

【问题讨论】:

    标签: xampp cors ngrok


    【解决方案1】:

    我今天偶然发现了这个问题,并且能够通过启动 ngrok 并包括 -host-header 标志来解决它。

    ngrok http -host-header=rewrite 3000

    来自文档:

    使用 -host-header 开关重写传入的 HTTP 请求。

    如果指定了重写,Host 头将被重写以匹配 转发地址的主机名部分。

    【讨论】:

    • 谢谢。这对我有用。
    【解决方案2】:

    首先,ngrok 只是一个隧道而不是服务器,因此在 ngrok 中配置 CORS 标头是不可能的。所以任何类型的 CORS 配置都需要在服务器级别完成。

    例如,如果你使用的是 nginx 服务器,你需要在 nginx conf 文件中配置 header,如:

    location / {
        /*some default configuration here*/
        add_header 'Access-Control-Allow-Origin' '*';
    }
    

    通过添加此标头,您表示允许从任何地址跨源访问您的地址,因为标头的值为“*”。您还可以通过替换值来指定允许访问您的地址的特定地址。

    【讨论】:

      【解决方案3】:

      如果您将 ngrok 与 nodejs/express.js 一起使用。

      使用此代码:

      app.use(function(req, res, next) {
      res.header("Access-Control-Allow-Origin", "YOUR-DOMAIN.TLD"); // update to match 
      the domain you will make the request from
      res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content- 
      Type, Accept");
      next();
      });
      

      将“YOUR-DOMAIN.TLD”替换为“*”以允许访问所有网址或您的特定网站网址。

      更多详情请参考https://enable-cors.org/server_expressjs.html

      【讨论】:

      • 我在我的 js 中运行 axios,我的后端是 php。我该怎么办?
      【解决方案4】:

      让 ngrok 工作需要一些时间来弄清楚,但实际上很容易。

      In chrome there is an option to turn off CORS.  In the chrome address bar go to
      chrome://flags and look for Block insecure private network requests.
      This needs to be disabled.
      
      Second, in my ajax request I had used an absolute path and this needed to be changed 
      to a relative path.  
      

      记住:这是为了运行 localhost 并将其暴露给网络

      【讨论】:

        猜你喜欢
        • 2018-01-18
        • 2015-11-01
        • 2016-04-26
        • 2018-08-10
        • 2020-02-21
        • 2020-01-21
        • 2017-06-21
        相关资源
        最近更新 更多