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;

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容