通过nginx配置解决前端跨域CORS问题

nginx 配置

              location ~ ^/dingdang-cms/.*$
                {
                    #add_header 'Access-Control-Allow-Origin' 'http://192.168.2.7:6970' always;
                    #如果使用此配置则只能为http://192.168.2.7:6970的域名可以访问
                    add_header 'Access-Control-Allow-Origin' "$http_origin";
                    add_header 'Access-Control-Allow-Credentials' 'true' always ;
                    if ($request_method = 'OPTIONS') {
                       add_header Access-Control-Max-Age "3600" ;
                       add_header 'Access-Control-Allow-Origin' 'http://192.168.2.7:6970' always;
                       #add_header 'Access-Control-Allow-Origin' "$http_origin";
                       add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,  Access-Control-Expose-Headers, Token, Authorization';
                       return 200;
                    }
                proxy_pass http://dingdang-cms;
                }

你可能感兴趣的:(通过nginx配置解决前端跨域CORS问题)