webman-admin打包后出现跨域问题

lychuan

问题描述

webman-admin 打包之后出现跨域问题,服务端用的nginx代理,按照手册上设置的,Nginx也配置了跨域,前端请求的时候,还会提示跨域错误

程序代码

nginx配置

upstream webman {
    server 172.31.34.153:8787;
    keepalive 10240;
}
server {
  listen 8989;
  listen [::]:8989;
  server_name IP:8989;
  access_log off;
  index index.html index.htm index.php;
  root /data/wwwroot/IP:8787;

  include /usr/local/nginx/conf/rewrite/none.conf;
  #error_page 404 /404.html;
  #error_page 502 /502.html;

  location / {

      add_header 'Access-Control-Allow-Origin' '*';
      add_header 'Access-Control-Allow-Headers' '*';
      add_header 'Access-Control-Allow-Methods' '*';
      add_header 'Access-Control-Max-Age' 86400;

      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header Host $host;
      proxy_http_version 1.1;
      proxy_set_header Connection "";
      if (!-f $request_filename){
          proxy_pass http://webman;
      }
  }
}

webman跨域中间件

<?php
namespace app\middleware;

use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;

class AccessControl implements MiddlewareInterface
{
    public function process(Request $request, callable $handler) : Response
    {
        // 如果是opitons请求则返回一个空的响应,否则继续向洋葱芯穿越,并得到一个响应
        $response = $request->method() == 'OPTIONS' ? response('') : $handler($request);

        // 给响应添加跨域相关的http头
        $response->withHeaders([
            'Access-Control-Allow-Credentials' => 'true',
            'Access-Control-Allow-Origin' => $request->header('origin', '*'),
            'Access-Control-Allow-Methods' => $request->header('access-control-request-method', '*'),
            'Access-Control-Allow-Headers' => $request->header('access-control-request-headers', '*'),
        ]);

        return $response;
    }
}

报错信息

Access to XMLHttpRequest at 'http://ip:8989/api/sport/1?pageSize=20&page=1&type=1' from origin 'http://ip' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

操作系统及workerman/webman等框架组件具体版本

webman-admin 最新版

951 4 0
4个回答

lychuan

在本地的时候,通过vite的proxy代理,解决了跨域问题额,传到服务器又出现了跨域问题,有没有明白的大佬给指点一下。感谢

  • 暂无评论
lychuan

有没有大佬知道原因的额?

  • 暂无评论
six

1、nginx 改完配置要重启才能生效。
2、webman-admin是应用插件,手册说每个应用插件有自己独立的配置和主项目是独立的,所以给webman-admin插件设置跨域应该设置plugin/admin/config/middleware.php而不是主项目中的config/middleware.php
3、既然都发布到外网了,前端和后端域名端口应该保持一致,保持一致自然就不会跨域了,自然没这些麻烦。

  • lychuan 2022-11-24

    大佬,非常的神奇,webman-admin 设置了官网的跨域中间件,然后nginx又设置了跨域,这样会导致报错,为什么呢?

lychuan

webman-admin 设置了官网的跨域中间件,然后nginx又设置了跨域,这样会导致跨域不成功,有没有大佬明白是为什么???把webman-admin的跨域中间件取消,就正常了。。。

  • xiuwang 2022-11-24

    具体原因浏览器会提示,类似 the xxx header contains multiple values xxx, but only one is allowed。翻译过来就是某个跨域header只允许设置一个值,不允许重复设置。
    当然,你最开始的报错No 'Access-Control-Allow-Origin' header is present on the requested resource不是重复设置跨域头,是没设置跨域头。两个是不同的问题。

  • lychuan 2022-11-24

    真的太对了大佬,为什么不能重复设置呢?nginx算是前端的跨域,webman跨域中间件算是后端的跨域,前后端都设置允许跨域这个有问题吗?

  • xiuwang 2022-11-24

    webman返回的数据会经过nginx转发给客户端,webman加了跨域header,nginx又加了一次,所以跨域header重复了

  • lychuan 2022-11-24

    大佬,现在又出问题,nginx做跨域处理,后端webman没有加跨域中间件,get,post都正常,可是图片上传又提示跨域问题了,这可咋整呢?

  • lychuan 2022-11-24

    大佬,api请求没走webman的跨域中间件是怎么回事呢?

年代过于久远,无法发表回答
🔝