Nginx实现Access-Control-Allow-Origin通配符

雪域幽狐 2021-08-09 09:46 阅读:4595


    在遇到CORS问题时,需要配置Access-Control-Allow-Origin,值要么设置为*,要么为一个具体的域名。
    处于安全考虑,一般不配置为*。因此需要设置具体域名,但是如果有多个域名(常见为多个子域名)想请求资源时,就遇到问题。
    对于Nginx而言,可以使用对应的语法来实现,如下:
        location / {
            root   /data/static;
            index  index.html index.htm;
            if ( $http_origin ~ .*.nowfox.com ) {
                add_header 'Access-Control-Allow-Origin' $http_origin;
                add_header 'Access-Control-Allow-Methods' 'GET';
            }
        }

0条评论

登陆后可评论