【发布时间】:2019-02-03 22:19:18
【问题描述】:
我的网站托管在亚马逊 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
【问题讨论】:
-
你能在这里附上nodejs的错误代码吗?
-
我没有看到任何错误...我添加了我的网站 url.. 你可以在控制台中看到 cors 问题,在网络中你会看到我的 api 返回 403。
标签: node.js express nginx cors cloudflare