NGINX 支持跨域请求配置


location ^~/api/v1 {
        add_header'Access-Control-Allow-Origin'"$http_origin";
        add_header'Access-Control-Allow-Methods''GET, POST, PUT, DELETE, OPTIONS';
        add_header'Access-Control-Allow-Headers''DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type    ';
        add_header'Access-Control-Allow-Credentials''true';
        if($request_method='OPTIONS') {
                add_header'Access-Control-Allow-Origin'"$http_origin";
                add_header'Access-Control-Allow-Methods''GET, POST, PUT, DELETE, OPTIONS';
                add_header'Access-Control-Allow-Headers''DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type    ';
                add_header'Access-Control-Allow-Credentials''true';
                add_header'Access-Control-Max-Age'1728000;# 20 天
                add_header'Content-Type''text/html charset=UTF-8';
                add_header'Content-Length'0;
                return200;
        }

# 这下面是要被代理的后端服务器,它们就不需要修改代码来支持跨域了

        proxy_passhttp://127.0.0.1:8085;

        proxy_set_headerHost$host;

        proxy_redirectoff;

        proxy_set_headerX-Real-IP$remote_addr;

        proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;

        proxy_connect_timeout60;

        proxy_read_timeout60;

        proxy_send_timeout60;

}

你可能感兴趣的:(NGINX 支持跨域请求配置)