var orginList = [
'http://127.0.0.1:4000', 'https://xindot.com', 'https://www.xindot.com'
]
app.all("*", function (req, res, next) {
if (orginList.includes(req.headers.origin)) {
//设置允许跨域的域名,*代表允许任意域名跨域
res.header("Access-Control-Allow-Origin", req.headers.origin);
}
//允许的header类型
res.header("Access-Control-Allow-Headers", "content-type");
//跨域允许的请求方式
res.header("Access-Control-Allow-Methods", "DELETE,PUT,POST,GET,OPTIONS");
next();
})
// 注意:放在路由设置之前
// app.use('/user', require('./routes/user'));
var orginList = [
'http://127.0.0.1:4000', 'https://xindot.com', 'https://www.xindot.com'
]
router.all("/*", function (req, res, next) {
if (orginList.includes(req.headers.origin)) {
//设置允许跨域的域名,*代表允许任意域名跨域
res.header("Access-Control-Allow-Origin", req.headers.origin);
}
//允许的header类型
res.header("Access-Control-Allow-Headers", "content-type");
//跨域允许的请求方式
res.header("Access-Control-Allow-Methods", "DELETE,PUT,POST,GET,OPTIONS");
next();
})
// 注意:放在路由设置之前
//router.get('/list', (req, res) => { res.send({code:200})} )
location / {
set $match "";
# 支持http及https
if ($http_origin ~* 'https?://(localhost|.*\.example\.com)') {
set $match "true";
}
if ($match = "true") {
add_header Access-Control-Allow-Origin "$http_origin";
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
add_header Access-Control-Allow-Methods GET,POST,OPTIONS,DELETE;
add_header Access-Control-Allow-Credentials true;
}
# 处理OPTIONS请求
if ($request_method = 'OPTIONS') {
return 204;
}
}
map $http_origin $corsHost {
default 0;
"~http://www.123admin.com" http://www.123admin.com;
"~http://m.123admin.com" http://m.123admin.com;
"~http://wap.123admin.com" http://wap.123admin.com;
}
server {
listen 80;
server_name search.123admin.com;
root /nginx;
location / {
add_header Access-Control-Allow-Origin $corsHost;
}
}