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;
# 服務(wù)器根目錄
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;
}
}
}其中的“允許瀏覽器跨域請求”是關(guān)鍵點,因為瀏覽器在發(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);
}
}到此這篇關(guān)于Nginx+ThinkPHP+Vue解決跨域問題的方法詳解的文章就介紹到這了,更多相關(guān)Nginx ThinkPHP解決跨域內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
php array_walk() 數(shù)組函數(shù)
函數(shù)array_walk():單一數(shù)組回調(diào)函數(shù)---對數(shù)組中的每個成員應(yīng)用用戶函數(shù)2011-07-07
PHP調(diào)用Linux的命令行執(zhí)行文件壓縮命令
一開始,我和普通青年一樣,想到用PHP內(nèi)置的 ZipArchive糾結(jié)的是環(huán)境上沒安裝zip擴展,想采用用PHP調(diào)用Linux的命令行 ,執(zhí)行壓縮命令,感興趣的朋友可以了解下,希望本文對你有所幫助2013-01-01
php+jQuery遞歸調(diào)用POST循環(huán)請求示例
這篇文章主要介紹了php+jQuery遞歸調(diào)用POST循環(huán)請求,結(jié)合實例形式分析了php+jQuery的ajax方法遞歸調(diào)用與json轉(zhuǎn)換技巧,需要的朋友可以參考下2016-10-10

