nginx如何配置跨域请求?

提问者:帅平 问题分类:运维
nginx如何配置跨域请求?

 您阅读本篇文章共花了: 

2 个回答
逾
给一个示例:
server {
    listen       80;
    server_name  your_domain.com;
    location /api {
        # 允许跨域请求的域名,* 表示允许所有域名访问
        add_header 'Access-Control-Allow-Origin' '*';
        # 允许跨域请求的方法
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        # 允许跨域请求的自定义 Header
        add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept';
        # 允许跨域请求的 Credential
        add_header 'Access-Control-Allow-Credentials' 'true';
        # 预检请求的存活时间,即 Options 请求的响应缓存时间
        add_header 'Access-Control-Max-Age' 3600;
        # 处理预检请求
        if ($request_method = 'OPTIONS') {
            return 204;
        }
    }
    # 其他配置...
}
发布于:3个月前 (01-29) IP属地:未知
有个笨蛋住进我心
有个笨蛋住进我心
nginx配置跨域,只需要在nginx的conf里面的location里面配置跨域参数即可,跨域参数是:
add_header 'Access-Control-Allow-Origin' *;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' *;
add_header 'Access-Control-Allow-Headers' 'Origin,X-Requested-With,Content-Type,Accept,token';

具体配置如下图:
发布于:1年前 (2022-11-27) IP属地:四川省
我来回答