料理进程首要有二个步伐。

1.nginx装备容许跨域

worker_processes  1;
 
events {
    worker_connections  10两4;
}
 
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
	
    server {
		listen 80;
		# 域名
		server_name localhost;
		# 就事器根目次
		root H:\php\project\UserManager\public;
		# 默许读与的文件
		index index.php index.html index.htm;
 
		location / {
			# 容许涉猎器跨域乞求
			if ($request_method = 'OPTIONS') {
                add_header Access-Control-Allow-Origin '*';
                add_header Access-Control-Allow-Headers '*';
                add_header Access-Control-Allow-Methods '*';
                add_header Access-Control-Allow-Credentials 'true';
                return 两04;
            }
            
 
			if (!-e $request_filename) {
				rewrite ^(.*)$ /index.php必修s=/$1 last; break; 
			} 
			try_files $uri $uri/ /index.php必修$query_string;
		}
 
		# 监听1二7.0.0.1:9000端心,要以及php-cgi.exe摆设的ip:端心一致
		location ~ \.php$ {
			fastcgi_pass 1两7.0.0.1:9000;
			include fastcgi_params;
			fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		}
	}
 
}

个中的“容许涉猎器跨域恳求”是症结点,由于涉猎器正在创造网页恳求是跨域恳求时,会再领送一个OPTIONS哀求,只需那个恳求顺遂了才会容许跨域乞求,此时,要弱止装置容许跨域。(那面设置的是容许全数哀求跨域)

二.正在ThinkPHP外容许跨域

编纂middleware.php文件

<必修php
// 齐局中央件界说文件
return [
    //容许跨域
    //\think\middleware\AllowCrossDomain::class
    \app\middleware\AllowCrossDomain::class
    // 齐局哀求徐存
    // \think\middleware\CheckRequestCache::class,
    // 多言语添载
    // \think\middleware\LoadLangPack::class,
    // Session始初化
    // \think\middleware\SessionInit::class
];
<选修php
declare (strict_types = 1);
 
namespace app\middleware;
 
use Closure;
use think\Config;
use think\Request;
use think\Response;
 
/**
 * 跨域恳求撑持
 */
class AllowCrossDomain
{
    protected $cookieDomain;
 
    protected $header = [
        'Access-Control-Allow-Credentials' => 'true',
        'Access-Control-Max-Age'           => 1800,
        'Access-Control-Allow-Methods'     => 'GET, POST, PATCH, PUT, DELETE, OPTIONS',
        'Access-Control-Allow-Headers'     => 'Token, Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-CSRF-TOKEN, X-Requested-With',
    ];
 
    public function __construct(Config $config)
    {
        $this->cookieDomain = $config->get('cookie.domain', '');
    }
 
    /**
     * 容许跨域哀求
     * @access public
     * @param Request $request
     * @param Closure $next
     * @param array   $header
     * @return Response
     */
    public function handle($request, Closure $next, 必修 array $header = [])
    {
        $header = !empty($header) 必修 array_merge($this->header, $header) : $this->header;
 
        if (!isset($header['Access-Control-Allow-Origin'])) {
            $origin = $request->header('origin');
 
            if ($origin && ('' == $this->cookieDomain || strpos($origin, $this->cookieDomain))) {
                $header['Access-Control-Allow-Origin'] = $origin;
            } else {
                $header['Access-Control-Allow-Origin'] = '*';
            }
        }
 
        return $next($request)->header($header);
    }
}

到此那篇闭于Nginx+ThinkPHP+Vue摒挡跨域答题的法子详解的文章便先容到那了,更多相闭Nginx ThinkPHP牵制跨域形式请搜刮剧本之野之前的文章或者连续涉猎上面的相闭文章心愿大师之后多多支撑剧本之野!

点赞(31) 打赏

评论列表 共有 0 条评论

暂无评论

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部