解决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;
		}

		# 监听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;
		}
	}

}

其中的“允许浏览器跨域请求”是关键点,因为浏览器在发现网页请求是跨域请求时,会再发送一个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);
    }
}

 结束。

相关推荐

  1. 问题解决

    2024-03-30 17:44:04       42 阅读
  2. 解决问题

    2024-03-30 17:44:04       21 阅读
  3. 解决问题

    2024-03-30 17:44:04       7 阅读
  4. Vue中问题解决

    2024-03-30 17:44:04       32 阅读
  5. 问题+解决express

    2024-03-30 17:44:04       12 阅读
  6. 如何解决问题

    2024-03-30 17:44:04       49 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-30 17:44:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-30 17:44:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-30 17:44:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-30 17:44:04       20 阅读

热门阅读

  1. 算法——运动模型

    2024-03-30 17:44:04       21 阅读
  2. (67)动态口令 (68)解码异或后的数组

    2024-03-30 17:44:04       18 阅读
  3. 详解索引及优化

    2024-03-30 17:44:04       18 阅读
  4. SublimeText3多次保存自动弹出窗口

    2024-03-30 17:44:04       18 阅读
  5. 【Go】Context

    2024-03-30 17:44:04       16 阅读
  6. IO流主要有哪些?

    2024-03-30 17:44:04       18 阅读
  7. 实现文件下载

    2024-03-30 17:44:04       18 阅读
  8. Nginx专栏分享

    2024-03-30 17:44:04       20 阅读
  9. DNS 域名解析流程

    2024-03-30 17:44:04       21 阅读
  10. vue3路由跳转

    2024-03-30 17:44:04       15 阅读
  11. C语言共用体和枚举

    2024-03-30 17:44:04       19 阅读
  12. C#-非托管代码

    2024-03-30 17:44:04       21 阅读
  13. sql sqlserver常用日期函数

    2024-03-30 17:44:04       21 阅读
  14. 多进程和多线程

    2024-03-30 17:44:04       17 阅读
  15. 一些常见的与 Vim 相关的文件类型及其描述

    2024-03-30 17:44:04       17 阅读