1. 程式人生 > >nginx 解決跨域問題

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等跳轉,感覺沒必要,就沒再查解決方法