Node.js nginx、aws和cloudflare的cors问题
我有我的网站托管在亚马逊aws。我的应用程序前端使用angular构建,后端使用nodejs express框架构建。该域使用Cloudflare管理,Cloudflare提供免费SSL证书。服务器是使用nginx配置的 一切正常,但突然我的服务器返回403响应。结果,我的API停止工作 我的ngnixn服务器代码:Node.js nginx、aws和cloudflare的cors问题,node.js,express,nginx,cors,cloudflare,Node.js,Express,Nginx,Cors,Cloudflare,我有我的网站托管在亚马逊aws。我的应用程序前端使用angular构建,后端使用nodejs express框架构建。该域使用Cloudflare管理,Cloudflare提供免费SSL证书。服务器是使用nginx配置的 一切正常,但突然我的服务器返回403响应。结果,我的API停止工作 我的ngnixn服务器代码: server { index index.php index.html index.htm index.nginx-debian.html; server
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' '*';
}
}
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);
我的express服务器cors设置:
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' '*';
}
}
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);
我的网站url:cloudflare出现了一些问题,导致cors问题。从cloudflare中删除了我的域,从godaddy中管理了我的dns,一切都开始工作,没有任何更改。不知道cloudflare那边发生了什么。如果有人能回答这个问题会更好你能在这里附加nodejs错误代码吗?我没有看到任何错误。。。我添加了我的网站url。。您可以在控制台中看到cors问题,在网络中您将看到我的api返回403。