Nginx 设置头部允许所有跨域请求

server {
    listen       80;
    server_name  charm.xljsci.com  dev.xljsci.com dev.editor.com;
    set $root "/var/www/html/wwwroot/editor/public";
    root   $root;

    add_header Access-Control-Allow-Methods *;
    add_header Access-Control-Max-Age 3600;
    add_header Access-Control-Allow-Credentials false;
    add_header Access-Control-Allow-Origin $http_origin;
    add_header Access-Control-Allow-Headers 
    $http_access_control_request_headers;
    if ($request_method = OPTIONS){
        return 200;
    }

    location / {
        index  index.php index.html index.htm;
        try_files $uri $uri/ /index.php$is_args$args;
        if (!-e $request_filename){  
            rewrite ^/(.*)$ /index.php?s=$1 last;  
        }
    }

    ##外部1
    location  ^~ /zzapi/ {
        proxy_pass http://zz.xljsci.com/;
    }

    ##外部2
    location  ^~ /zzapi2/ {
        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
        add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
        proxy_pass http://116.62.243.33/;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host "zz.xljsci.com";
        proxy_ignore_client_abort on;
        proxy_read_timeout 120s;
    }

    ##本地
    location  ^~ /adminapi/ {
        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
        add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
        proxy_pass http://127.0.0.1/;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host "zzcharm.xljsci.com";
        proxy_ignore_client_abort on;
        proxy_read_timeout 120s;
    }

    location ~ \.php(.*)$ {
        fastcgi_pass   php71:9000;
        fastcgi_index  index.php;
        fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  PATH_INFO  $fastcgi_path_info;
        fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
        include        fastcgi_params;
    }

}

你可能感兴趣的:(Nginx 设置头部允许所有跨域请求)