Yii 跨域设置

控制器设置:

abstract class ControllerBase extends Controller
{
    public function __construct($id, $module, $config = [])
    {
        parent::__construct($id, $module, $config);
    }

    public function beforeAction($action)
    {
        header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
        header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
        header('Access-Control-Allow-Credentials: true');
        header("Access-Control-Allow-Headers: Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,Keep-Alive,X-Requested-With,If-Modified-Since");
        if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
            \Yii::$app->response->setStatusCode(204);
            \Yii::$app->end(0);
        }
        return parent::beforeAction($action);
    }
}

 

 

nginx设置:

server {
    listen   80;
    server_name    xxx-dev.myfuwu.cn;
    root /webser/www/xxx/web;
    index  index.php;
    access_log  "pipe:/usr/local/sbin/sbin/cronolog /webser/logs/tengine/cronolog/%Y/%m/%Y-%m-%d_access_xxx.log" wwwlogs;
    error_log   "pipe:/usr/local/sbin/sbin/cronolog /webser/logs/tengine/cronolog/%Y/%m/%Y-%m-%d_error_xxx.log" warn;
    
    location ~ /static {
        try_files $uri $uri/ /static/index.html =404;
    }
    
    location / {
        set $cors "true";
        
        if ($request_method = 'OPTIONS') {
            set $cors "${cors}options";  
        }
        
        add_header 'Access-Control-Allow-Origin' "$http_origin";
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,Keep-Alive,X-Requested-With,If-Modified-Since';
        

        # ifit's OPTIONS, then it's a CORS preflight request so respond immediately with no response body
        if ($cors = "trueoptions") {
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Length' 0;
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            return 204;
        }
        
        try_files $uri $uri/ /index.php;
        include /webser/www/xxx/web/nginx-rewrite.conf;
    }
    
    location ~ \.php  {
        include fastcgi_params;
        fastcgi_pass  127.0.0.1:9001;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

    #fastcgi_param  SCRIPT_FILENAME   $document_root$real_script_name;
        #    fastcgi_param  SCRIPT_NAME       $real_script_name;
        #    fastcgi_param  PATH_INFO         $path_info;
        #    fastcgi_param  TAG               $real_script_name;
    }

    location ~* ^.+\.(jpg|jpeg|gif|png|bmp|css|js|swf)$ {
        access_log off;
        #break;
    }

    location =/check.html
    {
            root /webser/www;
            access_log off;
    }

}

 

posted @ 2016-04-26 16:15  stone_wl  阅读(2777)  评论(0编辑  收藏  举报