Nginx+ThinkPHP+Vue解決跨域問(wèn)題的方法詳解
解決過(guò)程主要有兩個(gè)步驟。
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; # 服務(wù)器根目錄 root H:\php\project\UserManager\public; # 默認(rèn)讀取的文件 index index.php index.html index.htm; location / { # 允許瀏覽器跨域請(qǐng)求 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)聽(tīng)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; } } }
其中的“允許瀏覽器跨域請(qǐng)求”是關(guān)鍵點(diǎn),因?yàn)闉g覽器在發(fā)現(xiàn)網(wǎng)頁(yè)請(qǐng)求是跨域請(qǐng)求時(shí),會(huì)再發(fā)送一個(gè)OPTIONS請(qǐng)求,只有這個(gè)請(qǐng)求成功了才會(huì)允許跨域請(qǐng)求,此時(shí),要強(qiáng)行配置允許跨域。(這里配置的是允許全部請(qǐng)求跨域)
2.在ThinkPHP中允許跨域
編輯middleware.php文件
<?php // 全局中間件定義文件 return [ //允許跨域 //\think\middleware\AllowCrossDomain::class \app\middleware\AllowCrossDomain::class // 全局請(qǐng)求緩存 // \think\middleware\CheckRequestCache::class, // 多語(yǔ)言加載 // \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; /** * 跨域請(qǐng)求支持 */ 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', ''); } /** * 允許跨域請(qǐng)求 * @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); } }
到此這篇關(guān)于Nginx+ThinkPHP+Vue解決跨域問(wèn)題的方法詳解的文章就介紹到這了,更多相關(guān)Nginx ThinkPHP解決跨域內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Thinkphp中import的幾個(gè)用法詳細(xì)介紹
這篇文章主要介紹了Thinkphp中import的幾個(gè)用法,需要的朋友可以參考下2014-07-07php array_walk() 數(shù)組函數(shù)
函數(shù)array_walk():單一數(shù)組回調(diào)函數(shù)---對(duì)數(shù)組中的每個(gè)成員應(yīng)用用戶函數(shù)2011-07-07PHP程序守護(hù)進(jìn)程化實(shí)現(xiàn)方法詳解
這篇文章主要介紹了PHP程序守護(hù)進(jìn)程化實(shí)現(xiàn)方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07PHP調(diào)用Linux的命令行執(zhí)行文件壓縮命令
一開(kāi)始,我和普通青年一樣,想到用PHP內(nèi)置的 ZipArchive糾結(jié)的是環(huán)境上沒(méi)安裝zip擴(kuò)展,想采用用PHP調(diào)用Linux的命令行 ,執(zhí)行壓縮命令,感興趣的朋友可以了解下,希望本文對(duì)你有所幫助2013-01-01php+jQuery遞歸調(diào)用POST循環(huán)請(qǐng)求示例
這篇文章主要介紹了php+jQuery遞歸調(diào)用POST循環(huán)請(qǐng)求,結(jié)合實(shí)例形式分析了php+jQuery的ajax方法遞歸調(diào)用與json轉(zhuǎn)換技巧,需要的朋友可以參考下2016-10-10介紹幾個(gè)array庫(kù)的新函數(shù) php
介紹幾個(gè)array庫(kù)的新函數(shù) php...2006-12-12PHP.ini中配置屏蔽錯(cuò)誤信息顯示和保存錯(cuò)誤日志的例子
這篇文章主要介紹了PHP.ini中配置屏蔽錯(cuò)誤信息顯示和保存錯(cuò)誤日志的例子,需要的朋友可以參考下2014-05-05