当项目访问走Nginx,可以在Nginx的配置文件nginx.conf中配置白名单,允许指定域发送ajax请求,获得项目资源。
如下配置:
location / {
if ($http_origin ~* (http?://192.168.2.155:8083$)) {
add_header 'Access-Control-Allow-Origin' '*';
}
}
配置讲解:
add_header 'Access-Control-Allow-Origin' 'http://192.168.2.155:8083';指定url访问;
#add_header 'Access-Control-Allow-Origin' '*';允许任何域访问;
真实环境下根据实际情况编写正则表达式替换http?://192.168.2.155:8083$表达式。
#跨域访问设置
location / {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Credentials true;
add_header Access-Control-Allow-Methods GET;
root /yyy/;
index kiss.html index.html index.htm;
}