首页 > 解决方案 > nginx、aws 和 cloudflare 的 cors 问题

问题描述

我的网站托管在亚马逊 AWS 中。我的应用程序前端是用 angular 构建的,后端是用 nodejs express 框架构建的。该域使用 Cloudflare 和来自 Cloudflare 的免费 SSL 证书进行管理。服务器是使用 nginx 配置的。

一切都很好,但突然我的服务器返回 403 响应。结果,我的 API 停止工作。

我的 ngnixn 服务器代码:

server {
    index index.php index.html index.htm index.nginx-debian.html;

        server_name api.shajao.com;
    client_max_body_size 500M;
        underscores_in_headers on;
        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
        proxy_pass http://shajao_server;

    proxy_pass_request_headers on;

    client_body_buffer_size    500M;
    client_max_body_size 500M;

        proxy_http_version 1.1;
    proxy_set_header Access-Control-Allow-Origin *;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;

        add_header 'Access-Control-Allow-Origin' '*';
        }

}

我的快递服务器 cors 设置:

var allowCrossDomain = function (req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With, Accept');

    // intercept OPTIONS method
    if ('OPTIONS' === req.method) {
        res.sendStatus(200);
    } else {
        next();
    }
};
app.use(allowCrossDomain);

我的网址:www.shajao.com

标签: node.jsexpressnginxcorscloudflare

解决方案


cloudflare 发生了一些事情,导致了 cors 问题。从 cloudflare 中删除了我的域并从 godaddy 管理了我的 dns,一切都开始工作而没有任何更改。不确定 cloudflare 方面发生了什么。如果有人能回答就更好了


推荐阅读