欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Nginx+ThinkPHP+Vue解決跨域問題的方法詳解

 更新時間:2024年04月02日 14:27:05   作者:皮皮高  
這篇文章主要為大家詳細介紹了Nginx+ThinkPHP+Vue解決跨域問題的方法,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以參考下

解決過程主要有兩個步驟。

1.nginx配置允許跨域

worker_processes  1;
 
events {
    worker_connections  1024;
}
 
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 204;
            }
            
 
			if (!-e $request_filename) {
				rewrite ^(.*)$ /index.php?s=/$1 last; break; 
			} 
			try_files $uri $uri/ /index.php?$query_string;
		}
 
		# 監(jiān)聽127.0.0.1:9000端口,要和php-cgi.exe配置的ip:端口一致
		location ~ \.php$ {
			fastcgi_pass 127.0.0.1:9000;
			include fastcgi_params;
			fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		}
	}
 
}

其中的“允許瀏覽器跨域請求”是關鍵點,因為瀏覽器在發(fā)現(xiàn)網(wǎng)頁請求是跨域請求時,會再發(fā)送一個OPTIONS請求,只有這個請求成功了才會允許跨域請求,此時,要強行配置允許跨域。(這里配置的是允許全部請求跨域)

2.在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解決跨域內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論