nginx允许所有二级域名跨域请求

w3c标准中,跨域请求可以添加http header:Access-Control-Allow-Origin,但在w3c中有如下限制:


nginx允许所有二级域名跨域请求_第1张图片

Access-Control-Allow-Origin头只允许域名列表和*这两种值,如果要允许所有的二级域名,可以使用下面的变通方法:


                location ~ .*\.(php|php5)?$
                        {

                                fastcgi_pass  unix:/tmp/php-cgi.sock;
                                #fastcgi_pass  127.0.0.1:9000;
                                fastcgi_index index.php;

                                include fastcgi_params;
                                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                                #fastcgi_param PATH_INFO $fastcgi_script_name;

                                 if ($http_origin ~* (http?://.*\.test\.cn$)) {

                                     add_header Access-Control-Allow-Origin $http_origin;
                                     add_header 'Access-Control-Allow-Credentials' 'true';
                                     add_header 'Access-Control-Allow-Methods' 'GET,POST';
                                }

                        }



【作者】曾健生
【QQ】190678908
【app后端qq群】254659220 
【微信公众号】 appbackend
【新浪微博】 @newjueqi
【博客】http://blog.csdn.net/newjueqi 

你可能感兴趣的:(linux)