phpstudy-nginx下解决json文件跨域问题

        cors跨域一直是个很头疼的问题,php文件的跨域跨域通过配置请求头的方式去解决。文件的话跨域通过配置nginx来解决。

        

 

在上面的选择你的phpstudy项目中对应的配置文件,用编辑器打开。在文档中配置

 location ~ \.json {
        client_body_timeout 6s;
        	if ($request_method = 'OPTIONS') {
            	add_header 'Access-Control-Allow-Origin' '*' always;
            	add_header 'Access-Control-Allow-Credentials' 'true';
            	add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, DELETE, PUT, 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,  Access-Control-Expose-Headers, Token, Authorization';
            	add_header 'Access-Control-Max-Age' 1728000;
            	add_header 'Content-Type' 'text/plain charset=UTF-8';
            	add_header 'Content-Length' 0;
            	return 204;
        	}
        	add_header 'Access-Control-Allow-Origin' '*' always;
    	}

在前面的.json是需要配置的文件后缀名,下面则是配置允许的请求头。

整个文件如下

 

server {
        listen        8102;
        server_name  localhost;
        root   "E:/Me/Dw/code/server/php-platform/public";
        location / {
            index index.php index.html error/index.html;
            error_page 400 /error/400.html;
            error_page 403 /error/403.html;
            error_page 404 /error/404.html;
            error_page 500 /error/500.html;
            error_page 501 /error/501.html;
            error_page 502 /error/502.html;
            error_page 503 /error/503.html;
            error_page 504 /error/504.html;
            error_page 505 /error/505.html;
            error_page 506 /error/506.html;
            error_page 507 /error/507.html;
            error_page 509 /error/509.html;
            error_page 510 /error/510.html;
            # include E:/Me/Dw/code/server/php-platform/public/nginx.htaccess;
            autoindex  off;
        }

        location ~ \.json {
        client_body_timeout 6s;
        	if ($request_method = 'OPTIONS') {
            	add_header 'Access-Control-Allow-Origin' '*' always;
            	add_header 'Access-Control-Allow-Credentials' 'true';
            	add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, DELETE, PUT, 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,  Access-Control-Expose-Headers, Token, Authorization';
            	add_header 'Access-Control-Max-Age' 1728000;
            	add_header 'Content-Type' 'text/plain charset=UTF-8';
            	add_header 'Content-Length' 0;
            	return 204;
        	}
        	add_header 'Access-Control-Allow-Origin' '*' always;
    	}
        
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9004;
            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;
        }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值