AJAX从一个域请求另一个域会有跨域的问题。那么如何在nginx上实现ajax跨域请求呢?要在nginx上启用跨域请求,需要添加add_header Access-Control*指令。如下所示:
[color=blue] location / {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'POST,GET,OPTIONS,DELETE,PUT';
proxy_pass http://fosCluster;
proxy_redirect off;
}[/color]
注释如下:
第一条指令:授权所有域的请求
第二条指令:指定请求的方法,可以是GET,POST等
重启nginx
/usr/local/nginx/sbin/nginx -s reload
[color=blue] location / {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'POST,GET,OPTIONS,DELETE,PUT';
proxy_pass http://fosCluster;
proxy_redirect off;
}[/color]
注释如下:
第一条指令:授权所有域的请求
第二条指令:指定请求的方法,可以是GET,POST等
重启nginx
/usr/local/nginx/sbin/nginx -s reload