Nginx解决跨域问题

114 阅读1分钟

跨域问题

当我们在同一个站点请求时,是不存在什么问题的,但是当我们从一个站点向另外一个站点访问的时候就会出现跨域问题

解决跨域问题(CORS跨域资源共享)

1.Cross-Origin Resource Sharing(跨域资源共享) 2.允许浏览器向跨Origin的服务器发起js请求获取响应 3.Jsonp、SpringBoot Cors、Nginx

Nginx的解决方案

在nginx.conf中配置文件中的server指令块下面配置以下内容,之后就可以解决跨域问题了

server {
    listen       80;
    server_name  localhost;

    # 允许跨域请求的域,*代表所有
    add_header 'Access-Control-Allow-Origin' *;
    # 允许带上cookie请求
    add_header 'Access-Control-Allow-Credentials' 'true';
    # 允许请求的方法,比如 GET/POST/PUT/DELETE
    add_header 'Access-Control-Allow-Method' *;
    # 允许请求的header
    add_header 'Access-Control-Allow-Headers' *;

    location / {
        root   html;
        index  index.html index.htm;
    }
}