Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

跨域问题如何解决? #1

Closed
MFYDev opened this issue Mar 9, 2020 · 5 comments
Closed

跨域问题如何解决? #1

MFYDev opened this issue Mar 9, 2020 · 5 comments

Comments

@MFYDev
Copy link

MFYDev commented Mar 9, 2020

您好Carey,
在配置了一上午这个MyUrls之后我发现还是无法运行,subweb始终报错跨域有问题,我是宝塔面板安装的redis,php也安装了redis插件,redis-cli正常运行,后端地址配置无误,所有文件使用最新master。

跨域问题我在短链接反向代理配置文件中加入了

add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;

始终无效,后续换了很多也持续报错,请问这个跨域该怎么配置才是正确的呢?

@CareyWang
Copy link
Owner

项目使用 Golang 开发,打包生成的可执行文件可直接运行,无需使用 php。

没有使用过宝塔,建议服务器上看看redis-cli -h 127.0.0.1 -p 6379是不是能正常连接,或者按照 README 中重新安装 Redis。后续版本将加入 Redis host port 自定义参数与 docker-compose 部署支持。

跨域问题的话,我线上使用nginx配置反向代理,前端请求没有问题。

server {
    server_name example.com;

    location / {
        add_header 'Access-Control-Allow-Origin' '*';
        proxy_pass http://127.0.0.1:8002/;
    }
}

@MFYDev
Copy link
Author

MFYDev commented Mar 9, 2020

很奇怪,我这边还是不行,我的配置是这样的

首先短链接使用 https://link.mfy.cool 作为域名

然后后端输入 ./myurls.service -domain https://link.mfy.cool -port 8001 使用8001端口

然后在宝塔的反向代理里面 反向代理http://link.mfy.cool:8001 -> http://link.mfy.cool

SSL证书已申请

然后在反向代理配置里面加入 add_header 'Access-Control-Allow-Origin' '*';

最后宝塔的反向代理配置如下:

#PROXY-START/
location /
{
    proxy_pass http://link.mfy.cool:8001;
    proxy_set_header Host link.mfy.cool;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header REMOTE-HOST $remote_addr;
    
    add_header 'Access-Control-Allow-Origin' '*';
}

#PROXY-END/

前端的shortUrlbackend配置为 https://link.mfy.cool

最后使用的时候发现还是报错跨域缺少Access-Control-Allow-Origin

我人傻了。。。完全不知道错在哪里,请大佬指教一下,万分感谢了!

@MFYDev
Copy link
Author

MFYDev commented Mar 9, 2020

捕获
Redis正常运行,但是短链接后端就是这样

@MFYDev
Copy link
Author

MFYDev commented Mar 9, 2020

捕获
前端一直这样报错,域名解析是正常的

@CareyWang
Copy link
Owner

如果是配合sub-web使用,由于修改了请求方式,前端需要更新到最新。

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants