1 Star 5 Fork 2

姚文强/learn-nginx

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
cors.md 1.08 KB
一键复制 编辑 原始数据 按行查看 历史
xuexb 提交于 8年前 . 添加CORS跨域

配置 nginx CORS 跨域

设置允许所有的请求

server {
    location / {
        add_header 'Access-Control-Allow-Origin' '*';
    }
}

只允许GET请求

server {
    location / {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Request-Method' 'GET';
    }
}

请求白名单

server {
    location / {
        # 白名单
        if ($http_origin ~* (baidu\.com|github.xuexb.com)$) {
            add_header 'Access-Control-Allow-Origin' '$http_origin';

            # 允许cookie
            add_header 'Access-Control-Allow-Credentials' true;

            # 只允许某些方法
            add_header 'Access-Control-Request-Method' 'GET, POST, OPTIONS';

            # 支持获取其她字段, 需要前端设置 `xhr.withCredentials = true`
            add_header 'Access-Control-Allow-Headers' 'User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        }
    }
}

link

Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yaowenqiang/learn-nginx.git
git@gitee.com:yaowenqiang/learn-nginx.git
yaowenqiang
learn-nginx
learn-nginx
master

搜索帮助