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)文章
PHP pthreads v3下的Volatile簡(jiǎn)介與使用方法示例
這篇文章主要介紹了PHP pthreads v3下的Volatile簡(jiǎn)介與使用方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了PHP pthreads v3下Volatile的功能、原理、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2020-02-02
PHP使用PHPexcel導(dǎo)入導(dǎo)出數(shù)據(jù)的方法
這篇文章主要介紹了PHP使用PHPexcel導(dǎo)入導(dǎo)出數(shù)據(jù)的方法,以實(shí)例形式較為詳細(xì)的分析了PHP使用PHPexcel實(shí)現(xiàn)數(shù)據(jù)的導(dǎo)入與導(dǎo)出操作相關(guān)技巧,需要的朋友可以參考下2015-11-11
談?wù)勑率秩绾螌W(xué)習(xí)PHP網(wǎng)絡(luò)編程
最近用到了php,雖然php的好的都不懂,也只是做一些簡(jiǎn)單的修改和書寫很少的代碼,但感覺(jué)php的功能真的很強(qiáng)2008-05-05
新安裝的MySQL數(shù)據(jù)庫(kù)需要注意的安全知識(shí)
在你自己安裝了一個(gè)新的MySQL服務(wù)器后,你需要為MySQL的root用戶指定一個(gè)目錄(缺省無(wú)口令),否則如果你忘記這點(diǎn),你將你的MySQL處于極不安全的狀態(tài)(至少在一段時(shí)間內(nèi))。2008-07-07
php實(shí)現(xiàn)的pdo公共類定義與用法示例
這篇文章主要介紹了php實(shí)現(xiàn)的pdo公共類定義與用法,結(jié)合具體實(shí)例形式分析了php實(shí)現(xiàn)的pdo操作類定義及查詢、插入等使用技巧,需要的朋友可以參考下2017-07-07
PHP中單引號(hào)和雙引號(hào)的區(qū)別詳解
看好多代碼有時(shí)候用單引號(hào)或雙引號(hào)實(shí)現(xiàn)包含字符串的內(nèi)容,其實(shí)簡(jiǎn)單個(gè)概括下雙引號(hào)中的變量可以解析,單引號(hào)就是絕對(duì)的字符串,下面這篇文章主要給大家介紹了關(guān)于PHP中單引號(hào)和雙引號(hào)區(qū)別的相關(guān)資料,需要的朋友可以參考下2023-01-01
php實(shí)現(xiàn)連接access數(shù)據(jù)庫(kù)并轉(zhuǎn)txt寫入的方法
這篇文章主要介紹了php實(shí)現(xiàn)連接access數(shù)據(jù)庫(kù)并轉(zhuǎn)txt寫入的方法,涉及php連接、讀取access數(shù)據(jù)庫(kù)及寫入txt文件的相關(guān)操作技巧,需要的朋友可以參考下2017-02-02

