前端如何通过Nginx代理做到跨域访问API接

2022-10-19  本文已影响0人  凡凡的小web

location / {
if ($request_method ~* "(GET|POST)") {
add_header "Access-Control-Allow-Origin" *;
}

        # Preflighted requests
        if ($request_method = OPTIONS ) {
            add_header "Access-Control-Allow-Origin" *;
            add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS, HEAD";
            add_header "Access-Control-Allow-Headers" "Authorization, Origin, X-Requested-With, Content-Type, Accept";
            return 200;
        }
        //本地所指向的地址
        proxy_pass http://127.0.0.1:7457;
        proxy_redirect default;
    }


    location /api{
        rewrite ^/api/(.*)$ /$1 break;
        //我们要访问的接口地址
        proxy_pass https://dev-api-fillcolor.hudong.city/;
    }

//https://www.cnblogs.com/bruce1992/p/15591105.html

上一篇 下一篇

猜你喜欢

热点阅读