一个Https网站发送Http的 ajax请求的解决方法

但是在https网站内发送http的ajax请求报错

可以看到是在https://127.0.0.1 请求http://kafka1:8080/api/hello便失败。并且报错
Mixed Content: The page at ‘https://127.0.0.1/’ was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint ‘http://kafka1:8080/api/hello’. This request has been blocked; the content must be served over HTTPS.
大致意思就是https网站内不可以发送http请求,并且http请求直接被拦截并不发送到后台服务器。

解决方案如下几种:

修改https 为 http协议,大部分不可取
修改后台的http服务器为https协议,并修改前端代码
修改前端代码请求地址为nginx地址,使用当前https服务器对应的nginx转发前端https请求发送到后台http服务器,从而不用修改http服务器。
如第三种方案

首先我的hosts如下

127.0.0.1 kafka1
1
nginx配置如下:

# 配置后台服务器地址
upstream kafka1{
ip_hash;
server 127.0.0.1:8080;
}
server {
listen 443 ssl;
server_name kafka1;

ssl_certificate shfqcert.cer;
ssl_certificate_key cert.key;

ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;

ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;

location / {
root html;
index index.html index.htm;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
# 转发到后台服务器
proxy_pass http://kafka1;
proxy_buffer_size 8k;
proxy_buffers 64 32k;
}
}

然后前端可以发送请求之后,后台如果没有做跨域处理的话需要修改响应的请求头。

服务端需要设置响应头Access-Control-Allow-Origin为* 或者当前访问后端服务器的地址

如当前我这里是这个访问地址是https://localhost/

如 SpringMVC的 @CrossOrigin("https://localhost"),每个语言的都不尽相同,但都是响应头添加Access-Control-Allow-Origin罢了

原文:https://blog.csdn.net/u013076044/article/details/84261762
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自www.cnblogs.com/yumingzhao/p/10417485.html