nginx 解决跨域问题

跨域的解决方式是CORS  CORS介绍  
弄了好久,其实只要允许options请求,在head内加入标识允许字段即可。 但是老项目的原因,权限动不了,options会302跳登录界面 

就想到用nginx前置过滤,url-->nginx-->真正服务器

nginx处理: 

1.正常情况下,跳转到服务器 

2.遇到options请求,直接返回,并带上指定head

例如:真实服务在127.0.0.1:8080,域名为 platform-api-test.zzg.me

server {

        listen 80;
        server_name platform-api-test.1yd.me;
      
        location / {

                if ($request_method = OPTIONS ) {
                        add_header Access-Control-Allow-Origin "*";
                        add_header Access-Control-Allow-Methods "POST, GET, PUT, OPTIONS, DELETE";
                        add_header Access-Control-Max-Age "3600";
                        add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept, Authorization";

                        #add_header Access-Control-Allow-Credentials "true";
                        add_header Content-Length 0;
                        #add_header  Access-Control-Max-Age "3600";
                        add_header Content-Type text/plain;
                        return 200;
                 }

                proxy_pass http://webservers;
        }
}




问题:目前ajax跨区不允许302等跳转,感觉没必要,就没再查解决方法

你可能感兴趣的:(跨域,nginx,跨域)